4
4
import com .fasterxml .jackson .annotation .JsonAutoDetect .Visibility ;
5
5
import com .fasterxml .jackson .databind .*;
6
6
import com .fasterxml .jackson .databind .annotation .*;
7
+ import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
7
8
8
9
/**
9
10
* Unit tests for verifying that field-backed properties can also be
12
13
public class TestFieldDeserialization
13
14
extends BaseMapTest
14
15
{
15
- /*
16
- /**********************************************************
17
- /* Annotated helper classes
18
- /**********************************************************
19
- */
20
-
21
16
static class SimpleFieldBean
22
17
{
23
18
public int x , y ;
@@ -59,7 +54,7 @@ public static class DupFieldBean2
59
54
public int _z ;
60
55
61
56
@ JsonDeserialize
62
- private int foo ;
57
+ int foo ;
63
58
}
64
59
65
60
public static class OkDupFieldBean
@@ -91,19 +86,19 @@ static class AbstractWrapper {
91
86
/**********************************************************
92
87
*/
93
88
89
+ private final ObjectMapper MAPPER = newJsonMapper ();
90
+
94
91
public void testSimpleAutoDetect () throws Exception
95
92
{
96
- ObjectMapper m = new ObjectMapper ();
97
- SimpleFieldBean result = m .readValue ("{ \" x\" : -13 }" ,
98
- SimpleFieldBean .class );
93
+ SimpleFieldBean result = MAPPER .readValue ("{ \" x\" : -13 }" ,
94
+ SimpleFieldBean .class );
99
95
assertEquals (-13 , result .x );
100
96
assertEquals (0 , result .y );
101
97
}
102
98
103
99
public void testSimpleAnnotation () throws Exception
104
100
{
105
- ObjectMapper m = new ObjectMapper ();
106
- SimpleFieldBean2 bean = m .readValue ("{ \" values\" : [ \" x\" , \" y\" ] }" ,
101
+ SimpleFieldBean2 bean = MAPPER .readValue ("{ \" values\" : [ \" x\" , \" y\" ] }" ,
107
102
SimpleFieldBean2 .class );
108
103
String [] values = bean .values ;
109
104
assertNotNull (values );
@@ -114,46 +109,42 @@ public void testSimpleAnnotation() throws Exception
114
109
115
110
public void testNoAutoDetect () throws Exception
116
111
{
117
- ObjectMapper m = new ObjectMapper ();
118
- NoAutoDetectBean bean = m .readValue ("{ \" z\" : 7 }" ,
119
- NoAutoDetectBean .class );
112
+ NoAutoDetectBean bean = MAPPER .readValue ("{ \" z\" : 7 }" ,
113
+ NoAutoDetectBean .class );
120
114
assertEquals (7 , bean ._z );
121
115
}
122
116
123
117
public void testTypeAnnotation () throws Exception
124
118
{
125
- ObjectMapper m = new ObjectMapper ();
126
- AbstractWrapper w = m .readValue ("{ \" value\" : \" abc\" }" ,
127
- AbstractWrapper .class );
119
+ AbstractWrapper w = MAPPER .readValue ("{ \" value\" : \" abc\" }" ,
120
+ AbstractWrapper .class );
128
121
Abstract bean = w .value ;
129
122
assertNotNull (bean );
130
123
assertEquals (Concrete .class , bean .getClass ());
131
124
assertEquals ("abc" , ((Concrete )bean ).value );
132
125
}
133
126
134
- public void testFailureDueToDups () throws Exception
127
+ public void testResolvedDups1 () throws Exception
135
128
{
136
- try {
137
- writeAndMap (new ObjectMapper (), new DupFieldBean ());
138
- } catch (JsonMappingException e ) {
139
- verifyException (e , "Multiple fields representing property" );
140
- }
129
+ DupFieldBean result = MAPPER .readValue (a2q ("{'z':3}" ), DupFieldBean .class );
130
+ assertEquals (3 , result ._z );
131
+ assertEquals (0 , result .z );
141
132
}
142
133
143
- public void testFailureDueToDups2 () throws Exception
134
+ public void testFailingDups2 () throws Exception
144
135
{
136
+ // Fails because both fields have explicit annotation
145
137
try {
146
- writeAndMap (new ObjectMapper (), new DupFieldBean2 ());
147
- } catch (JsonMappingException e ) {
148
- verifyException (e , "Multiple fields representing property" );
138
+ DupFieldBean2 result = MAPPER .readValue (a2q ("{'foo':28}" ), DupFieldBean2 .class );
139
+ fail ("Should not pass but got: " +result );
140
+ } catch (InvalidDefinitionException e ) {
141
+ verifyException (e , "Multiple fields representing property \" foo\" " );
149
142
}
150
143
}
151
144
152
- // For [JACKSON-226], acceptable field overrides
153
145
public void testOkFieldOverride () throws Exception
154
146
{
155
- ObjectMapper m = new ObjectMapper ();
156
- OkDupFieldBean result = m .readValue ("{ \" x\" : 1, \" y\" : 2 }" ,
147
+ OkDupFieldBean result = MAPPER .readValue ("{ \" x\" : 1, \" y\" : 2 }" ,
157
148
OkDupFieldBean .class );
158
149
assertEquals (1 , result .myX );
159
150
assertEquals (2 , result .y );
0 commit comments