Subversion Repositories general

Compare Revisions

No changes between revisions

Ignore whitespace Rev 1267 → Rev 1268

/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/MetadataTest.java
0,0 → 1,176
/*
* MetadataTest.java
*
* Test class written by Drew Noakes.
*
* This is public domain software - that is, you can do whatever you want
* with it, and include it software that is licensed under the GNU or the
* BSD license, or whatever other licence you choose, including proprietary
* closed source licenses. I do ask that you leave this header in tact.
*
* If you make modifications to this code that you think would benefit the
* wider community, please send me a copy and I'll post it on my site.
*
* If you make use of this code, I'd appreciate hearing about it.
* drew@drewnoakes.com
* Latest version of this software kept at
* http://drewnoakes.com/
*
* Created by dnoakes on 26-Oct-2002 18:35:12 using IntelliJ IDEA.
*/
package com.drew.metadata.test;
 
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.lang.NullOutputStream;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifDirectory;
import com.drew.metadata.exif.GpsDirectory;
import com.drew.metadata.iptc.IptcDirectory;
import junit.framework.TestCase;
 
import java.io.*;
import java.util.Iterator;
 
/**
* JUnit test case for class Metadata.
* @author Drew Noakes http://drewnoakes.com
*/
public class MetadataTest extends TestCase
{
public MetadataTest(String s)
{
super(s);
}
 
public void testSetAndGetSingleTag() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(ExifDirectory.class);
directory.setInt(ExifDirectory.TAG_APERTURE, 1);
assertEquals(1, directory.getInt(ExifDirectory.TAG_APERTURE));
}
 
public void testSetSameTagMultpleTimes() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(ExifDirectory.class);
directory.setInt(ExifDirectory.TAG_APERTURE, 1);
directory.setInt(ExifDirectory.TAG_APERTURE, 2);
assertEquals("setting the tag with a different value should override old value",
2, directory.getInt(ExifDirectory.TAG_APERTURE));
}
 
public void testGetDirectory() throws Exception
{
Metadata metadata = new Metadata();
assertTrue(metadata.getDirectory(ExifDirectory.class) instanceof ExifDirectory);
}
 
public void testSetAndGetMultipleTagsInSingleDirectory() throws Exception
{
Metadata metadata = new Metadata();
Directory exifDir = metadata.getDirectory(ExifDirectory.class);
exifDir.setString(ExifDirectory.TAG_APERTURE, "Tag Value");
exifDir.setString(ExifDirectory.TAG_BATTERY_LEVEL, "Another tag");
assertEquals("Tag Value", exifDir.getString(ExifDirectory.TAG_APERTURE));
assertEquals("Another tag", exifDir.getString(ExifDirectory.TAG_BATTERY_LEVEL));
}
 
public void testSetAndGetMultipleTagsInMultilpeDirectories() throws Exception
{
Metadata metadata = new Metadata();
Directory exifDir = metadata.getDirectory(ExifDirectory.class);
Directory gpsDir = metadata.getDirectory(GpsDirectory.class);
exifDir.setString(ExifDirectory.TAG_APERTURE, "ExifAperture");
exifDir.setString(ExifDirectory.TAG_BATTERY_LEVEL, "ExifBatteryLevel");
gpsDir.setString(GpsDirectory.TAG_GPS_ALTITUDE, "GpsAltitude");
gpsDir.setString(GpsDirectory.TAG_GPS_DEST_BEARING, "GpsDestBearing");
assertEquals("ExifAperture", exifDir.getString(ExifDirectory.TAG_APERTURE));
assertEquals("ExifBatteryLevel", exifDir.getString(ExifDirectory.TAG_BATTERY_LEVEL));
assertEquals("GpsAltitude", gpsDir.getString(GpsDirectory.TAG_GPS_ALTITUDE));
assertEquals("GpsDestBearing", gpsDir.getString(GpsDirectory.TAG_GPS_DEST_BEARING));
}
 
