1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.date;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.TimeZone;
|
---|
8 |
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Unit tests of {@link DateUtils} class.
|
---|
14 | */
|
---|
15 | public class DateUtilsTest {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Allows to override the timezone used in {@link DateUtils} for unit tests.
|
---|
19 | * @param zone the timezone to use
|
---|
20 | */
|
---|
21 | public static void setTimeZone(TimeZone zone) {
|
---|
22 | DateUtils.setTimeZone(zone);
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Test to parse date as returned for map data.
|
---|
27 | */
|
---|
28 | @Test
|
---|
29 | public void testMapDate() {
|
---|
30 | assertEquals(1344870637000L, DateUtils.fromString("2012-08-13T15:10:37Z").getTime());
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Test to parse date as returned for note data.
|
---|
35 | */
|
---|
36 | @Test
|
---|
37 | public void testNoteDate() {
|
---|
38 | assertEquals(1417298930000L, DateUtils.fromString("2014-11-29 22:08:50 UTC").getTime());
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Test to parse date as used in EXIF structures.
|
---|
43 | */
|
---|
44 | @Test
|
---|
45 | public void testExifDate() {
|
---|
46 | setTimeZone(TimeZone.getTimeZone("GMT+8:00")); // parsing is timezone aware
|
---|
47 | assertEquals(1443038712000L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12").getTime());
|
---|
48 | assertEquals(1443038712888L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12.888").getTime());
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Test to parse date as used in GPX files
|
---|
53 | */
|
---|
54 | @Test
|
---|
55 | public void testGPXDate() {
|
---|
56 | assertEquals(1277465405000L, DateUtils.fromString("2010-06-25T11:30:05.000Z").getTime());
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Test to parse date as defined in <a href="https://tools.ietf.org/html/rfc3339">RFC 3339</a>
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | public void testRfc3339() {
|
---|
64 | // examples taken from RFC
|
---|
65 | assertEquals(482196050520L, DateUtils.fromString("1985-04-12T23:20:50.52Z").getTime());
|
---|
66 | assertEquals(851042397000L, DateUtils.fromString("1996-12-19T16:39:57-08:00").getTime());
|
---|
67 | assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime());
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Verifies that parsing an illegal date throws a {@link UncheckedParseException}
|
---|
72 | */
|
---|
73 | @Test(expected = UncheckedParseException.class)
|
---|
74 | public void testIllegalDate() {
|
---|
75 | DateUtils.fromString("2014-");
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Tests that formatting a date w/ milliseconds does not cause incorrect parsing afterwards
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | public void testFormattingMillisecondsDoesNotCauseIncorrectParsing() {
|
---|
83 | DateUtils.fromDate(new Date(123));
|
---|
84 | assertEquals(1453694709000L, DateUtils.fromString("2016-01-25T04:05:09.000Z").getTime());
|
---|
85 | assertEquals(1453694709200L, DateUtils.fromString("2016-01-25T04:05:09.200Z").getTime());
|
---|
86 | assertEquals(1453694709400L, DateUtils.fromString("2016-01-25T04:05:09.400Z").getTime());
|
---|
87 | }
|
---|
88 | }
|
---|