6
6
7
7
import com .fasterxml .jackson .databind .BaseMapTest ;
8
8
import com .fasterxml .jackson .databind .ObjectMapper ;
9
+ import com .fasterxml .jackson .databind .testutil .NoCheckSubTypeValidator ;
9
10
10
11
/**
11
12
* Testing to verify that {@link JsonTypeInfo} works
@@ -81,36 +82,37 @@ static class OtherBean {
81
82
/**********************************************************
82
83
*/
83
84
85
+ final ObjectMapper MAPPER = jsonMapperBuilder ()
86
+ .polymorphicTypeValidator (new NoCheckSubTypeValidator ())
87
+ .build ();
88
+
84
89
public void testSimpleField () throws Exception
85
90
{
86
- ObjectMapper mapper = new ObjectMapper ();
87
- String json = mapper .writeValueAsString (new FieldWrapperBean (new StringWrapper ("foo" )));
91
+ String json = MAPPER .writeValueAsString (new FieldWrapperBean (new StringWrapper ("foo" )));
88
92
//System.out.println("JSON/field+object == "+json);
89
- FieldWrapperBean bean = mapper .readValue (json , FieldWrapperBean .class );
93
+ FieldWrapperBean bean = MAPPER .readValue (json , FieldWrapperBean .class );
90
94
assertNotNull (bean .value );
91
95
assertEquals (StringWrapper .class , bean .value .getClass ());
92
96
assertEquals (((StringWrapper ) bean .value ).str , "foo" );
93
97
}
94
98
95
99
public void testSimpleMethod () throws Exception
96
100
{
97
- ObjectMapper mapper = new ObjectMapper ();
98
- String json = mapper .writeValueAsString (new FieldWrapperBean (new IntWrapper (37 )));
101
+ String json = MAPPER .writeValueAsString (new FieldWrapperBean (new IntWrapper (37 )));
99
102
//System.out.println("JSON/method+object == "+json);
100
- FieldWrapperBean bean = mapper .readValue (json , FieldWrapperBean .class );
103
+ FieldWrapperBean bean = MAPPER .readValue (json , FieldWrapperBean .class );
101
104
assertNotNull (bean .value );
102
105
assertEquals (IntWrapper .class , bean .value .getClass ());
103
106
assertEquals (((IntWrapper ) bean .value ).i , 37 );
104
107
}
105
108
106
109
public void testSimpleListField () throws Exception
107
110
{
108
- ObjectMapper mapper = new ObjectMapper ();
109
111
FieldWrapperBeanList list = new FieldWrapperBeanList ();
110
112
list .add (new FieldWrapperBean (new OtherBean ()));
111
- String json = mapper .writeValueAsString (list );
113
+ String json = MAPPER .writeValueAsString (list );
112
114
//System.out.println("JSON/field+list == "+json);
113
- FieldWrapperBeanList result = mapper .readValue (json , FieldWrapperBeanList .class );
115
+ FieldWrapperBeanList result = MAPPER .readValue (json , FieldWrapperBeanList .class );
114
116
assertNotNull (result );
115
117
assertEquals (1 , result .size ());
116
118
FieldWrapperBean bean = list .get (0 );
@@ -121,13 +123,12 @@ public void testSimpleListField() throws Exception
121
123
122
124
public void testSimpleListMethod () throws Exception
123
125
{
124
- ObjectMapper mapper = new ObjectMapper ();
125
126
MethodWrapperBeanList list = new MethodWrapperBeanList ();
126
127
list .add (new MethodWrapperBean (new BooleanValue (true )));
127
128
list .add (new MethodWrapperBean (new StringWrapper ("x" )));
128
129
list .add (new MethodWrapperBean (new OtherBean ()));
129
- String json = mapper .writeValueAsString (list );
130
- MethodWrapperBeanList result = mapper .readValue (json , MethodWrapperBeanList .class );
130
+ String json = MAPPER .writeValueAsString (list );
131
+ MethodWrapperBeanList result = MAPPER .readValue (json , MethodWrapperBeanList .class );
131
132
assertNotNull (result );
132
133
assertEquals (3 , result .size ());
133
134
MethodWrapperBean bean = result .get (0 );
@@ -142,11 +143,10 @@ public void testSimpleListMethod() throws Exception
142
143
143
144
public void testSimpleArrayField () throws Exception
144
145
{
145
- ObjectMapper mapper = new ObjectMapper ();
146
146
FieldWrapperBeanArray array = new FieldWrapperBeanArray (new
147
147
FieldWrapperBean [] { new FieldWrapperBean (new BooleanValue (true )) });
148
- String json = mapper .writeValueAsString (array );
149
- FieldWrapperBeanArray result = mapper .readValue (json , FieldWrapperBeanArray .class );
148
+ String json = MAPPER .writeValueAsString (array );
149
+ FieldWrapperBeanArray result = MAPPER .readValue (json , FieldWrapperBeanArray .class );
150
150
assertNotNull (result );
151
151
FieldWrapperBean [] beans = result .beans ;
152
152
assertEquals (1 , beans .length );
@@ -157,11 +157,10 @@ public void testSimpleArrayField() throws Exception
157
157
158
158
public void testSimpleArrayMethod () throws Exception
159
159
{
160
- ObjectMapper mapper = new ObjectMapper ();
161
160
MethodWrapperBeanArray array = new MethodWrapperBeanArray (new
162
161
MethodWrapperBean [] { new MethodWrapperBean (new StringWrapper ("A" )) });
163
- String json = mapper .writeValueAsString (array );
164
- MethodWrapperBeanArray result = mapper .readValue (json , MethodWrapperBeanArray .class );
162
+ String json = MAPPER .writeValueAsString (array );
163
+ MethodWrapperBeanArray result = MAPPER .readValue (json , MethodWrapperBeanArray .class );
165
164
assertNotNull (result );
166
165
MethodWrapperBean [] beans = result .beans ;
167
166
assertEquals (1 , beans .length );
@@ -172,11 +171,10 @@ public void testSimpleArrayMethod() throws Exception
172
171
173
172
public void testSimpleMapField () throws Exception
174
173
{
175
- ObjectMapper mapper = new ObjectMapper ();
176
174
FieldWrapperBeanMap map = new FieldWrapperBeanMap ();
177
175
map .put ("foop" , new FieldWrapperBean (new IntWrapper (13 )));
178
- String json = mapper .writeValueAsString (map );
179
- FieldWrapperBeanMap result = mapper .readValue (json , FieldWrapperBeanMap .class );
176
+ String json = MAPPER .writeValueAsString (map );
177
+ FieldWrapperBeanMap result = MAPPER .readValue (json , FieldWrapperBeanMap .class );
180
178
assertNotNull (result );
181
179
assertEquals (1 , result .size ());
182
180
FieldWrapperBean bean = result .get ("foop" );
@@ -188,11 +186,10 @@ public void testSimpleMapField() throws Exception
188
186
189
187
public void testSimpleMapMethod () throws Exception
190
188
{
191
- ObjectMapper mapper = new ObjectMapper ();
192
189
MethodWrapperBeanMap map = new MethodWrapperBeanMap ();
193
190
map .put ("xyz" , new MethodWrapperBean (new BooleanValue (true )));
194
- String json = mapper .writeValueAsString (map );
195
- MethodWrapperBeanMap result = mapper .readValue (json , MethodWrapperBeanMap .class );
191
+ String json = MAPPER .writeValueAsString (map );
192
+ MethodWrapperBeanMap result = MAPPER .readValue (json , MethodWrapperBeanMap .class );
196
193
assertNotNull (result );
197
194
assertEquals (1 , result .size ());
198
195
MethodWrapperBean bean = result .get ("xyz" );
0 commit comments