1
1
package com .fasterxml .jackson .datatype .jsr310 .deser ;
2
2
3
+ import java .io .IOException ;
4
+ import java .time .Month ;
5
+ import java .time .YearMonth ;
6
+ import java .time .format .DateTimeParseException ;
7
+ import java .util .Map ;
8
+
3
9
import com .fasterxml .jackson .annotation .JsonFormat ;
4
10
import com .fasterxml .jackson .core .JsonProcessingException ;
5
11
import com .fasterxml .jackson .core .type .TypeReference ;
6
12
import com .fasterxml .jackson .databind .DeserializationFeature ;
7
13
import com .fasterxml .jackson .databind .JsonMappingException ;
8
14
import com .fasterxml .jackson .databind .ObjectMapper ;
9
15
import com .fasterxml .jackson .databind .ObjectReader ;
16
+ import com .fasterxml .jackson .databind .SerializationFeature ;
10
17
import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
11
18
import com .fasterxml .jackson .datatype .jsr310 .ModuleTestBase ;
12
19
13
20
import org .junit .Test ;
14
21
15
- import java .io .IOException ;
16
- import java .time .Month ;
17
- import java .time .YearMonth ;
18
- import java .time .format .DateTimeParseException ;
19
- import java .util .Map ;
20
-
21
22
import static org .junit .Assert .assertEquals ;
22
23
import static org .junit .Assert .assertNotNull ;
23
24
import static org .junit .Assert .assertNull ;
24
25
import static org .junit .Assert .fail ;
25
26
26
27
public class YearMonthDeserTest extends ModuleTestBase
27
28
{
28
- private final ObjectReader READER = newMapper ().readerFor (YearMonth .class );
29
+ private final ObjectMapper MAPPER = newMapper ();
30
+ private final ObjectReader READER = MAPPER .readerFor (YearMonth .class );
29
31
private final TypeReference <Map <String , YearMonth >> MAP_TYPE_REF = new TypeReference <Map <String , YearMonth >>() { };
30
32
31
33
@ Test
@@ -35,52 +37,63 @@ public void testDeserializationAsString01() throws Exception
35
37
}
36
38
37
39
@ Test
38
- public void testBadDeserializationAsString01 () throws Throwable
40
+ public void testBadDeserializationAsString01 () throws Exception
39
41
{
40
42
expectFailure ("'notayearmonth'" );
41
43
}
42
44
43
45
@ Test
44
- public void testDeserializationAsArrayDisabled () throws Throwable
46
+ public void testDeserializationAsArrayDisabled () throws Exception
45
47
{
46
- try {
47
- read ("['2000-01']" );
48
- fail ("expected JsonMappingException" );
48
+ try {
49
+ read ("['2000-01']" );
50
+ fail ("expected JsonMappingException" );
49
51
} catch (JsonMappingException e ) {
50
52
// OK
51
53
} catch (IOException e ) {
52
54
throw e ;
53
55
}
54
-
55
56
}
56
57
57
58
@ Test
58
- public void testDeserializationAsEmptyArrayDisabled () throws Throwable
59
+ public void testDeserializationAsEmptyArrayDisabled () throws Exception
59
60
{
60
- // works even without the feature enabled
61
- assertNull (read ("[]" ));
61
+ // works even without the feature enabled
62
+ assertNull (read ("[]" ));
62
63
}
63
64
64
65
@ Test
65
- public void testDeserializationAsArrayEnabled () throws Throwable
66
+ public void testDeserializationAsArrayEnabled () throws Exception
66
67
{
67
- String json ="['2000-01']" ;
68
- YearMonth value = newMapper ()
69
- .configure (DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS , true )
70
- .readerFor (YearMonth .class ).readValue (a2q (json ));
71
- notNull (value );
68
+ String json ="['2000-01']" ;
69
+ YearMonth value = newMapper ()
70
+ .configure (DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS , true )
71
+ .readerFor (YearMonth .class ).readValue (a2q (json ));
72
+ notNull (value );
72
73
expect (YearMonth .of (2000 , Month .JANUARY ), value );
73
74
}
74
75
75
76
@ Test
76
- public void testDeserializationAsEmptyArrayEnabled () throws Throwable
77
+ public void testDeserializationAsEmptyArrayEnabled () throws Exception
78
+ {
79
+ String json ="[]" ;
80
+ YearMonth value = newMapper ()
81
+ .configure (DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS , true )
82
+ .configure (DeserializationFeature .ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT , true )
83
+ .readerFor (YearMonth .class ).readValue (a2q (json ));
84
+ assertNull (value );
85
+ }
86
+
87
+ // [modules-java8#249
88
+ @ Test
89
+ public void testYearAbove10k () throws Exception
77
90
{
78
- String json = "[]" ;
79
- YearMonth value = newMapper ()
80
- . configure ( DeserializationFeature . UNWRAP_SINGLE_VALUE_ARRAYS , true )
81
- . configure ( DeserializationFeature . ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT , true )
82
- . readerFor ( YearMonth . class ). readValue (a2q ( json ) );
83
- assertNull ( value );
91
+ YearMonth input = YearMonth . of ( 10000 , 1 ) ;
92
+ String json = MAPPER . writer ()
93
+ . without ( SerializationFeature . WRITE_DATES_AS_TIMESTAMPS )
94
+ . writeValueAsString ( input );
95
+ YearMonth result = READER . readValue (json );
96
+ expect ( input , result );
84
97
}
85
98
86
99
/*
@@ -126,7 +139,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
126
139
objectReader .readValue (valueFromEmptyStr );
127
140
}
128
141
129
- private void expectFailure (String json ) throws Throwable {
142
+ private void expectFailure (String json ) throws Exception {
130
143
try {
131
144
read (json );
132
145
fail ("expected DateTimeParseException" );
@@ -135,20 +148,20 @@ private void expectFailure(String json) throws Throwable {
135
148
throw e ;
136
149
}
137
150
if (!(e .getCause () instanceof DateTimeParseException )) {
138
- throw e .getCause ();
151
+ throw ( Exception ) e .getCause ();
139
152
}
140
153
} catch (IOException e ) {
141
154
throw e ;
142
155
}
143
156
}
144
157
145
- private void expectSuccess (Object exp , String json ) throws IOException {
158
+ private void expectSuccess (Object exp , String json ) throws Exception {
146
159
final YearMonth value = read (json );
147
160
notNull (value );
148
161
expect (exp , value );
149
162
}
150
163
151
- private YearMonth read (final String json ) throws IOException {
164
+ private YearMonth read (final String json ) throws Exception {
152
165
return READER .readValue (a2q (json ));
153
166
}
154
167
0 commit comments