/*
public void testCountTags() throws Exception
{
Metadata info = new Metadata();
assertEquals(0, info.countTags());
 
info.setString(ExifReader.DIRECTORY_EXIF_EXIF, ExifDirectory.TAG_APERTURE, "ExifAperture");
assertEquals(1, info.countTags());
info.setString(ExifReader.DIRECTORY_EXIF_EXIF, ExifDirectory.TAG_BATTERY_LEVEL, "ExifBatteryLevel");
assertEquals(2, info.countTags());
info.setString(ExifReader.DIRECTORY_EXIF_GPS, GpsDirectory.TAG_GPS_ALTITUDE, "GpsAltitude");
assertEquals(3, info.countTags());
info.setString(ExifReader.DIRECTORY_EXIF_GPS, GpsDirectory.TAG_GPS_DEST_BEARING, "GpsDestBearing");
assertEquals(4, info.countTags());
}
*/
 
public void testContainsTag() throws Exception
{
Metadata metadata = new Metadata();
Directory exifDir = metadata.getDirectory(ExifDirectory.class);
assertTrue(!exifDir.containsTag(ExifDirectory.TAG_APERTURE));
exifDir.setString(ExifDirectory.TAG_APERTURE, "Tag Value");
assertTrue(exifDir.containsTag(ExifDirectory.TAG_APERTURE));
}
 
public void testGetNonExistantTag() throws Exception
{
Metadata metadata = new Metadata();
Directory exifDir = metadata.getDirectory(ExifDirectory.class);
assertEquals(null, exifDir.getString(ExifDirectory.TAG_APERTURE));
}
 
public void testHasErrors() throws Exception
{
Metadata metadata = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/exif/test/badExif.jpg"));
assertTrue("exif error", metadata.getDirectory(ExifDirectory.class).hasErrors());
metadata = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/exif/test/withExif.jpg"));
assertTrue("no errors", !metadata.getDirectory(ExifDirectory.class).hasErrors());
}
 
public void testGetErrors() throws Exception
{
Metadata metadata = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/exif/test/badExif.jpg"));
Iterator errors = metadata.getDirectory(ExifDirectory.class).getErrors();
assertTrue(errors.hasNext());
String error = (String) errors.next();
assertEquals("Exif data segment must contain at least 14 bytes", error);
assertTrue(!errors.hasNext());
}
 
public void testGetErrorCount() throws Exception
{
Metadata metadata = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/exif/test/badExif.jpg"));
assertEquals(1, metadata.getDirectory(ExifDirectory.class).getErrorCount());
}
 
public void testMetadataSerializable() throws Exception
{
Metadata metadata = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/test/withIptcExifGps.jpg"));
new ObjectOutputStream(new NullOutputStream()).writeObject(metadata);
}
 
public void testSerializeAndRestore() throws Exception
{
Metadata metadataWrite = JpegMetadataReader.readMetadata(new File("src/com/drew/metadata/test/withIptcExifGps.jpg"));
Metadata metadataRead;
File ser = File.createTempFile("test", "ser");
try {
// write the ser object
new ObjectOutputStream(new FileOutputStream(ser)).writeObject(metadataWrite);
// read the ser object
metadataRead = (Metadata)new ObjectInputStream(new FileInputStream(ser)).readObject();
// make sure they're equivalent
// TODO should compare the two objects via iteration of directories and tags
assertTrue(metadataRead.containsDirectory(ExifDirectory.class));
assertTrue(metadataRead.containsDirectory(IptcDirectory.class));
} finally {
ser.delete();
}
}
}
/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/withIptcExifGps.jpg
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes:
Added: svn:mime-type
+application/octet-stream
\ No newline at end of property
/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/SpecialTests.java
0,0 → 1,121
/*
* This is public domain software - that is, you can do whatever you want
* with it, and include it software that is licensed under the GNU or the
* BSD license, or whatever other licence you choose, including proprietary
* closed source licenses. I do ask that you leave this header in tact.
*
* If you make modifications to this code that you think would benefit the
* wider community, please send me a copy and I'll post it on my site.
*
* If you make use of this code, I'd appreciate hearing about it.
* drew@drewnoakes.com
* Latest version of this software kept at
* http://drewnoakes.com/
*
* Created by dnoakes on 07-May-2005 12:38:18 using IntelliJ IDEA.
*/
package com.drew.metadata.test;
 
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.imaging.jpeg.JpegSegmentData;
import com.drew.imaging.jpeg.JpegSegmentReader;
import junit.framework.TestCase;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
 
