7
7
8
8
import com .fasterxml .jackson .databind .ObjectMapper ;
9
9
import com .fasterxml .jackson .databind .exc .ValueInstantiationException ;
10
+ import com .fasterxml .jackson .databind .testutil .DatabindTestUtil ;
10
11
11
12
import static org .junit .jupiter .api .Assertions .assertEquals ;
12
13
import static org .junit .jupiter .api .Assertions .assertNull ;
15
16
16
17
// [databind#2978]
17
18
public class StdValueInstantiatorTest
19
+ extends DatabindTestUtil
18
20
{
21
+ private final ObjectMapper MAPPER = newJsonMapper ();
22
+
19
23
private static final long LONG_TEST_VALUE = 12345678901L ;
24
+
25
+ static class Stuff {
26
+ final double value ;
27
+
28
+ Stuff (double value ) {
29
+ this .value = value ;
30
+ }
31
+ }
20
32
21
33
@ Test
22
34
public void testDoubleValidation_valid () {
@@ -34,31 +46,20 @@ public void testDoubleValidation_invalid() {
34
46
35
47
@ Test
36
48
public void testJsonIntegerToDouble () throws Exception {
37
- ObjectMapper m = new ObjectMapper ();
38
- Stuff a = m .readValue ("5" , Stuff .class );
49
+ Stuff a = MAPPER .readValue ("5" , Stuff .class );
39
50
assertEquals (5 , a .value );
40
51
}
41
52
42
53
@ Test
43
54
public void testJsonLongToDouble () throws Exception {
44
- ObjectMapper m = new ObjectMapper ();
45
55
assertTrue (LONG_TEST_VALUE > Integer .MAX_VALUE );
46
- Stuff a = m .readValue (String .valueOf (LONG_TEST_VALUE ), Stuff .class );
56
+ Stuff a = MAPPER .readValue (String .valueOf (LONG_TEST_VALUE ), Stuff .class );
47
57
assertEquals (LONG_TEST_VALUE , a .value );
48
58
}
49
59
50
- static class Stuff {
51
- final double value ;
52
-
53
- Stuff (double value ) {
54
- this .value = value ;
55
- }
56
- }
57
-
58
60
@ Test
59
61
public void testJsonIntegerDeserializationPrefersInt () throws Exception {
60
- ObjectMapper m = new ObjectMapper ();
61
- A a = m .readValue ("5" , A .class );
62
+ A a = MAPPER .readValue ("5" , A .class );
62
63
assertEquals (1 , a .creatorType );
63
64
}
64
65
@@ -84,8 +85,7 @@ static class A {
84
85
85
86
@ Test
86
87
public void testJsonIntegerDeserializationPrefersLong () throws Exception {
87
- ObjectMapper m = new ObjectMapper ();
88
- B a = m .readValue ("5" , B .class );
88
+ B a = MAPPER .readValue ("5" , B .class );
89
89
assertEquals (2 , a .creatorType );
90
90
}
91
91
@@ -107,8 +107,7 @@ static class B {
107
107
108
108
@ Test
109
109
public void testJsonIntegerDeserializationPrefersBigInteger () throws Exception {
110
- ObjectMapper m = new ObjectMapper ();
111
- C a = m .readValue ("5" , C .class );
110
+ C a = MAPPER .readValue ("5" , C .class );
112
111
assertEquals (3 , a .creatorType );
113
112
}
114
113
@@ -126,8 +125,7 @@ static class C {
126
125
127
126
@ Test
128
127
public void testJsonLongDeserializationPrefersLong () throws Exception {
129
- ObjectMapper m = new ObjectMapper ();
130
- A2 a = m .readValue (String .valueOf (LONG_TEST_VALUE ), A2 .class );
128
+ A2 a = MAPPER .readValue (String .valueOf (LONG_TEST_VALUE ), A2 .class );
131
129
assertEquals (2 , a .creatorType );
132
130
}
133
131
@@ -153,8 +151,7 @@ static class A2 {
153
151
154
152
@ Test
155
153
public void testJsonLongDeserializationPrefersBigInteger () throws Exception {
156
- ObjectMapper m = new ObjectMapper ();
157
- B2 a = m .readValue (String .valueOf (LONG_TEST_VALUE ), B2 .class );
154
+ B2 a = MAPPER .readValue (String .valueOf (LONG_TEST_VALUE ), B2 .class );
158
155
assertEquals (3 , a .creatorType );
159
156
}
160
157
@@ -172,10 +169,9 @@ static class B2 {
172
169
173
170
@ Test
174
171
public void testJsonIntegerIntoDoubleConstructorThrows () throws Exception {
175
- ObjectMapper m = new ObjectMapper ();
176
172
try {
177
- m .readValue ("5" , D .class );
178
- fail ();
173
+ MAPPER .readValue ("5" , D .class );
174
+ fail ("Should not pass" );
179
175
} catch (ValueInstantiationException e ) {
180
176
assertTrue (e .getCause () instanceof IllegalArgumentException );
181
177
assertEquals ("boo" , e .getCause ().getMessage ());
@@ -191,14 +187,12 @@ static final class D {
191
187
192
188
@ Test
193
189
public void testJsonLongIntoDoubleConstructorThrows () throws Exception {
194
- ObjectMapper m = new ObjectMapper ();
195
190
try {
196
- m .readValue (String .valueOf (LONG_TEST_VALUE ), D .class );
197
- fail ();
191
+ MAPPER .readValue (String .valueOf (LONG_TEST_VALUE ), D .class );
192
+ fail ("Should not pass" );
198
193
} catch (ValueInstantiationException e ) {
199
194
assertTrue (e .getCause () instanceof IllegalArgumentException );
200
195
assertEquals ("boo" , e .getCause ().getMessage ());
201
196
}
202
197
}
203
-
204
198
}
0 commit comments