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

Last change on this file since 19055 was 19055, checked in by taylor.smock, 4 weeks ago

Fix tests in Java 21

  • Regenerate Java 21 image files (probably from r19043: Drop COMPAT locale provider).
  • Remove Java 8 image files, since we no longer support Java 8.
  • Fix an issue where Java 21 uses a non-breaking space between time and AM/PM.
  • 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), DateFormat.LONG), "This is mostly dependent upon java.locale.providers. CET is also OK.");
167 }
168
169 /**
170 * Unit test of {@link DateUtils#formatDate} method.
171 */
172 @Test
173 void testFormatDate() {
174 assertEquals("1/1/70", DateUtils.formatDate(new Date(123), DateFormat.SHORT));
175 assertEquals("January 1, 1970", DateUtils.formatDate(new Date(123), DateFormat.LONG));
176 }
177
178 /**
179 * Unit test of {@link DateUtils#tsFromString} method.
180 */
181 @Test
182 void testTsFromString() {
183 // UTC times
184 assertEquals(1459641600000L, DateUtils.tsFromString("2016-04-03"));
185 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00Z"));
186 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00"));
187 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03 15:00:00 UTC"));
188 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00"));
189 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00"));
190 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00+00:00"));
191 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00-00:00"));
192
193 // UTC times with millis
194 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000Z"));
195 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000"));
196 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000+00:00"));
197 assertEquals(1459695600000L, DateUtils.tsFromString("2016-04-03T15:00:00.000-00:00"));
198 assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123+00:00"));
199 assertEquals(1459695600123L, DateUtils.tsFromString("2016-04-03T15:00:00.123-00:00"));
200
201 // Offset times
202 assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03"));
203 assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05"));
204 assertEquals(1459695600000L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00+03:00"));
205 assertEquals(1459695600000L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00-05:00"));
206 assertEquals(1459695600123L - 3 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123+03:00"));
207 assertEquals(1459695600123L + 5 * 3600 * 1000, DateUtils.tsFromString("2016-04-03T15:00:00.123-05:00"));
208
209 // Local time
210 setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
211 assertEquals(1459688400000L, DateUtils.tsFromString("03-APR-16 15:00:00"));
212 }
213
214 @Test
215 @Disabled("slow; use for thread safety testing")
216 void testTsFromString800k() throws Exception {
217 new ForkJoinPool(64).submit(() -> new Random()
218 .longs(800_000)
219 .parallel()
220 .forEach(ignore -> testTsFromString())).get();
221 }
222
223 /**
224 * Unit test of {@link DateUtils#tsFromString} method.
225 */
226 @Test
227 void testTsFromStringInvalid1() {
228 assertThrows(UncheckedParseException.class, () -> DateUtils.tsFromString("foobar"));
229 }
230
231 /**
232 * Unit test of {@link DateUtils#tsFromString} method.
233 */
234 @Test
235 void testTsFromStringInvalid2() {
236 assertThrows(UncheckedParseException.class, () -> DateUtils.tsFromString("2016/04/03"));
237 }
238
239 /**
240 * Unit test of {@link DateUtils#getDateFormat} method.
241 */
242 @Test
243 void testGetDateFormat() {
244 Boolean iso = DateUtils.PROP_ISO_DATES.get();
245 try {
246 DateFormat f1 = DateUtils.getDateFormat(DateFormat.SHORT);
247 assertNotNull(f1);
248 DateUtils.PROP_ISO_DATES.put(!iso);
249 DateFormat f2 = DateUtils.getDateFormat(DateFormat.SHORT);
250 assertNotNull(f1);
251 assertNotEquals(f1, f2);
252 DateUtils.PROP_ISO_DATES.put(true);
253 assertEquals("2006-01-02", DateUtils.getDateFormatter(null).format(Instant.parse("2006-01-02T15:04:05.777Z")));
254 } finally {
255 DateUtils.PROP_ISO_DATES.put(iso);
256 }
257 }
258
259 /**
260 * Unit test of {@link DateUtils#getTimeFormat} method.
261 */
262 @Test
263 void testTimeFormat() {
264 Boolean iso = DateUtils.PROP_ISO_DATES.get();
265 try {
266 DateFormat f1 = DateUtils.getTimeFormat(DateFormat.SHORT);
267 assertNotNull(f1);
268 DateUtils.PROP_ISO_DATES.put(!iso);
269 DateFormat f2 = DateUtils.getTimeFormat(DateFormat.SHORT);
270 assertNotNull(f1);
271 assertNotEquals(f1, f2);
272 DateUtils.PROP_ISO_DATES.put(true);
273 assertEquals("15:04:05.777", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:05.777Z")));
274 assertEquals("15:04:05", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:05Z")));
275 assertEquals("15:04:00", DateUtils.getTimeFormatter(null).format(Instant.parse("2006-01-02T15:04:00Z")));
276 } finally {
277 DateUtils.PROP_ISO_DATES.put(iso);
278 }
279 }
280
281 @Test
282 void testCloneDate() {
283 assertNull(DateUtils.cloneDate(null));
284 final Date date = new Date(1453694709000L);
285 assertEquals(date, DateUtils.cloneDate(date));
286 assertNotSame(date, DateUtils.cloneDate(date));
287 }
288
289 /**
290 * Unit test of {@link DateUtils#getDateTimeFormatter} method.
291 */
292 @Test
293 void testDateTimeFormatter() {
294 Instant instant = Instant.parse("2006-01-02T15:04:05.777Z");
295 Boolean iso = DateUtils.PROP_ISO_DATES.get();
296 try {
297 assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
298 assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
299 assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
300 DateUtils.PROP_ISO_DATES.put(!iso);
301 assertNotNull(DateUtils.getDateFormatter(FormatStyle.SHORT).format(instant));
302 assertNotNull(DateUtils.getTimeFormatter(FormatStyle.SHORT).format(instant));
303 assertNotNull(DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(instant));
304 DateUtils.PROP_ISO_DATES.put(true);
305 assertEquals("2006-01-02 15:04:05.777", DateUtils.getDateTimeFormatter(null, null).format(instant));
306 assertEquals(Instant.parse("2006-01-02T15:04:05.000Z"),
307 DateUtils.getDateTimeFormatter(null, null).parse("2006-01-02 15:04:05", Instant::from));
308 assertEquals(Instant.parse("2006-01-02T15:04:00.000Z"),
309 DateUtils.getDateTimeFormatter(null, null).parse("2006-01-02 15:04", Instant::from));
310 } finally {
311 DateUtils.PROP_ISO_DATES.put(iso);
312 }
313 }
314
315 /**
316 * Some Java version use narrow no-break space ("NNBSP") instead of a space.
317 * @param time The time string with NNBSP instead of a space
318 * @return The time with spaces instead of NNBSP
319 */
320 private static String replaceWhitespace(String time) {
321 return time.replace((char) 8239 /* "NNBSP" */, ' ');
322 }
323}
Note: See TracBrowser for help on using the repository browser.