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

Last change on this file since 10956 was 10569, checked in by Don-vip, 8 years ago

fix #13157 - Make DateUtilsTest not time out on some time zones (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 4.1 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.util.Calendar;
13import java.util.Date;
14import java.util.GregorianCalendar;
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(new GregorianCalendar(2010, Calendar.MAY, 15, 17, 12, 05).getTime(), date);
58
59 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
60 date = ExifReader.readTime(directionSampleFile);
61 TimeZone.setDefault(DateUtils.UTC);
62 assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 15, 12, 05).getTime(), date);
63 }
64
65 /**
66 * Tests reading sub-seconds from the EXIF header
67 * @throws ParseException if {@link ExifReader#readTime} fails to parse date/time of sample file
68 */
69 @Test
70 public void testReadTimeSubSecond1() throws ParseException {
71 Date date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg"));
72 String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
73 assertEquals("2015-07-11T19:34:19.100", dateStr);
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 dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
79 assertEquals("2015-07-11T17:34:19.100", dateStr);
80 }
81
82 /**
83 * Test orientation extraction
84 */
85 @Test
86 public void testReadOrientation() {
87 Integer orientation = ExifReader.readOrientation(orientationSampleFile);
88 assertEquals(Integer.valueOf(6), orientation);
89 }
90
91 /**
92 * Test coordinates extraction
93 */
94 @Test
95 public void testReadLatLon() {
96 LatLon latlon = ExifReader.readLatLon(directionSampleFile);
97 assertNotNull(latlon);
98 DecimalFormat f = new DecimalFormat("00.0");
99 assertEquals("51°46'"+f.format(43.0)+"\"", LatLon.dms(latlon.lat()));
100 assertEquals("8°21'"+f.format(56.3)+"\"", LatLon.dms(latlon.lon()));
101 }
102
103 /**
104 * Test direction extraction
105 */
106 @Test
107 public void testReadDirection() {
108 assertEquals(Double.valueOf(46.5), ExifReader.readDirection(directionSampleFile));
109 }
110
111 /**
112 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/11685">#11685</a>
113 * @throws IOException if an error occurs during reading
114 */
115 @Test
116 public void testTicket11685() throws IOException {
117 File file = new File(TestUtils.getRegressionDataFile(11685, "2015-11-08_15-33-27-Xiaomi_YI-Y0030832.jpg"));
118 String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(ExifReader.readTime(file));
119 assertEquals("2015-11-08T15:33:27.500", dateStr);
120 }
121}
Note: See TracBrowser for help on using the repository browser.