/**
*
*/
public class SpecialTests extends TestCase
{
public void testExtractMetadataToASeparateFile() throws Exception
{
String filename = "src/com/drew/metadata/exif/test/nikonMakernoteType2";
JpegSegmentData segmentData = new JpegSegmentReader(new File(filename + ".jpg")).getSegmentData();
segmentData.removeSegment(JpegSegmentReader.SEGMENT_DHT);
segmentData.removeSegment(JpegSegmentReader.SEGMENT_DQT);
segmentData.removeSegment(JpegSegmentReader.SEGMENT_SOF0);
segmentData.removeSegment(JpegSegmentReader.SEGMENT_SOI);
ObjectOutputStream outputStream = null;
try
{
outputStream = new ObjectOutputStream(new FileOutputStream(new File(filename + ".metadata")));
outputStream.writeObject(segmentData);
}
finally
{
if (outputStream!=null)
outputStream.close();
}
}
 
public void testScanFoldersForImagesThatCauseFailures() throws Exception
{
// String directory = "G:/Recovered Images/AquariumC"; // 1446 files 883 MB (done)
// String directory = "G:/Recovered Images/AquariumF"; // 25,378 files 34.4 GB
// String directory = "G:/Recovered Images/DesktopC"; // 41,518 files 8.73 GB
// String directory = "G:/Recovered Images/DesktopF"; // 8,016 files 5.11 GB (done)
// String directory = "C:/Documents and Settings/Drew/My Documents/IntelliJ Projects/MetadataExtractor/src/";
// String directory = "C:/Documents and Settings/Drew/My Documents/IntelliJ Projects/MetadataExtractor/Sample Images";
String directory = "\\\\annie\\htdocs\\drewnoakes.com\\code\\exif\\exifImages";
processDirectory(directory);
System.out.println("Complete test successfully.");
}
 
private void processDirectory(String pathName)
{
File directory = new File(pathName);
String[] directoryItems = directory.list();
if (directoryItems==null)
return;
 
for (int i=0; i<directoryItems.length; i++) {
String subItem = directoryItems[i].toLowerCase();
File file = new File(directory, subItem);
if (!file.exists())
throw new RuntimeException("World gone nuts.");
 
if (file.isDirectory())
{
processDirectory(file.getAbsolutePath());
}
else if (subItem.endsWith(".jpg") || subItem.endsWith(".jpeg"))
{
// process this item
try
{
JpegSegmentReader segmentReader = new JpegSegmentReader(file);
try
{
JpegMetadataReader.extractMetadataFromJpegSegmentReader(segmentReader);
}
catch (Throwable t)
{
// general, uncaught exception during processing of metadata
System.err.println(file + "[BadMetadata]");
System.err.println(t);
System.err.println(t.getMessage());
t.printStackTrace(System.err);
}
}
catch (JpegProcessingException e)
{
System.err.println(file + "[BadSegments]");
// this is an error in the Jpeg segment structure. we're looking for bad handling of
// metadata segments. in this case, we didn't even get a segment.
}
catch (Throwable t)
{
// general, uncaught exception during processing of jpeg segments
System.err.println(file + "[FAILURE]");
System.err.println(t);
System.err.println(t.getMessage());
t.printStackTrace(System.err);
}
}
}
}
}
/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/DirectoryTest.java
0,0 → 1,109
/*
* This is public domain software - that is, you can do whatever you want
* with it, and include it software that is licensed under the GNU or the
* BSD license, or whatever other licence you choose, including proprietary
* closed source licenses. I do ask that you leave this header in tact.
*
* If you make modifications to this code that you think would benefit the
* wider community, please send me a copy and I'll post it on my site.
*
* If you make use of this code, I'd appreciate hearing about it.
* drew@drewnoakes.com
* Latest version of this software kept at
* http://drewnoakes.com/
*
* Created by dnoakes on 29-Nov-2002 08:40:07 using IntelliJ IDEA.
*/
package com.drew.metadata.test;
 
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import junit.framework.TestCase;
 
