Skip to content

Commit 4cd4140

Browse files
authored
Merge pull request #1748 from brenuart/gh1745
Truncate millis larger than 3 digits
2 parents 424f56c + abb168a commit 4cd4140

File tree

1 file changed

+49
-62
lines changed

1 file changed

+49
-62
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateDeserializationTZTest.java

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,16 @@ public void testDateUtilISO8601_Timezone() throws Exception
9797
// According to ISO8601, hours and minutes of the offset must be expressed with 2 digits
9898
// (not more, not less), i.e. Z or +hh:mm or -hh:mm. See https://www.w3.org/TR/NOTE-datetime.
9999
//
100-
// The forms below should be refused but some are accepted by the StdDateFormat. They are
101-
// included in the test to detect any change in behavior in futur releases...
100+
// The forms below are therefore ILLEGAL and must be refused.
102101
// ---------------------------------------------------------------------------------------------
103102

104-
// Interpreted as if there was no timezone, therefore producing a date with the TZ set on the mapper
105-
// FIXME it is probably better to refuse these cases instead of silently creating dates in local tz...
106-
failure( MAPPER, "2000-01-02T03:04:05.678+"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
107-
failure( MAPPER, "2000-01-02T03:04:05.678+1"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
103+
failure( MAPPER, "2000-01-02T03:04:05.678+");
104+
failure( MAPPER, "2000-01-02T03:04:05.678+1");
108105

109-
failure( MAPPER, "2000-01-02T03:04:05.678+001"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
110-
failure( MAPPER, "2000-01-02T03:04:05.678+00:"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
111-
failure( MAPPER, "2000-01-02T03:04:05.678+00:001"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
112-
failure( MAPPER, "2000-01-02T03:04:05.678+001:001"); // judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
106+
failure( MAPPER, "2000-01-02T03:04:05.678+001");
107+
failure( MAPPER, "2000-01-02T03:04:05.678+00:");
108+
failure( MAPPER, "2000-01-02T03:04:05.678+00:001");
109+
failure( MAPPER, "2000-01-02T03:04:05.678+001:001");
113110

114111
failure( MAPPER, "2000-01-02T03:04:05.678+1:");
115112
failure( MAPPER, "2000-01-02T03:04:05.678+00:1");
@@ -121,36 +118,30 @@ public void testDateUtilISO8601_Timezone() throws Exception
121118
public void testDateUtilISO8601_DateTimeMillis() throws Exception
122119
{
123120
// WITH timezone (from 4 to 0 digits)
124-
verify( MAPPER, "2000-01-02T03:04:05.678+01:00", judate(2000, 1, 2, 3, 4, 5, 678, "GMT+1"));
125-
verify( MAPPER, "2000-01-02T03:04:05.67+01:00", judate(2000, 1, 2, 3, 4, 5, 670, "GMT+1"));
126-
verify( MAPPER, "2000-01-02T03:04:05.6+01:00", judate(2000, 1, 2, 3, 4, 5, 600, "GMT+1"));
127-
verify( MAPPER, "2000-01-02T03:04:05+01:00", judate(2000, 1, 2, 3, 4, 5, 000, "GMT+1"));
121+
failure(MAPPER, "2000-01-02T03:04:05.0123456789+01:00"); // at most 9 digits for the millis
122+
verify( MAPPER, "2000-01-02T03:04:05.6789+01:00", judate(2000, 1, 2, 3, 4, 5, 678, "GMT+1"));
123+
verify( MAPPER, "2000-01-02T03:04:05.678+01:00", judate(2000, 1, 2, 3, 4, 5, 678, "GMT+1"));
124+
verify( MAPPER, "2000-01-02T03:04:05.67+01:00", judate(2000, 1, 2, 3, 4, 5, 670, "GMT+1"));
125+
verify( MAPPER, "2000-01-02T03:04:05.6+01:00", judate(2000, 1, 2, 3, 4, 5, 600, "GMT+1"));
126+
verify( MAPPER, "2000-01-02T03:04:05+01:00", judate(2000, 1, 2, 3, 4, 5, 000, "GMT+1"));
128127

129128

130-
// failure(MAPPER, "2000-01-02T03:04:05.6789+01:00");
131-
// failure( MAPPER, "2000-01-02T03:04:05.6789Z"); // , judate(2000, 1, 2, 3, 4, 11, 789, "UTC"));
132-
133129
// WITH timezone Z (from 4 to 0 digits)
134-
// FIXME the .6789 millis are interpreted as 6789 millisecondes or 6.789 seconds!
135-
verify( MAPPER, "2000-01-02T03:04:05.678Z", judate(2000, 1, 2, 3, 4, 5, 678, "UTC"));
136-
verify( MAPPER, "2000-01-02T03:04:05.67Z", judate(2000, 1, 2, 3, 4, 5, 670, "UTC"));
137-
// FIXME should be 670 millis instead of 67
138-
verify( MAPPER, "2000-01-02T03:04:05.6Z", judate(2000, 1, 2, 3, 4, 5, 600, "UTC"));
139-
// FIXME should be 600 millis instead of 6
140-
verify( MAPPER, "2000-01-02T03:04:05Z", judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
130+
failure(MAPPER, "2000-01-02T03:04:05.0123456789Z"); // at most 9 digits for the millis
131+
verify( MAPPER, "2000-01-02T03:04:05.6789Z", judate(2000, 1, 2, 3, 4, 5, 678, "UTC"));
132+
verify( MAPPER, "2000-01-02T03:04:05.678Z", judate(2000, 1, 2, 3, 4, 5, 678, "UTC"));
133+
verify( MAPPER, "2000-01-02T03:04:05.67Z", judate(2000, 1, 2, 3, 4, 5, 670, "UTC"));
134+
verify( MAPPER, "2000-01-02T03:04:05.6Z", judate(2000, 1, 2, 3, 4, 5, 600, "UTC"));
135+
verify( MAPPER, "2000-01-02T03:04:05Z", judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
141136

142137

143138
// WITHOUT timezone (from 4 to 0 digits)
144-
// FIXME: the .6789 millis are interpreted as 6789 millisecondes or 6.789 seconds!
145-
146-
verify( MAPPER, "2000-01-02T03:04:05.678", judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
147-
verify( MAPPER, "2000-01-02T03:04:05.67", judate(2000, 1, 2, 3, 4, 5, 670, LOCAL_TZ));
148-
// FIXME: the .67 millis are interpreted as 67 seconds.
149-
150-
verify( MAPPER, "2000-01-02T03:04:05.6", judate(2000, 1, 2, 3, 4, 5, 600, LOCAL_TZ));
151-
verify( MAPPER, "2000-01-02T03:04:05", judate(2000, 1, 2, 3, 4, 5, 000, LOCAL_TZ));
152-
153-
// failure(MAPPER, "2000-01-02T03:04:05.6789"); // judate(2000, 1, 2, 3, 4, 11, 789, LOCAL_TZ));
139+
failure(MAPPER, "2000-01-02T03:04:05.0123456789"); // at most 9 digits for the millis
140+
verify( MAPPER, "2000-01-02T03:04:05.6789", judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
141+
verify( MAPPER, "2000-01-02T03:04:05.678", judate(2000, 1, 2, 3, 4, 5, 678, LOCAL_TZ));
142+
verify( MAPPER, "2000-01-02T03:04:05.67", judate(2000, 1, 2, 3, 4, 5, 670, LOCAL_TZ));
143+
verify( MAPPER, "2000-01-02T03:04:05.6", judate(2000, 1, 2, 3, 4, 5, 600, LOCAL_TZ));
144+
verify( MAPPER, "2000-01-02T03:04:05", judate(2000, 1, 2, 3, 4, 5, 000, LOCAL_TZ));
154145

155146

156147
// ---------------------------------------------------------------------------------------------
@@ -165,17 +156,16 @@ public void testDateUtilISO8601_DateTimeMillis() throws Exception
165156
// time-secfrac = "." 1*DIGIT
166157
// partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
167158
//
168-
// The second fraction (ie the millis) is optional and can be ommitted. However, a fraction
159+
// The second fraction (ie the millis) is optional and can be omitted. However, a fraction
169160
// with only a dot (.) and no digit is not allowed.
170161
//
171-
// The forms below should be refused but some are accepted by the StdDateFormat. They are
172-
// included in the test to detect any change in behavior in futur releases...
162+
// The forms below are therefore ILLEGAL and must be refused.
173163
// ---------------------------------------------------------------------------------------------
174164

175165
// millis part with only a dot (.) and no digits
176-
failure( MAPPER, "2000-01-02T03:04:05.+01:00"); // judate(2000, 1, 2, 3, 4, 5, 000, "GMT+1"));
177-
failure( MAPPER, "2000-01-02T03:04:05."); // judate(2000, 1, 2, 3, 4, 5, 000, LOCAL_TZ));
178-
failure(MAPPER, "2000-01-02T03:04:05.Z"); // FIXME this one fails, but not the others...
166+
failure( MAPPER, "2000-01-02T03:04:05.+01:00");
167+
failure( MAPPER, "2000-01-02T03:04:05.");
168+
failure( MAPPER, "2000-01-02T03:04:05.Z");
179169
}
180170

181171

@@ -192,26 +182,24 @@ public void testDateUtilISO8601_DateTime() throws Exception
192182
// No timezone --> the one configured on the ObjectMapper must be used
193183
verify(MAPPER, "2000-01-02T03:04:05", judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
194184

195-
// Hours, minutes and seconds are mandatory when time is specified
185+
// Hours, minutes are mandatory but seconds are optional
196186
failure(MAPPER, "2000-01-02T");
197187
failure(MAPPER, "2000-01-02T03");
198188
failure(MAPPER, "2000-01-02T03:");
199189
verify(MAPPER, "2000-01-02T03:04", judate(2000, 1, 2, 3, 4, 0, 0, LOCAL_TZ));
200190
failure(MAPPER, "2000-01-02T03:04:");
201191

202-
// Although hours, minutes and seconds are mandatory, they can sometimes be omitted
203-
// if a TZ is specified... !!??
192+
// Hours, minutes are mandatory but seconds are optional - test with a TZ
204193
failure(MAPPER, "2000-01-02T+01:00");
205194
failure(MAPPER, "2000-01-02T03+01:00");
206195
failure(MAPPER, "2000-01-02T03:+01:00");
207-
verify( MAPPER, "2000-01-02T03:04+01:00", judate(2000, 1, 2, 3, 4, 0, 0, "GMT+1")); // FIXME should be refused
196+
verify( MAPPER, "2000-01-02T03:04+01:00", judate(2000, 1, 2, 3, 4, 0, 0, "GMT+1"));
208197
failure(MAPPER, "2000-01-02T03:04:+01:00");
209198

210199
failure(MAPPER, "2000-01-02TZ");
211200
failure(MAPPER, "2000-01-02T03Z");
212201
failure(MAPPER, "2000-01-02T03:Z");
213202
verify(MAPPER, "2000-01-02T03:04Z", judate(2000, 1, 2, 3, 4, 0, 0, "UTC"));
214-
215203
failure(MAPPER, "2000-01-02T03:04:Z");
216204

217205

@@ -233,8 +221,8 @@ public void testDateUtilISO8601_DateTime() throws Exception
233221
// Behavior should be the SAME whatever the timezone and/or the millis.
234222

235223
// seconds (no TZ)
236-
failure( MAPPER, "2000-01-02T03:04:5"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
237-
failure( MAPPER, "2000-01-02T03:04:5.000"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
224+
failure( MAPPER, "2000-01-02T03:04:5");
225+
failure( MAPPER, "2000-01-02T03:04:5.000");
238226
failure(MAPPER, "2000-01-02T03:04:005");
239227

240228
// seconds (+01:00)
@@ -244,13 +232,13 @@ public void testDateUtilISO8601_DateTime() throws Exception
244232

245233
// seconds (Z)
246234
failure(MAPPER, "2000-01-02T03:04:5Z");
247-
failure( MAPPER, "2000-01-02T03:04:5.000Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
235+
failure( MAPPER, "2000-01-02T03:04:5.000Z");
248236
failure(MAPPER, "2000-01-02T03:04:005Z");
249237

250238

251239
// minutes (no TZ)
252-
failure( MAPPER, "2000-01-02T03:4:05"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
253-
failure( MAPPER, "2000-01-02T03:4:05.000"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
240+
failure( MAPPER, "2000-01-02T03:4:05");
241+
failure( MAPPER, "2000-01-02T03:4:05.000");
254242
failure(MAPPER, "2000-01-02T03:004:05");
255243

256244
// minutes (+01:00)
@@ -259,13 +247,13 @@ public void testDateUtilISO8601_DateTime() throws Exception
259247
failure(MAPPER, "2000-01-02T03:004:05+01:00");
260248

261249
// minutes (Z)
262-
failure( MAPPER, "2000-01-02T03:4:05Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
263-
failure( MAPPER, "2000-01-02T03:4:05.000Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
264-
failure( MAPPER, "2000-01-02T03:004:05Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
250+
failure( MAPPER, "2000-01-02T03:4:05Z");
251+
failure( MAPPER, "2000-01-02T03:4:05.000Z");
252+
failure( MAPPER, "2000-01-02T03:004:05Z");
265253

266254
// hour (no TZ)
267-
failure( MAPPER, "2000-01-02T3:04:05"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
268-
failure( MAPPER, "2000-01-02T3:04:05.000"); // judate(2000, 1, 2, 3, 4, 5, 0, LOCAL_TZ));
255+
failure( MAPPER, "2000-01-02T3:04:05");
256+
failure( MAPPER, "2000-01-02T3:04:05.000");
269257
failure(MAPPER, "2000-01-02T003:04:05");
270258

271259
// hour (+01:00)
@@ -274,9 +262,9 @@ public void testDateUtilISO8601_DateTime() throws Exception
274262
failure(MAPPER, "2000-01-02T003:04:05+01:00");
275263

276264
// hour (Z)
277-
failure( MAPPER, "2000-01-02T3:04:05Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
278-
failure( MAPPER, "2000-01-02T3:04:05.000Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
279-
failure( MAPPER, "2000-01-02T003:04:05Z"); // judate(2000, 1, 2, 3, 4, 5, 0, "UTC"));
265+
failure( MAPPER, "2000-01-02T3:04:05Z");
266+
failure( MAPPER, "2000-01-02T3:04:05.000Z");
267+
failure( MAPPER, "2000-01-02T003:04:05Z");
280268
}
281269

282270
/**
@@ -302,18 +290,18 @@ public void testDateUtilISO8601_Date() throws Exception
302290
// ---------------------------------------------------------------------------------------------
303291

304292
// day
305-
failure( MAPPER, "2000-01-2"); // judate(2000, 1, 2, 0, 0, 0, 0, LOCAL_TZ));
293+
failure( MAPPER, "2000-01-2");
306294
failure( MAPPER, "2000-01-002");
307295

308296
// month
309-
failure( MAPPER, "2000-1-02"); // judate(2000, 1, 2, 0, 0, 0, 0, LOCAL_TZ));
297+
failure( MAPPER, "2000-1-02");
310298
failure( MAPPER, "2000-001-02");
311299

312300
// year
313301
failure( MAPPER, "20000-01-02");
314302
failure( MAPPER, "200-01-02" );
315303
failure( MAPPER, "20-01-02" );
316-
failure( MAPPER, "2-01-02"); // judate(2, 1, 2, 0, 0, 0, 0, LOCAL_TZ)); // FIXME Why accept 1 digit and refuse they other cases??
304+
failure( MAPPER, "2-01-02");
317305
}
318306

319307
/**
@@ -559,7 +547,6 @@ private static void failure(ObjectMapper mapper, Object input, Class<? extends E
559547
catch(Exception e) {
560548
// Is it the expected exception ?
561549
if (!exceptionType.isAssignableFrom(e.getClass()) ) {
562-
e.printStackTrace();
563550
fail("Wrong exception thrown when reading "+input+", actual: "+e.getClass().getName() + "("+e.getMessage()+"), expected: "+exceptionType.getName());
564551
}
565552
}

0 commit comments

Comments
 (0)