diff --git a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java index a2a1367a195b0ed63e57903f7a2b660d798c3a4c..99474019fd5cdcd92a95a703a99bdb4c0e5c537e 100644 --- a/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java +++ b/src/main/java/NTNU/IDATT1002/utils/MetaDataExtractor.java @@ -80,7 +80,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Make") || tag.toString().contains("Model")) { - cameraInformation += tag.toString() + ","; + cameraInformation += tag.toString() + ", "; } } if(!(cameraInformation.isBlank())) @@ -105,7 +105,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Lens")) { - lensInformation += tag.toString() + ","; + lensInformation += tag.toString() + ", "; } } if(!(lensInformation.isBlank())) @@ -131,7 +131,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Aperture")) { - apertureInformation += tag.toString() + ","; + apertureInformation += tag.toString() + ", "; } } if(!(apertureInformation.isBlank())) @@ -156,7 +156,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Shutter")) { - shutterSpeedInformation += tag.toString() + ","; + shutterSpeedInformation += tag.toString() + ", "; } } if(!(shutterSpeedInformation.isBlank())) @@ -181,7 +181,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("ISO")) { - isoInformation += tag.toString() + ","; + isoInformation += tag.toString() + ", "; } } if(!(isoInformation.isBlank())) @@ -206,7 +206,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Focal Length")) { - focalLengthInformation += tag.toString() + ","; + focalLengthInformation += tag.toString() + ", "; break; } } @@ -232,7 +232,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(FileTypeDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("File Type Name")) { - fileTypeInformation += tag.toString() + ","; + fileTypeInformation += tag.toString() + ", "; break; } } @@ -258,13 +258,13 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(IptcDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Date Created")) { - dateInformation += tag.toString() + ","; + dateInformation += tag.toString() + ", "; break; } } for(Tag tag : directory.getTags()) { if(tag.toString().contains("Time Created")) { - dateInformation += tag.toString() + ","; + dateInformation += tag.toString() + ", "; break; } } @@ -290,7 +290,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(FileSystemDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Size")) { - fileSize += tag.toString() + ","; + fileSize += tag.toString() + ", "; } } if(!(fileSize.isBlank())) @@ -316,7 +316,7 @@ public class MetaDataExtractor { directory = metadata.getFirstDirectoryOfType(JpegDirectory.class); for(Tag tag : directory.getTags()) { if(tag.toString().contains("Height") || tag.toString().contains("Width")) { - fileDimension += tag.toString() + ","; + fileDimension += tag.toString() + ", "; } } if(!(fileDimension.isBlank())) @@ -350,9 +350,9 @@ public class MetaDataExtractor { String removingText = directoryToRemove.getName(); textToClean = textToClean.replace(removingText, ""); textToClean = textToClean.replace("[", ""); - textToClean = textToClean.replace("]", ""); + textToClean = textToClean.replace("] ", ""); textToClean = textToClean.replace(" - ", ": "); - textToClean = textToClean.substring(0, textToClean.length()-1); + textToClean = textToClean.substring(0, textToClean.length()-2); return textToClean; } } diff --git a/src/test/java/NTNU/IDATT1002/utils/MetaDataExtractorTest.java b/src/test/java/NTNU/IDATT1002/utils/MetaDataExtractorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6e9e5b3c428fede0c70b044852b16aec9c7c2ef1 --- /dev/null +++ b/src/test/java/NTNU/IDATT1002/utils/MetaDataExtractorTest.java @@ -0,0 +1,148 @@ +package NTNU.IDATT1002.utils; + +import static org.junit.jupiter.api.Assertions.*; + +import NTNU.IDATT1002.models.GeoLocation; +import NTNU.IDATT1002.models.Metadata; +import java.io.File; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link MetaDataExtractor} + * These test are not the best and are best used as a proof of concept + * For testing we have used two predefined pictures so we know what metadata is supposed to receive + * + * @author madslun + * @version 1.0 13.04.20 + */ +class MetaDataExtractorTest { + + private MetaDataExtractor metaDataExtractor; + private File geolocationImage; + private File metadataImage; + + /** + * Sets up necessary test data for the tests + */ + @BeforeEach + void setUp() { + geolocationImage = new File("src/test/resources/Images/plsWork.jpg"); + metadataImage = new File("src/test/resources/Images/pb.jpg"); + metaDataExtractor = new MetaDataExtractor(); + } + + /** + * Test extracts geolocation from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetGeoLocation() { + String expectedLatitude = "51.504105555555554"; + String expectedLongitude = "-0.074575"; + GeoLocation returnedGeoLocation = metaDataExtractor.getGeoLocation(geolocationImage); + assertEquals(expectedLatitude, returnedGeoLocation.getLatitude()); + assertEquals(expectedLongitude, returnedGeoLocation.getLongitude()); + } + + /** + * Test extracts camerainformation from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetCamera() { + String expectedCamera = "Make: NIKON CORPORATION, Model: NIKON D610"; + String extractedCamera = metaDataExtractor.getCamera(metadataImage); + assertEquals(expectedCamera, extractedCamera); + } + + /** + * Test extracts lens information from a chosen image and compares with a predefined string containing expected information + */ + + @Test + void testGetLens() { + String expectedLens = "Lens Specification: 24-70mm f/2.8, Lens Model: 24.0-70.0 mm f/2.8"; + String extractedLens = metaDataExtractor.getLens(metadataImage); + assertEquals(expectedLens, extractedLens); + } + + /** + * Test extracts aperture information from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetAperture() { + String expectedAperture = "Aperture Value: f/2.8, Max Aperture Value: f/2.8"; + String extractedAperture = metaDataExtractor.getAperture(metadataImage); + assertEquals(expectedAperture, extractedAperture); + } + + /** + * Test extracts shutterspeed information from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetShutterSpeed() { + String expectedShutterSpeed = "Shutter Speed Value: 1/124 sec"; + String extractedAperture = metaDataExtractor.getShutterSpeed(metadataImage); + assertEquals(expectedShutterSpeed, extractedAperture); + } + + /** + * Test extracts iso information from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetIso() { + String expectedIso = "ISO Speed Ratings: 640"; + String extractedIso = metaDataExtractor.getIso(metadataImage); + assertEquals(expectedIso, extractedIso); + } + + /** + * Test extracts focal length from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetFocalLength() { + String expectedFocalLength = "Focal Length: 70 mm"; + String extractedFocalLength = metaDataExtractor.getFocalLength(metadataImage); + assertEquals(expectedFocalLength, extractedFocalLength); + } + + /** + * Test extracts file type from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetFileType() { + String expectedFileType = "Detected Name: JPEG"; + String extractedFileType = metaDataExtractor.getFileType(metadataImage); + System.out.println(extractedFileType); + assertEquals(expectedFileType, extractedFileType); + } + + /** + * Test extracts photo date from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetPhotoDate() { + String expectedPhotoDate = "Date Created: 2020:02:15, Time Created: 12:07:01"; + String extractedPhotoDate = metaDataExtractor.getPhotoDate(metadataImage); + assertEquals(expectedPhotoDate, extractedPhotoDate); + } + + /** + * Test extracts file size from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetFileSize() { + String expectedFileSize = " Size: 821839 bytes"; + String extractedFileSize = metaDataExtractor.getFileSize(metadataImage); + assertEquals(expectedFileSize, extractedFileSize); + } + + /** + * Test extracts file dimension from a chosen image and compares with a predefined string containing expected information + */ + @Test + void testGetFileDimension() { + String expectedFileDimension = "Image Height: 930 pixels, Image Width: 1394 pixels"; + String extractedFileDimension = metaDataExtractor.getFileDimension(metadataImage); + assertEquals(expectedFileDimension, extractedFileDimension); + } +} \ No newline at end of file diff --git a/src/test/resources/Images/pb.jpg b/src/test/resources/Images/pb.jpg new file mode 100644 index 0000000000000000000000000000000000000000..68c54e9d8499bb64957fac781965cf30d8736e58 Binary files /dev/null and b/src/test/resources/Images/pb.jpg differ diff --git a/src/test/resources/Images/plsWork.jpg b/src/test/resources/Images/plsWork.jpg new file mode 100644 index 0000000000000000000000000000000000000000..7a5464d68ee6a01d18ae56f896ba083adfa55a46 Binary files /dev/null and b/src/test/resources/Images/plsWork.jpg differ