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

Last change on this file since 11514 was 11514, checked in by Don-vip, 7 years ago

fix #14209 - EXIF: prefer DATETIME over DATETIME_DIGITIZED

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