@@ -49,6 +49,14 @@ public class LocalDateTimeDeserTest
49
49
{
50
50
private final static ObjectMapper MAPPER = newMapper ();
51
51
private final static ObjectReader READER = MAPPER .readerFor (LocalDateTime .class );
52
+
53
+ private final static ObjectMapper STRICT_MAPPER ;
54
+ static {
55
+ STRICT_MAPPER = newMapper ();
56
+ STRICT_MAPPER .configOverride (LocalDateTime .class )
57
+ .setFormat (JsonFormat .Value .forLeniency (false ));
58
+ }
59
+
52
60
private final TypeReference <Map <String , LocalDateTime >> MAP_TYPE_REF = new TypeReference <Map <String , LocalDateTime >>() { };
53
61
54
62
final static class StrictWrapper {
@@ -155,28 +163,56 @@ public void testDeserializationAsString01() throws Exception
155
163
public void testDeserializationAsString02 () throws Exception
156
164
{
157
165
LocalDateTime time = LocalDateTime .of (2013 , Month .AUGUST , 21 , 9 , 22 , 57 );
158
- LocalDateTime value = MAPPER .readValue ('"' + time .toString () + '"' , LocalDateTime .class );
166
+ LocalDateTime value = MAPPER .readValue (q ( time .toString ()) , LocalDateTime .class );
159
167
assertEquals ("The value is not correct." , time , value );
160
168
}
161
169
162
170
@ Test
163
171
public void testDeserializationAsString03 () throws Exception
164
172
{
165
173
LocalDateTime time = LocalDateTime .of (2005 , Month .NOVEMBER , 5 , 22 , 31 , 5 , 829837 );
166
- LocalDateTime value = MAPPER .readValue ('"' + time .toString () + '"' , LocalDateTime .class );
174
+ LocalDateTime value = MAPPER .readValue (q ( time .toString ()) , LocalDateTime .class );
167
175
assertEquals ("The value is not correct." , time , value );
168
176
}
169
177
170
- // [modules-java#94]: Should not include timezone
178
+ /*
179
+ /**********************************************************
180
+ /* Tests for deserializing from textual representation,
181
+ /* fail cases, leniency checking
182
+ /**********************************************************
183
+ */
184
+
185
+ // [modules-java#94]: "Z" offset MAY be allowed, requires leniency
171
186
@ Test
172
- public void testBadDeserializationOfTimeWithTimeZone () throws Exception
187
+ public void testAllowZuluIfLenient () throws Exception
188
+ {
189
+ final LocalDateTime EXP = LocalDateTime .of (2020 , Month .OCTOBER , 22 , 4 , 16 , 20 , 504000000 );
190
+ final String input = q ("2020-10-22T04:16:20.504Z" );
191
+ final ObjectReader r = MAPPER .readerFor (LocalDateTime .class );
192
+
193
+ // First, defaults:
194
+ assertEquals ("The value is not correct." , EXP , r .readValue (input ));
195
+
196
+ // but ensure that global timezone setting doesn't matter
197
+ LocalDateTime value = r .with (TimeZone .getTimeZone (Z_CHICAGO ))
198
+ .readValue (input );
199
+ assertEquals ("The value is not correct." , EXP , value );
200
+
201
+ value = r .with (TimeZone .getTimeZone (Z_BUDAPEST ))
202
+ .readValue (input );
203
+ assertEquals ("The value is not correct." , EXP , value );
204
+ }
205
+
206
+ // [modules-java#94]: "Z" offset not allowed if strict mode
207
+ @ Test
208
+ public void testFailOnZuluIfStrict () throws Exception
173
209
{
174
210
try {
175
- MAPPER .readValue (q ("2020-10-22T00:16:20.504Z" ), LocalDateTime .class );
176
- fail ("expected fail " );
211
+ STRICT_MAPPER .readValue (q ("2020-10-22T00:16:20.504Z" ), LocalDateTime .class );
212
+ fail ("Should not pass " );
177
213
} catch (InvalidFormatException e ) {
178
- verifyException (e , "Invalid value" );
179
- verifyException (e , "for `java.time.LocalDateTime` " );
214
+ verifyException (e , "Cannot deserialize value of type " );
215
+ verifyException (e , "Should not contain offset when 'strict' mode " );
180
216
}
181
217
}
182
218
@@ -192,10 +228,9 @@ public void testBadDeserializationAsString01() throws Throwable
192
228
}
193
229
}
194
230
195
- /*
231
+ /*
196
232
/**********************************************************
197
233
/* Tests for empty string handling
198
- */
199
234
/**********************************************************
200
235
*/
201
236
@@ -224,20 +259,17 @@ public void testLenientDeserializeFromEmptyString() throws Exception {
224
259
public void testStrictDeserializeFromEmptyString () throws Exception {
225
260
226
261
final String key = "datetime" ;
227
- final ObjectMapper mapper = mapperBuilder ().build ();
228
- mapper .configOverride (LocalDateTime .class )
229
- .setFormat (JsonFormat .Value .forLeniency (false ));
230
- final ObjectReader objectReader = mapper .readerFor (MAP_TYPE_REF );
262
+ final ObjectReader objectReader = STRICT_MAPPER .readerFor (MAP_TYPE_REF );
231
263
final String dateValAsNullStr = null ;
232
264
233
265
// even with strict, null value should be deserialized without throwing an exception
234
- String valueFromNullStr = mapper .writeValueAsString (asMap (key , dateValAsNullStr ));
266
+ String valueFromNullStr = STRICT_MAPPER .writeValueAsString (asMap (key , dateValAsNullStr ));
235
267
Map <String , LocalDateTime > actualMapFromNullStr = objectReader .readValue (valueFromNullStr );
236
268
assertNull (actualMapFromNullStr .get (key ));
237
269
238
270
String dateValAsEmptyStr = "" ;
239
271
// TODO: nothing stops us from writing an empty string, maybe there should be a check there too?
240
- String valueFromEmptyStr = mapper .writeValueAsString (asMap ("date" , dateValAsEmptyStr ));
272
+ String valueFromEmptyStr = STRICT_MAPPER .writeValueAsString (asMap ("date" , dateValAsEmptyStr ));
241
273
// with strict, deserializing an empty string is not permitted
242
274
objectReader .readValue (valueFromEmptyStr );
243
275
}
0 commit comments