source: josm/trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java@ 13812

Last change on this file since 13812 was 12746, checked in by bastiK, 7 years ago

see #15229 - fix deprecations in tests

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