1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.text.DecimalFormat;
|
---|
10 | import java.text.ParseException;
|
---|
11 | import java.text.SimpleDateFormat;
|
---|
12 | import java.util.Date;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.BeforeEach;
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.coor.conversion.AbstractCoordinateFormat;
|
---|
20 | import org.openstreetmap.josm.data.coor.conversion.DMSCoordinateFormat;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * EXIF metadata extraction test
|
---|
27 | * @since 6209
|
---|
28 | */
|
---|
29 | class ExifReaderTest {
|
---|
30 | /**
|
---|
31 | * Set the timezone and timeout.
|
---|
32 | */
|
---|
33 | @RegisterExtension
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules();
|
---|
36 |
|
---|
37 | private File orientationSampleFile, directionSampleFile;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Setup test
|
---|
41 | */
|
---|
42 | @BeforeEach
|
---|
43 | public void setUp() {
|
---|
44 | directionSampleFile = new File("nodist/data/exif-example_direction.jpg");
|
---|
45 | orientationSampleFile = new File("nodist/data/exif-example_orientation=6.jpg");
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Test time extraction
|
---|
50 | * @throws ParseException if {@link ExifReader#readTime} fails to parse date/time of sample file
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | void testReadTime() throws ParseException {
|
---|
54 | Date date = ExifReader.readTime(directionSampleFile);
|
---|
55 | doTest("2010-05-15T17:12:05.000", date);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Tests reading sub-seconds from the EXIF header
|
---|
60 | * @throws ParseException if {@link ExifReader#readTime} fails to parse date/time of sample file
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | void testReadTimeSubSecond1() throws ParseException {
|
---|
64 | Date date = ExifReader.readTime(new File("nodist/data/IMG_20150711_193419.jpg"));
|
---|
65 | doTest("2015-07-11T19:34:19.100", date);
|
---|
66 | }
|
---|
67 |
|
---|
68 | private static void doTest(String expectedDate, Date parsedDate) {
|
---|
69 | assertEquals(expectedDate, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(parsedDate));
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static void doTestFile(String expectedDate, int ticket, String filename) {
|
---|
73 | doTest(expectedDate, ExifReader.readTime(new File(TestUtils.getRegressionDataFile(ticket, filename))));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Test orientation extraction
|
---|
78 | */
|
---|
79 | @Test
|
---|
80 | void testReadOrientation() {
|
---|
81 | Integer orientation = ExifReader.readOrientation(orientationSampleFile);
|
---|
82 | assertEquals(Integer.valueOf(6), orientation);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test coordinates extraction
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | void testReadLatLon() {
|
---|
90 | LatLon latlon = ExifReader.readLatLon(directionSampleFile);
|
---|
91 | assertNotNull(latlon);
|
---|
92 | DecimalFormat f = AbstractCoordinateFormat.newUnlocalizedDecimalFormat("00.0");
|
---|
93 | assertEquals("51°46'"+f.format(43.0)+"\"", DMSCoordinateFormat.degreesMinutesSeconds(latlon.lat()));
|
---|
94 | assertEquals("8°21'"+f.format(56.3)+"\"", DMSCoordinateFormat.degreesMinutesSeconds(latlon.lon()));
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Test direction extraction
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | void testReadDirection() {
|
---|
102 | assertEquals(Double.valueOf(46.5), ExifReader.readDirection(directionSampleFile));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Test speed extraction
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | void testReadSpeed() {
|
---|
110 | assertEquals(Double.valueOf(12.3), ExifReader.readSpeed(new File("nodist/data/exif-example_speed_ele.jpg")));
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Test elevation extraction
|
---|
115 | */
|
---|
116 | @Test
|
---|
117 | void testReadElevation() {
|
---|
118 | assertEquals(Double.valueOf(23.4), ExifReader.readElevation(new File("nodist/data/exif-example_speed_ele.jpg")));
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/11685">#11685</a>
|
---|
123 | * @throws IOException if an error occurs during reading
|
---|
124 | */
|
---|
125 | @Test
|
---|
126 | void testTicket11685() throws IOException {
|
---|
127 | doTestFile("2015-11-08T15:33:27.500", 11685, "2015-11-08_15-33-27-Xiaomi_YI-Y0030832.jpg");
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/14209">#14209</a>
|
---|
132 | * @throws IOException if an error occurs during reading
|
---|
133 | */
|
---|
134 | @Test
|
---|
135 | void testTicket14209() throws IOException {
|
---|
136 | doTestFile("2017-01-16T18:27:00.000", 14209, "0MbEfj1S--.1.jpg");
|
---|
137 | doTestFile("2016-08-13T19:51:13.000", 14209, "7VWFOryj--.1.jpg");
|
---|
138 | }
|
---|
139 | }
|
---|