source: josm/trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java@ 11486

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

fix #14275 - robustness against invalid timestamps in GPX files

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.date;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotEquals;
6import static org.junit.Assert.assertNotNull;
7
8import java.text.DateFormat;
9import java.util.Date;
10import java.util.TimeZone;
11
12import org.junit.Rule;
13import org.junit.Test;
14import org.openstreetmap.josm.testutils.JOSMTestRules;
15import org.openstreetmap.josm.tools.UncheckedParseException;
16
17import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18
19/**
20 * Unit tests of {@link DateUtils} class.
21 */
22public class DateUtilsTest {
23
24 /**
25 * Set the timezone and timeout.
26 * <p>
27 * Timeouts need to be disabled because we change the time zone.
28 */
29 @Rule
30 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
31 public JOSMTestRules test = new JOSMTestRules().i18n().preferences();
32
33 /**
34 * Allows to override the timezone used in {@link DateUtils} for unit tests.
35 * @param zone the timezone to use
36 */
37 public static void setTimeZone(TimeZone zone) {
38 TimeZone.setDefault(zone);
39 }
40
41 /**
42 * Test to parse date as returned for map data.
43 */
44 @Test
45 public void testMapDate() {
46 assertEquals(1344870637000L, DateUtils.fromString("2012-08-13T15:10:37Z").getTime());
47 }
48
49 /**
50 * Test to parse date as returned for note data.
51 */
52 @Test
53 public void testNoteDate() {
54 assertEquals(1417298930000L, DateUtils.fromString("2014-11-29 22:08:50 UTC").getTime());
55 }
56
57 /**
58 * Test to parse date as used in EXIF structures.
59 */
60 @Test
61 public void testExifDate() {
62 setTimeZone(TimeZone.getTimeZone("GMT+8:00")); // parsing is timezone aware
63 assertEquals(1443038712000L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12").getTime());
64 assertEquals(1443038712888L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12.888").getTime());
65 }
66
67 /**
68 * Test to parse date as used in GPX files
69 */
70 @Test
71 public void testGPXDate() {
72 assertEquals(1277465405000L, DateUtils.fromString("2010-06-25T11:30:05.000Z").getTime());
73 }
74
75 /**
76 * Test to parse date as defined in <a href="https://tools.ietf.org/html/rfc3339">RFC 3339</a>
77 */
78 @Test
79 public void testRfc3339() {
80 // examples taken from RFC
81 assertEquals(482196050520L, DateUtils.fromString("1985-04-12T23:20:50.52Z").getTime());
82 assertEquals(851042397000L, DateUtils.fromString("1996-12-19T16:39:57-08:00").getTime());
83 assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime());
84 }
85
86 /**
87 * Verifies that parsing an illegal date throws a {@link UncheckedParseException}
88 */
89 @Test(expected = UncheckedParseException.class)
90 public void testIllegalDate() {
91 DateUtils.fromString("2014-");
92 }
93
94 /**
95 * Tests that formatting a date w/ milliseconds does not cause incorrect parsing afterwards
96 */
97 @Test
98 public void testFormattingMillisecondsDoesNotCauseIncorrectParsing() {
99 DateUtils.fromDate(new Date(123));
100 assertEquals(1453694709000L, DateUtils.fromString("2016-01-25T04:05:09.000Z").getTime());
101 assertEquals(1453694709200L, DateUtils.fromString("2016-01-25T04:05:09.200Z").getTime());
102 assertEquals(1453694709400L, DateUtils.fromString("2016-01-25T04:05:09.400Z").getTime());
103 }
104
105 /**
106 * Unit test of {@link DateUtils#fromTimestamp} method.
107 */
108 @Test
109 public void testFromTimestamp() {
110 assertEquals("1970-01-01T00:00:00Z", DateUtils.fromTimestamp(0));
111 assertEquals("2001-09-09T01:46:40Z", DateUtils.fromTimestamp(1000000000));
112 assertEquals("2038-01-19T03:14:07Z", DateUtils.fromTimestamp(Integer.MAX_VALUE));
113 }
114
115 /**
116 * Unit test of {@link DateUtils#fromDate} method.
117 */
118 @Test
119 public void testFromDate() {
120 assertEquals("1970-01-01T00:00:00Z", DateUtils.fromDate(new Date(0)));
121 assertEquals("1970-01-01T00:00:00.1Z", DateUtils.fromDate(new Date(100)));
122 assertEquals("1970-01-01T00:00:00.12Z", DateUtils.fromDate(new Date(120)));
123 assertEquals("1970-01-01T00:00:00.123Z", DateUtils.fromDate(new Date(123)));
124 assertEquals("2016-01-25T04:05:09Z", DateUtils.fromDate(new Date(1453694709000L)));
125 }
126
127 /**
128 * Unit test of {@link DateUtils#formatTime} method.
129 */
130 @Test
131 public void testFormatTime() {
132 assertEquals("12:00 AM", DateUtils.formatTime(new Date(0), DateFormat.SHORT));
133 assertEquals("1:00 AM", DateUtils.formatTime(new Date(60 * 60 * 1000), DateFormat.SHORT));
134 assertEquals("12:00 AM", DateUtils.formatTime(new Date(999), DateFormat.SHORT));
135 // ignore seconds
136 assertEquals("12:00 AM", DateUtils.formatTime(new Date(5999), DateFormat.SHORT));
137
138 setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
139 assertEquals("1:00:00 AM CET", DateUtils.formatTime(new Date(0), DateFormat.LONG));
140 }
141
142 /**
143 * Unit test of {@link DateUtils#formatDate} method.
144 */
145 @Test
146 public void testFormatDate() {
147 assertEquals("1/1/70", DateUtils.formatDate(new Date(123), DateFormat.SHORT));
148 assertEquals("January 1, 1970", DateUtils.formatDate(new Date(123), DateFormat.LONG));
149 }
150
151 /**
152 * Unit test of {@link DateUtils#tsFromString} method.
153 */
154 @Test
155 public void testTsFromString() {
156 // UTC times
157 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00Z"));
158 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00"));
159 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03 15:00:00 UTC"));
160 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00"));
161 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00"));
162 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00:00"));
163 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00:00"));
164
165 // UTC times with millis
166 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000Z"));
167 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000"));
168 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000+00:00"));
169 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000-00:00"));
170
171 // Local time
172 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
173 assertEquals(1459688400000L, DateUtils.tsFromString("03-APR-16 15:00:00"));
174 }
175
176 /**
177 * Unit test of {@link DateUtils#getDateFormat} method.
178 */
179 @Test
180 public void testGetDateFormat() {
181 Boolean iso = DateUtils.PROP_ISO_DATES.get();
182 try {
183 DateFormat f1 = DateUtils.getDateFormat(DateFormat.SHORT);
184 assertNotNull(f1);
185 DateUtils.PROP_ISO_DATES.put(!iso);
186 DateFormat f2 = DateUtils.getDateFormat(DateFormat.SHORT);
187 assertNotNull(f1);
188 assertNotEquals(f1, f2);
189 } finally {
190 DateUtils.PROP_ISO_DATES.put(iso);
191 }
192 }
193
194 /**
195 * Unit test of {@link DateUtils#getTimeFormat} method.
196 */
197 @Test
198 public void testTimeFormat() {
199 Boolean iso = DateUtils.PROP_ISO_DATES.get();
200 try {
201 DateFormat f1 = DateUtils.getTimeFormat(DateFormat.SHORT);
202 assertNotNull(f1);
203 DateUtils.PROP_ISO_DATES.put(!iso);
204 DateFormat f2 = DateUtils.getTimeFormat(DateFormat.SHORT);
205 assertNotNull(f1);
206 assertNotEquals(f1, f2);
207 } finally {
208 DateUtils.PROP_ISO_DATES.put(iso);
209 }
210 }
211
212 /**
213 * Unit test to reach 100% code coverage.
214 */
215 @Test
216 @SuppressFBWarnings(value = "ISC_INSTANTIATE_STATIC_CLASS")
217 public void testCoverage() {
218 assertNotNull(new DateUtils());
219 }
220}
Note: See TracBrowser for help on using the repository browser.