@@ -21,15 +21,35 @@ public class YearAsKeyTest extends ModuleTestBase
21
21
private final ObjectReader READER = MAPPER .readerFor (TYPE_REF );
22
22
23
23
@ Test
24
- public void testSerialization () throws Exception {
24
+ public void testKeySerialization () throws Exception {
25
25
assertEquals ("Value is incorrect" , mapAsString ("3141" , "test" ),
26
26
MAPPER .writeValueAsString (asMap (Year .of (3141 ), "test" )));
27
27
}
28
28
29
29
@ Test
30
- public void testDeserialization () throws Exception {
30
+ public void testKeyDeserialization () throws Exception {
31
31
assertEquals ("Value is incorrect" , asMap (Year .of (3141 ), "test" ),
32
32
READER .readValue (mapAsString ("3141" , "test" )));
33
+ // Test both padded, unpadded
34
+ assertEquals ("Value is incorrect" , asMap (Year .of (476 ), "test" ),
35
+ READER .readValue (mapAsString ("0476" , "test" )));
36
+ assertEquals ("Value is incorrect" , asMap (Year .of (476 ), "test" ),
37
+ READER .readValue (mapAsString ("476" , "test" )));
38
+ }
39
+
40
+ @ Test
41
+ public void serializeAndDeserializeYearKeyUnpadded () throws Exception {
42
+ // fix for issue #51 verify we can deserialize an unpadded year e.g. "1"
43
+ Map <Year , Float > testMap = Collections .singletonMap (Year .of (1 ), 1F );
44
+ String serialized = MAPPER .writeValueAsString (testMap );
45
+ TypeReference <Map <Year , Float >> yearFloatTypeReference = new TypeReference <Map <Year , Float >>() {};
46
+ Map <Year , Float > deserialized = MAPPER .readValue (serialized , yearFloatTypeReference );
47
+ assertEquals (testMap , deserialized );
48
+
49
+ // actually, check padded as well just to make sure
50
+ Map <Year , Float > deserialized2 = MAPPER .readValue (aposToQuotes ("{'0001':1.0}" ),
51
+ yearFloatTypeReference );
52
+ assertEquals (testMap , deserialized2 );
33
53
}
34
54
35
55
@ Test (expected = InvalidFormatException .class )
@@ -41,15 +61,4 @@ public void deserializeYearKey_notANumber() throws Exception {
41
61
public void deserializeYearKey_notAYear () throws Exception {
42
62
READER .readValue (mapAsString (Integer .toString (Year .MAX_VALUE +1 ), "test" ));
43
63
}
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
- }
55
64
}
0 commit comments