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