1
1
package com .fasterxml .jackson .databind .deser .std ;
2
2
3
3
import java .math .BigDecimal ;
4
+ import java .math .BigInteger ;
4
5
5
6
import org .junit .jupiter .api .Test ;
6
7
8
+ import com .fasterxml .jackson .databind .ObjectMapper ;
9
+ import com .fasterxml .jackson .databind .exc .ValueInstantiationException ;
10
+
7
11
import static org .junit .jupiter .api .Assertions .assertEquals ;
8
12
import static org .junit .jupiter .api .Assertions .assertNull ;
13
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
14
+ import static org .junit .jupiter .api .Assertions .fail ;
9
15
10
16
// [databind#2978]
11
17
public class StdValueInstantiatorTest
12
18
{
19
+ private static final long LONG_TEST_VALUE = 12345678901L ;
20
+
13
21
@ Test
14
22
public void testDoubleValidation_valid () {
15
23
assertEquals (0d , StdValueInstantiator .tryConvertToDouble (BigDecimal .ZERO ));
@@ -23,4 +31,174 @@ public void testDoubleValidation_invalid() {
23
31
BigDecimal value = BigDecimal .valueOf (Double .MAX_VALUE ).add (BigDecimal .valueOf (Double .MAX_VALUE ));
24
32
assertNull (StdValueInstantiator .tryConvertToDouble (value ));
25
33
}
34
+
35
+ @ Test
36
+ public void testJsonIntegerToDouble () throws Exception {
37
+ ObjectMapper m = new ObjectMapper ();
38
+ Stuff a = m .readValue ("5" , Stuff .class );
39
+ assertEquals (5 , a .value );
40
+ }
41
+
42
+ @ Test
43
+ public void testJsonLongToDouble () throws Exception {
44
+ ObjectMapper m = new ObjectMapper ();
45
+ assertTrue (LONG_TEST_VALUE > Integer .MAX_VALUE );
46
+ Stuff a = m .readValue (String .valueOf (LONG_TEST_VALUE ), Stuff .class );
47
+ assertEquals (LONG_TEST_VALUE , a .value );
48
+ }
49
+
50
+ static class Stuff {
51
+ final double value ;
52
+
53
+ Stuff (double value ) {
54
+ this .value = value ;
55
+ }
56
+ }
57
+
58
+ @ Test
59
+ public void testJsonIntegerDeserializationPrefersInt () throws Exception {
60
+ ObjectMapper m = new ObjectMapper ();
61
+ A a = m .readValue ("5" , A .class );
62
+ assertEquals (1 , a .creatorType );
63
+ }
64
+
65
+ static class A {
66
+ final int creatorType ;
67
+
68
+ A (int value ) {
69
+ this .creatorType = 1 ;
70
+ }
71
+
72
+ A (long value ) {
73
+ this .creatorType = 2 ;
74
+ }
75
+
76
+ A (BigInteger value ) {
77
+ this .creatorType = 3 ;
78
+ }
79
+
80
+ A (double value ) {
81
+ this .creatorType = 4 ;
82
+ }
83
+ }
84
+
85
+ @ Test
86
+ public void testJsonIntegerDeserializationPrefersLong () throws Exception {
87
+ ObjectMapper m = new ObjectMapper ();
88
+ B a = m .readValue ("5" , B .class );
89
+ assertEquals (2 , a .creatorType );
90
+ }
91
+
92
+ static class B {
93
+ final int creatorType ;
94
+
95
+ B (long value ) {
96
+ this .creatorType = 2 ;
97
+ }
98
+
99
+ B (BigInteger value ) {
100
+ this .creatorType = 3 ;
101
+ }
102
+
103
+ B (double value ) {
104
+ this .creatorType = 4 ;
105
+ }
106
+ }
107
+
108
+ @ Test
109
+ public void testJsonIntegerDeserializationPrefersBigInteger () throws Exception {
110
+ ObjectMapper m = new ObjectMapper ();
111
+ C a = m .readValue ("5" , C .class );
112
+ assertEquals (3 , a .creatorType );
113
+ }
114
+
115
+ static class C {
116
+ final int creatorType ;
117
+
118
+ C (BigInteger value ) {
119
+ this .creatorType = 3 ;
120
+ }
121
+
122
+ C (double value ) {
123
+ this .creatorType = 4 ;
124
+ }
125
+ }
126
+
127
+ @ Test
128
+ public void testJsonLongDeserializationPrefersLong () throws Exception {
129
+ ObjectMapper m = new ObjectMapper ();
130
+ A2 a = m .readValue (String .valueOf (LONG_TEST_VALUE ), A2 .class );
131
+ assertEquals (2 , a .creatorType );
132
+ }
133
+
134
+ static class A2 {
135
+ final int creatorType ;
136
+
137
+ A2 (int value ) {
138
+ this .creatorType = 1 ;
139
+ }
140
+
141
+ A2 (long value ) {
142
+ this .creatorType = 2 ;
143
+ }
144
+
145
+ A2 (BigInteger value ) {
146
+ this .creatorType = 3 ;
147
+ }
148
+
149
+ A2 (double value ) {
150
+ this .creatorType = 4 ;
151
+ }
152
+ }
153
+
154
+ @ Test
155
+ public void testJsonLongDeserializationPrefersBigInteger () throws Exception {
156
+ ObjectMapper m = new ObjectMapper ();
157
+ B2 a = m .readValue (String .valueOf (LONG_TEST_VALUE ), B2 .class );
158
+ assertEquals (3 , a .creatorType );
159
+ }
160
+
161
+ static class B2 {
162
+ final int creatorType ;
163
+
164
+ B2 (BigInteger value ) {
165
+ this .creatorType = 3 ;
166
+ }
167
+
168
+ B2 (double value ) {
169
+ this .creatorType = 4 ;
170
+ }
171
+ }
172
+
173
+ @ Test
174
+ public void testJsonIntegerIntoDoubleConstructorThrows () throws Exception {
175
+ ObjectMapper m = new ObjectMapper ();
176
+ try {
177
+ m .readValue ("5" , D .class );
178
+ fail ();
179
+ } catch (ValueInstantiationException e ) {
180
+ assertTrue (e .getCause () instanceof IllegalArgumentException );
181
+ assertEquals ("boo" , e .getCause ().getMessage ());
182
+ }
183
+ }
184
+
185
+ static final class D {
186
+
187
+ D (double value ) {
188
+ throw new IllegalArgumentException ("boo" );
189
+ }
190
+ }
191
+
192
+ @ Test
193
+ public void testJsonLongIntoDoubleConstructorThrows () throws Exception {
194
+ ObjectMapper m = new ObjectMapper ();
195
+ try {
196
+ m .readValue (String .valueOf (LONG_TEST_VALUE ), D .class );
197
+ fail ();
198
+ } catch (ValueInstantiationException e ) {
199
+ assertTrue (e .getCause () instanceof IllegalArgumentException );
200
+ assertEquals ("boo" , e .getCause ().getMessage ());
201
+ }
202
+ }
203
+
26
204
}
0 commit comments