1
1
package com .fasterxml .jackson .datatype .jsr310 .key ;
2
2
3
- import java .time .Year ;
4
- import java .util .Map ;
5
-
6
3
import com .fasterxml .jackson .core .type .TypeReference ;
7
4
import com .fasterxml .jackson .databind .ObjectMapper ;
8
5
import com .fasterxml .jackson .databind .ObjectReader ;
6
+ import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
9
7
import com .fasterxml .jackson .datatype .jsr310 .ModuleTestBase ;
10
-
11
- import org .junit .Assert ;
12
8
import org .junit .Test ;
13
9
10
+ import java .time .Year ;
11
+ import java .util .Collections ;
12
+ import java .util .Map ;
13
+
14
+ import static org .junit .Assert .assertEquals ;
15
+
14
16
public class YearAsKeyTest extends ModuleTestBase
15
17
{
16
18
private static final TypeReference <Map <Year , String >> TYPE_REF = new TypeReference <Map <Year , String >>() {
@@ -20,13 +22,34 @@ public class YearAsKeyTest extends ModuleTestBase
20
22
21
23
@ Test
22
24
public void testSerialization () throws Exception {
23
- Assert . assertEquals ("Value is incorrect" , mapAsString ("3141" , "test" ),
25
+ assertEquals ("Value is incorrect" , mapAsString ("3141" , "test" ),
24
26
MAPPER .writeValueAsString (asMap (Year .of (3141 ), "test" )));
25
27
}
26
28
27
29
@ Test
28
30
public void testDeserialization () throws Exception {
29
- Assert . assertEquals ("Value is incorrect" , asMap (Year .of (3141 ), "test" ),
31
+ assertEquals ("Value is incorrect" , asMap (Year .of (3141 ), "test" ),
30
32
READER .readValue (mapAsString ("3141" , "test" )));
31
33
}
34
+
35
+ @ Test (expected = InvalidFormatException .class )
36
+ public void deserializeYearKey_notANumber () throws Exception {
37
+ READER .readValue (mapAsString ("10000BC" , "test" ));
38
+ }
39
+
40
+ @ Test (expected = InvalidFormatException .class )
41
+ public void deserializeYearKey_notAYear () throws Exception {
42
+ READER .readValue (mapAsString (Integer .toString (Year .MAX_VALUE +1 ), "test" ));
43
+ }
44
+
45
+ @ Test
46
+ public void serializeAndDeserializeYearKeyUnpadded () throws Exception {
47
+ // fix for issue #51 verify we can deserialize an unpadded year e.g. "1"
48
+ ObjectMapper unpaddedMapper = newMapper ();
49
+ Map <Year , Float > testMap = Collections .singletonMap (Year .of (1 ), 1F );
50
+ String serialized = unpaddedMapper .writeValueAsString (testMap );
51
+ TypeReference <Map <Year , Float >> yearFloatTypeReference = new TypeReference <Map <Year , Float >>() {};
52
+ Map <Year , Float > deserialized = unpaddedMapper .readValue (serialized , yearFloatTypeReference );
53
+ assertEquals (testMap , deserialized );
54
+ }
32
55
}
0 commit comments