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

Last change on this file since 19057 was 19057, checked in by stoecker, 5 weeks ago

fix checkstyle

  • Property svn:eol-style set to native
File size: 13.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.date;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNotEquals;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertNotSame;
8import static org.junit.jupiter.api.Assertions.assertNull;
9import static org.junit.jupiter.api.Assertions.assertThrows;
10
11import java.text.DateFormat;
12import java.time.Instant;
13import java.time.format.FormatStyle;
14import java.util.Date;
15import java.util.Random;
16import java.util.TimeZone;
17import java.util.concurrent.ForkJoinPool;
18
19import org.junit.jupiter.api.Disabled;
20import org.junit.jupiter.api.Test;
21import org.junit.jupiter.params.ParameterizedTest;
22import org.junit.jupiter.params.provider.ValueSource;
23import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
24import org.openstreetmap.josm.testutils.annotations.I18n;
25import org.openstreetmap.josm.tools.UncheckedParseException;
26import org.openstreetmap.josm.tools.Utils;
27
28import net.trajano.commons.testing.UtilityClassTestUtil;
29
30/**
31 * Unit tests of {@link DateUtils} class.
32 */
33@BasicPreferences
34@I18n
35public class DateUtilsTest {
36 /**
37 * Tests that {@code DateUtils} satisfies utility class criteria.
38 * @throws ReflectiveOperationException if an error occurs
39 */
40 @Test
41 void testUtilityClass() throws ReflectiveOperationException {
42 UtilityClassTestUtil.assertUtilityClassWellDefined(DateUtils.class);
43 }
44
45 /**
46 * Allows to override the timezone used in {@link DateUtils} for unit tests.
47 * @param zone the timezone to use
48 */
49 public static void setTimeZone(TimeZone zone) {
50 TimeZone.setDefault(zone);
51 }
52
53 /**
54 * Test to parse date as returned for map data.
55 */
56 @Test
57 void testMapDate() {
58 assertEquals(1344870637000L, DateUtils.fromString("2012-08-13T15:10:37Z").getTime());
59 }
60
61 /**
62 * Test to parse date as returned for note data.
63 */
64 @Test
65 void testNoteDate() {
66 assertEquals(1417298930000L, DateUtils.fromString("2014-11-29 22:08:50 UTC").getTime());
67 }
68
69 /**
70 * Test to parse date as used in EXIF structures.
71 */
72 @Test
73 void testExifDate() {
74 assertEquals(1443038712000L, DateUtils.fromString("2015:09:23 20:05:12").getTime());
75 assertEquals(1443038712888L, DateUtils.fromString("2015:09:23 20:05:12.888").getTime());
76 }
77
78 /**
79 * Test to parse date as used in GPX files
80 */
81 @Test
82 void testGPXDate() {
83 assertEquals(1277465405000L, DateUtils.fromString("2010-06-25T11:30:05.000Z").getTime());
84 }
85
86 /**
87 * Test to parse date as defined in <a href="https://tools.ietf.org/html/rfc3339">RFC 3339</a>
88 */
89 @Test
90 void testRfc3339() {
91 // examples taken from RFC
92 assertEquals(482196050520L, DateUtils.fromString("1985-04-12T23:20:50.52Z").getTime());
93 assertEquals(851042397000L, DateUtils.fromString("1996-12-19T16:39:57-08:00").getTime());
94 assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime());
95 // (partial) dates
96 assertEquals(482112000000L, DateUtils.fromString("1985-04-12").getTime());
97 assertEquals(481161600000L, DateUtils.fromString("1985-04").getTime());
98 assertEquals(473385600000L, DateUtils.fromString("1985").getTime());
99 }
100
101 @Test
102 void testRtklib() {
103 // examples taken from rtklib .pos files
104 assertEquals("2019-04-21T08:20:32Z", DateUtils.parseInstant("2019/04/21 08:20:32").toString());
105 assertEquals("2019-06-08T08:23:12.123Z", DateUtils.parseInstant("2019/06/08 08:23:12.123").toString());
106 assertEquals("2021-03-30T15:04:01.123456Z", DateUtils.parseInstant("2021/03/30 15:04:01.123456").toString());
107 }
108
109 /**
110 * Verifies that parsing an illegal date throws a {@link UncheckedParseException}
111 */
112 @ParameterizedTest
113 @ValueSource(strings = {"2014-", "2014-01-", "2014-01-01T", "2014-00-01", "2014-01-00"})
114 void testIllegalDate(String date) {
115 assertThrows(UncheckedParseException.class, () -> DateUtils.fromString(date));
116 }
117
118 /**
119 * Tests that formatting a date w/ milliseconds does not cause incorrect parsing afterwards
120 */
121 @Test
122 void testFormattingMillisecondsDoesNotCauseIncorrectParsing() {
123 DateUtils.fromDate(new Date(123));
124 assertEquals(1453694709000L, DateUtils.fromString("2016-01-25T04:05:09.000Z").getTime());
125 assertEquals(1453694709200L, DateUtils.fromString("2016-01-25T04:05:09.200Z").getTime());
126 assertEquals(1453694709400L, DateUtils.fromString("2016-01-25T04:05:09.400Z").getTime());
127 }
128
129 /**
130 * Unit test of {@link DateUtils#fromTimestamp} method.
131 */
132 @Test
133 void testFromTimestamp() {
134 assertEquals("1970-01-01T00:00:00Z", DateUtils.fromTimestamp(0));
135 assertEquals("2001-09-09T01:46:40Z", DateUtils.fromTimestamp(1000000000));
136 assertEquals("2038-01-19T03:14:07Z", DateUtils.fromTimestamp(Integer.MAX_VALUE));
137 }
138
139 /**
140 * Unit test of {@link DateUtils#fromDate} method.
141 */
142 @Test
143 void testFromDate() {
144 assertEquals("1970-01-01T00:00:00Z", DateUtils.fromDate(new Date(0)));
145 assertEquals("1970-01-01T00:00:00.1Z", DateUtils.fromDate(new Date(100)));
146 assertEquals("1970-01-01T00:00:00.12Z", DateUtils.fromDate(new Date(120)));
147 assertEquals("1970-01-01T00:00:00.123Z", DateUtils.fromDate(new Date(123)));
148 assertEquals("2016-01-25T04:05:09Z", DateUtils.fromDate(new Date(1453694709000L)));
149 }
150
151 /**
152 * Unit test of {@link DateUtils#formatTime} method.
153 */
154 @Test
155 void testFormatTime() {
156 // Somewhere between Java 17 and Java 21, a non-breaking space replaced the original space between the time and AM/PM.
157 final var separator = Utils.getJavaVersion() >= 21 ? '\u202f' : ' ';
158 final var twelveAM = "12:00" + separator + "AM";
159 assertEquals(twelveAM, DateUtils.formatTime(new Date(0), DateFormat.SHORT));
160 assertEquals("1:00" + separator + "AM", DateUtils.formatTime(new Date(60 * 60 * 1000), DateFormat.SHORT));
161 assertEquals(twelveAM, DateUtils.formatTime(new Date(999), DateFormat.SHORT));
162 // ignore seconds
163 assertEquals(twelveAM, DateUtils.formatTime(new Date(5999), DateFormat.SHORT));
164
165 setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
166 assertEquals("1:00:00" + separator + "AM GMT+01:00", DateUtils.formatTime(new Date(0),
167 DateFormat.LONG), "This is mostly dependent upon java.locale.providers. CET is also OK.");
168 }
169
170 /**
171 * Unit test of {@link DateUtils#formatDate} method.
172 */
173 @Test
174 void testFormatDate() {
175 assertEquals("1/1/70", DateUtils.formatDate(new Date(123), DateFormat.SHORT));
176 assertEquals("January 1, 1970", DateUtils.formatDate(new Date(123), DateFormat.LONG));
177 }
178
179 /**
180 * Unit test of {@link DateUtils#tsFromString} method.
181 */
182 @Test
183 void testTsFromString() {
184 // UTC times
185 assertEquals(1459641600000L, DateUtils.tsFromString("2016-04-03"));
186 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00Z"));
187 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00"));
188 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03 15:00:00 UTC"));
189 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00"));
190 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00"));
191 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00:00"));
192 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00:00"));
193
194 // UTC times with millis
195 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000Z"));
196 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000"));
197 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000+00:00"));
198 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000-00:00"));
199 assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123+00:00"));
200 assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123-00:00"));
201
202 // Offset times
203 assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03"));
204 assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05"));
205 assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03:00"));
206 assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05:00"));
207 assertEquals(1459695600123L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123+03:00"));
208 assertEquals(1459695600123L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123-05:00"));
209
210 // Local time
211 setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
212 assertEquals(1459688400000L, DateUtils.tsFromString("03-APR-16 15:00:00"));
213 }
214
215 @Test
216 @Disabled("slow; use for thread safety testing")
217 void testTsFromString800k() throws Exception {
218 new ForkJoinPool(64).submit(() -> new Random()
219 .longs(800_000)
220 .parallel()
221 .forEach(ignore -> testTsFromString())).get();
222 }
223
224 /**
225 * Unit test of {@link DateUtils#tsFromString} method.
226 */
227 @Test
228 void testTsFromStringInvalid1() {
229 assertThrows(UncheckedParseException.class, () -> DateUtils.tsFromString("foobar"));
230 }
231
232 /**
233 * Unit test of {@link DateUtils#tsFromString} method.
234 */
235 @Test
236 void testTsFromStringInvalid2() {
237 assertThrows(UncheckedParseException.class, () -> DateUtils.tsFromString("2016/04/03"));
238 }
239
240 /**
241 * Unit test of {@link DateUtils#getDateFormat} method.
242 */
243 @Test
244 void testGetDateFormat() {
245 Boolean iso = DateUtils.PROP_ISO_DATES.get();
246 try {
247 DateFormat f1 = DateUtils.getDateFormat(DateFormat.SHORT);
248 assertNotNull(f1);
249 DateUtils.PROP_ISO_DATES.put(!iso);
250 DateFormat f2 = DateUtils.getDateFormat(DateFormat.SHORT);
251 assertNotNull(f1);
252 assertNotEquals(f1, f2);
253 DateUtils.PROP_ISO_DATES.put(true);
254 assertEquals("2006-01-02", DateUtils.getDateFormatter(null).format(Instant.parse("2006-01-02T15:04:05.777Z")));
255 } finally {
256 DateUtils.PROP_ISO_DATES.put(iso);
257 }
258 }
259
260 /**
261 * Unit test of {@link DateUtils#getTimeFormat} method.
262 */
263 @Test
264 void testTimeFormat() {
265 Boolean iso = DateUtils.PROP_ISO_DATES.get();
266 try {
267 DateFormat f1 = DateUtils.getTimeFormat(DateFormat.SHORT);
268 assertNotNull(f1);
269 DateUtils.PROP_ISO_DATES.put(!iso);
270 DateFormat f2 = DateUtils.getTimeFormat(DateFormat.SHORT);
271 assertNotNull(f1);
272 assertNotEquals(f1, f2);
273 DateUtils.PROP_ISO_DATES.put(true);
274 assertEquals("15:04:05.777", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:05.777Z")));
275 assertEquals("15:04:05", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:05Z")));
276 assertEquals("15:04:00", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:00Z")));
277 } finally {
278 DateUtils.PROP_ISO_DATES.put(iso);
279 }
280 }
281
282 @Test
283 void testCloneDate() {
284 assertNull(DateUtils.cloneDate(null));
285 final Date date = new Date(1453694709000L);
286 assertEquals(date, DateUtils.cloneDate(date));
287 assertNotSame(date, DateUtils.cloneDate(date));
288 }
289
290 /**
291 * Unit test of {@link DateUtils#getDateTimeFormatter} method.
292 */
293 @Test
294 void testDateTimeFormatter() {
295 Instant instant = Instant.parse("2006-01-02T15:04:05.777Z");
296 Boolean iso = DateUtils.PROP_ISO_DATES.get();
297 try {
298 assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
299 assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
300 assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
301 DateUtils.PROP_ISO_DATES.put(!iso);
302 assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
303 assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
304 assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
305 DateUtils.PROP_ISO_DATES.put(true);
306 assertEquals("2006-01-02 15:04:05.777", DateUtils.getDateTimeFormatter(null, null).format(instant));
307 assertEquals(Instant.parse("2006-01-02T15:04:05.000Z"),
308 DateUtils.getDateTimeFormatter(null, null).parse("2006-01-02 15:04:05", Instant::from));
309 assertEquals(Instant.parse("2006-01-02T15:04:00.000Z"),
310 DateUtils.getDateTimeFormatter(null, null).parse("2006-01-02 15:04", Instant::from));
311 } finally {
312 DateUtils.PROP_ISO_DATES.put(iso);
313 }
314 }
315
316 /**
317 * Some Java version use narrow no-break space ("NNBSP") instead of a space.
318 * @param time The time string with NNBSP instead of a space
319 * @return The time with spaces instead of NNBSP
320 */
321 private static String replaceWhitespace(String time) {
322 return time.replace((char) 8239 /* "NNBSP" */, ' ');
323 }
324}
Note: See TracBrowser for help on using the repository browser.