1
1
package com .fasterxml .jackson .datatype .jsr310 .deser ;
2
2
3
- import static org .junit .Assert .assertEquals ;
4
- import static org .junit .Assert .assertNotNull ;
5
- import static org .junit .Assert .assertNull ;
6
- import static org .junit .Assert .fail ;
7
-
8
3
import java .io .IOException ;
9
4
import java .time .Instant ;
10
5
import java .time .LocalDate ;
16
11
import java .util .Map ;
17
12
18
13
import com .fasterxml .jackson .annotation .OptBoolean ;
14
+ import com .fasterxml .jackson .databind .cfg .CoercionAction ;
15
+ import com .fasterxml .jackson .databind .cfg .CoercionInputShape ;
19
16
import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
20
17
import org .junit .Test ;
21
18
33
30
import com .fasterxml .jackson .datatype .jsr310 .MockObjectConfiguration ;
34
31
import com .fasterxml .jackson .datatype .jsr310 .ModuleTestBase ;
35
32
33
+ import static org .hamcrest .CoreMatchers .containsString ;
34
+ import static org .hamcrest .MatcherAssert .assertThat ;
35
+ import static org .junit .Assert .*;
36
+ import static org .junit .Assert .assertThrows ;
37
+
36
38
public class LocalDateDeserTest extends ModuleTestBase
37
39
{
38
40
private final ObjectMapper MAPPER = newMapper ();
@@ -476,7 +478,7 @@ public void testLenientDeserializeFromNumberInt() throws Exception {
476
478
mapper .configOverride (LocalDate .class )
477
479
.setFormat (JsonFormat .Value .forShape (JsonFormat .Shape .NUMBER_INT ));
478
480
479
- assertEquals ("The value is not correct." , LocalDate .of (1970 , Month .MAY , 04 ),
481
+ assertEquals ("The value is not correct." , LocalDate .of (1970 , Month .MAY , 4 ),
480
482
mapper .readValue ("123" , LocalDate .class ));
481
483
}
482
484
@@ -490,7 +492,7 @@ public void testStrictDeserializeFromNumberInt() throws Exception
490
492
ShapeWrapper w = mapper .readValue ("{\" date\" :123}" , ShapeWrapper .class );
491
493
LocalDate localDate = w .date ;
492
494
493
- assertEquals ("The value is not correct." , LocalDate .of (1970 , Month .MAY , 04 ),
495
+ assertEquals ("The value is not correct." , LocalDate .of (1970 , Month .MAY , 4 ),
494
496
localDate );
495
497
}
496
498
@@ -504,6 +506,38 @@ public void testStrictDeserializeFromString() throws Exception
504
506
mapper .readValue ("{\" value\" :123}" , Wrapper .class );
505
507
}
506
508
509
+ /**********************************************************************
510
+ *
511
+ * coercion config test
512
+ *
513
+ /**********************************************************************
514
+ */
515
+ @ Test
516
+ public void testDeserializeFromIntegerWithCoercionActionFail () {
517
+ ObjectMapper mapper = newMapper ();
518
+ mapper .coercionConfigFor (LocalDate .class )
519
+ .setCoercion (CoercionInputShape .Integer , CoercionAction .Fail );
520
+
521
+ MismatchedInputException exception = assertThrows (MismatchedInputException .class ,
522
+ () -> mapper .readValue ("123" , LocalDate .class ));
523
+
524
+ assertThat (exception .getMessage (),
525
+ containsString ("Cannot coerce Integer value (123) to `java.time.LocalDate`" ));
526
+ }
527
+
528
+ @ Test
529
+ public void testDeserializeFromEmptyStringWithCoercionActionFail () {
530
+ ObjectMapper mapper = newMapper ();
531
+ mapper .coercionConfigFor (LocalDate .class )
532
+ .setCoercion (CoercionInputShape .EmptyString , CoercionAction .Fail );
533
+
534
+ MismatchedInputException exception = assertThrows (MismatchedInputException .class ,
535
+ () -> mapper .readValue (a2q ("{'value':''}" ), Wrapper .class ));
536
+
537
+ assertThat (exception .getMessage (),
538
+ containsString ("Cannot coerce empty String (\" \" ) to `java.time.LocalDate`" ));
539
+ }
540
+
507
541
/*
508
542
/**********************************************************************
509
543
/* Helper methods
0 commit comments