import java.util.GregorianCalendar;
 
/**
*
*/
public class DirectoryTest extends TestCase
{
public DirectoryTest(String s)
{
super(s);
}
 
public void testSetAndGetInt() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(MockDirectory.class);
int value = 321;
int tagType = 123;
directory.setInt(tagType, value);
assertEquals(value, directory.getInt(tagType));
assertEquals(Integer.toString(value), directory.getString(tagType));
}
 
public void testSetAndGetIntArray() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(MockDirectory.class);
int[] inputValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int tagType = 123;
directory.setIntArray(tagType, inputValues);
int[] outputValues = directory.getIntArray(tagType);
assertEquals(inputValues.length, outputValues.length);
for (int i = 0; i < inputValues.length; i++) {
int inputValue = inputValues[i];
int outputValue = outputValues[i];
assertEquals(inputValue, outputValue);
}
assertEquals(inputValues, directory.getIntArray(tagType));
StringBuffer outputString = new StringBuffer();
for (int i = 0; i < inputValues.length; i++) {
int inputValue = inputValues[i];
if (i > 0) {
outputString.append(' ');
}
outputString.append(inputValue);
}
assertEquals(outputString.toString(), directory.getString(tagType));
}
 
public void testSetStringAndGetDate() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(MockDirectory.class);
String date1 = "2002:01:30 24:59:59";
String date2 = "2002:01:30 24:59";
String date3 = "2002-01-30 24:59:59";
String date4 = "2002-01-30 24:59";
directory.setString(1, date1);
directory.setString(2, date2);
directory.setString(3, date3);
directory.setString(4, date4);
assertEquals(date1, directory.getString(1));
assertEquals(new GregorianCalendar(2002, GregorianCalendar.JANUARY, 30, 24, 59, 59).getTime(), directory.getDate(1));
assertEquals(new GregorianCalendar(2002, GregorianCalendar.JANUARY, 30, 24, 59, 0).getTime(), directory.getDate(2));
assertEquals(new GregorianCalendar(2002, GregorianCalendar.JANUARY, 30, 24, 59, 59).getTime(), directory.getDate(3));
assertEquals(new GregorianCalendar(2002, GregorianCalendar.JANUARY, 30, 24, 59, 0).getTime(), directory.getDate(4));
}
 
public void testSetIntArrayGetByteArray() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(MockDirectory.class);
int[] ints = {1, 2, 3, 4, 5};
directory.setIntArray(1, ints);
assertEquals(ints.length, directory.getByteArray(1).length);
assertEquals(1, directory.getByteArray(1)[0]);
}
 
public void testSetStringGetInt() throws Exception
{
Metadata metadata = new Metadata();
Directory directory = metadata.getDirectory(MockDirectory.class);
byte[] bytes = { 0x01, 0x02, 0x03 };
directory.setString(1, new String(bytes));
assertEquals(0x010203, directory.getInt(1));
}
}
/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/AllTests.java
0,0 → 1,75
/*
* AllTests.java
*
* Test suite class written by Drew Noakes.
*
* This is public domain software - that is, you can do whatever you want
* with it, and include it software that is licensed under the GNU or the
* BSD license, or whatever other licence you choose, including proprietary
* closed source licenses. I do ask that you leave this header in tact.
*
* If you make modifications to this code that you think would benefit the
* wider community, please send me a copy and I'll post it on my site.
*
* If you make use of this code, I'd appreciate hearing about it.
* drew@drewnoakes.com
* Latest version of this software kept at
* http://drewnoakes.com/
*
* Created by dnoakes on 26-Oct-2002 16:29:44 using IntelliJ IDEA.
* - First collection of basic unit tests, to compile against JUnit
* - Doesn't yet cover all classes
*/
package com.drew.metadata.test;
 
