ImageMetadataExtractor { protected void handle(com.drew.metadata.Metadata metadataExtractor) throws MetadataException { handle(metadataExtractor.getDirectories().iterator()); } ImageMetadataExtractor(Metadata metadata);  ImageMetadataExtractor(Metadata metadata, DirectoryHandler... handlers); void parseJpeg(File file); void parseTiff(File file); void parseWebP(File file); void parseHeif(InputStream is); void parseRawExif(InputStream stream, int length, boolean needsExifHeader); void parseRawExif(byte[] exifData); void parseRawXMP(byte[] xmpData);  }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testHandleDirectories() throws MetadataException { Metadata metadata = Mockito.mock(Metadata.class); ImageMetadataExtractor.DirectoryHandler handler1 = Mockito.mock(ImageMetadataExtractor.DirectoryHandler.class); ImageMetadataExtractor e = new ImageMetadataExtractor(metadata, handler1); Directory directory = new JpegCommentDirectory(); Iterator directories = Mockito.mock(Iterator.class); Mockito.when(directories.hasNext()).thenReturn(true, false); Mockito.when(directories.next()).thenReturn(directory); Mockito.when(handler1.supports(JpegCommentDirectory.class)).thenReturn(true); e.handle(directories); Mockito.verify(handler1).supports(JpegCommentDirectory.class); Mockito.verify(handler1).handle(directory, metadata); }
@Test public void testExifHandlerParseDate() throws MetadataException { ExifSubIFDDirectory exif = Mockito.mock(ExifSubIFDDirectory.class); Mockito.when(exif.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn(true); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault(), Locale.ROOT); calendar.setTimeInMillis(0); calendar.set(2000, 0, 1, 0, 0, 0); Mockito.when(exif.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn( calendar.getTime()); Metadata metadata = new Metadata(); new ImageMetadataExtractor.ExifHandler().handle(exif, metadata); assertEquals("Should be ISO date without time zone", "2000-01-01T00:00:00", metadata.get(TikaCoreProperties.CREATED)); }
@Test public void testExifHandlerParseDateFallback() throws MetadataException { ExifIFD0Directory exif = Mockito.mock(ExifIFD0Directory.class); Mockito.when(exif.containsTag(ExifIFD0Directory.TAG_DATETIME)).thenReturn(true); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault(), Locale.ROOT); calendar.setTimeInMillis(0); calendar.set(1999, 0, 1, 0, 0, 0); Mockito.when(exif.getDate(ExifIFD0Directory.TAG_DATETIME)).thenReturn( calendar.getTime()); Metadata metadata = new Metadata(); new ImageMetadataExtractor.ExifHandler().handle(exif, metadata); assertEquals("Should try EXIF Date/Time if Original is not set", "1999-01-01T00:00:00", metadata.get(TikaCoreProperties.CREATED)); }
@Test public void testExifHandlerParseDateError() throws MetadataException { ExifIFD0Directory exif = Mockito.mock(ExifIFD0Directory.class); Mockito.when(exif.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn(true); Mockito.when(exif.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn(null); Metadata metadata = new Metadata(); new ImageMetadataExtractor.ExifHandler().handle(exif, metadata); assertEquals("Parsing should proceed without date", null, metadata.get(TikaCoreProperties.CREATED)); }
@Test public void testCopyUnknownFieldsHandler() throws MetadataException { Directory d = Mockito.mock(Directory.class); Tag t1 = Mockito.mock(Tag.class); Mockito.when(t1.getTagName()).thenReturn("Image Description"); Mockito.when(t1.getDescription()).thenReturn("t1"); Tag t2 = Mockito.mock(Tag.class); Mockito.when(t2.getTagName()).thenReturn(TikaCoreProperties.SUBJECT.toString()); Mockito.when(t2.getDescription()).thenReturn("known"); Tag t3 = Mockito.mock(Tag.class); Mockito.when(t3.getTagName()).thenReturn(TikaCoreProperties.DESCRIPTION.getName()); Mockito.when(t3.getDescription()).thenReturn("known"); List<Tag> tags = Arrays.asList(t1, t2, t3); Mockito.when(d.getTags()).thenReturn(tags); Metadata metadata = new Metadata(); new ImageMetadataExtractor.CopyUnknownFieldsHandler().handle(d, metadata); assertEquals("t1", metadata.get("Image Description")); assertNull("keywords should be excluded from bulk copy because it is a defined field", metadata.get(TikaCoreProperties.SUBJECT)); assertNull(metadata.get(TikaCoreProperties.DESCRIPTION)); }