import com.drew.imaging.jpeg.test.JpegMetadataReaderTest;
import com.drew.imaging.jpeg.test.JpegSegmentDataTest;
import com.drew.imaging.jpeg.test.JpegSegmentReaderTest;
import com.drew.lang.test.CompoundExceptionTest;
import com.drew.lang.test.NullOutputStreamTest;
import com.drew.lang.test.RationalTest;
import com.drew.metadata.exif.test.*;
import com.drew.metadata.iptc.test.IptcReaderTest;
import com.drew.metadata.jpeg.test.JpegComponentTest;
import com.drew.metadata.jpeg.test.JpegDescriptorTest;
import com.drew.metadata.jpeg.test.JpegDirectoryTest;
import com.drew.metadata.jpeg.test.JpegReaderTest;
import junit.framework.Test;
import junit.framework.TestSuite;
 
/**
* The complete test suite for the metadata-extractor library.
* @author Drew Noakes http://drewnoakes.com
*/
public class AllTests extends TestSuite
{
public static Test suite()
{
TestSuite suite = new TestSuite();
 
suite.addTestSuite(DirectoryTest.class);
suite.addTestSuite(ExifDirectoryTest.class);
suite.addTestSuite(ExifReaderTest.class);
suite.addTestSuite(ExifDescriptorTest.class);
suite.addTestSuite(IptcReaderTest.class);
suite.addTestSuite(MetadataTest.class);
suite.addTestSuite(JpegReaderTest.class);
suite.addTestSuite(JpegSegmentDataTest.class);
suite.addTestSuite(JpegDirectoryTest.class);
suite.addTestSuite(JpegComponentTest.class);
suite.addTestSuite(JpegDescriptorTest.class);
suite.addTestSuite(NikonType1MakernoteTest.class);
suite.addTestSuite(NikonType2MakernoteTest1.class);
suite.addTestSuite(NikonType2MakernoteTest2.class);
suite.addTestSuite(CanonMakernoteDescriptorTest.class);
 
suite.addTestSuite(CompoundExceptionTest.class);
suite.addTestSuite(NullOutputStreamTest.class);
suite.addTestSuite(RationalTest.class);
 
suite.addTestSuite(JpegMetadataReaderTest.class);
suite.addTestSuite(JpegSegmentReaderTest.class);
 
return suite;
}
}
/contrib/metadata-extractor/trunk/src/com/drew/metadata/test/MockDirectory.java
0,0 → 1,41
/*
* This is public domain software - that is, you can do whatever you want
* with it, and include it software that is licensed under the GNU or the
* BSD license, or whatever other licence you choose, including proprietary
* closed source licenses. I do ask that you leave this header in tact.
*
* If you make modifications to this code that you think would benefit the
* wider community, please send me a copy and I'll post it on my site.
*
* If you make use of this code, I'd appreciate hearing about it.
* drew@drewnoakes.com
* Latest version of this software kept at
* http://drewnoakes.com/
*
* Created by dnoakes on 29-Nov-2002 09:07:43 using IntelliJ IDEA.
*/
package com.drew.metadata.test;
 
import com.drew.metadata.Directory;
 
import java.util.HashMap;
 
public class MockDirectory extends Directory
{
private final HashMap _tagNameMap;
 
public MockDirectory()
{
this._tagNameMap = new HashMap();
}
 
public String getName()
{
return "";
}
 
protected HashMap getTagNameMap()
{
return _tagNameMap;
}
}