@@ -60,33 +60,35 @@ static class Box {
60
60
/**********************************************************
61
61
*/
62
62
63
- private static final String deadCatJson = aposToQuotes ("{'name':'Felix','causeOfDeath':'entropy'}" );
64
- private static final String liveCatJson = aposToQuotes ("{'name':'Felix','angry':true}" );
65
- private static final String luckyCatJson = aposToQuotes ("{'name':'Felix','angry':true,'lives':8}" );
66
- private static final String ambiguousCatJson = aposToQuotes ("{'name':'Felix','age':2}" );
67
- private static final String fleabagJson = aposToQuotes ("{}" );
68
- private static final String box1Json = aposToQuotes ("{'feline':" + liveCatJson + "}" );
69
- private static final String box2Json = aposToQuotes ("{'feline':" + deadCatJson + "}" );
70
- private static final String box3Json = aposToQuotes ("{'feline':" + fleabagJson + "}" );
71
- private static final String box4Json = aposToQuotes ("{'feline':null}" );
72
- private static final String box5Json = aposToQuotes ("{}" );
73
- private static final String arrayOfCatsJson = aposToQuotes ("[" + liveCatJson + "," + deadCatJson + "]" );
74
- private static final String mapOfCatsJson = aposToQuotes ("{'live':" + liveCatJson + "}" );
63
+ private static final String deadCatJson = a2q ("{'name':'Felix','causeOfDeath':'entropy'}" );
64
+ private static final String liveCatJson = a2q ("{'name':'Felix','angry':true}" );
65
+ private static final String luckyCatJson = a2q ("{'name':'Felix','angry':true,'lives':8}" );
66
+ private static final String ambiguousCatJson = a2q ("{'name':'Felix','age':2}" );
67
+ private static final String fleabagJson = a2q ("{}" );
68
+ private static final String box1Json = a2q ("{'feline':" + liveCatJson + "}" );
69
+ private static final String box2Json = a2q ("{'feline':" + deadCatJson + "}" );
70
+ private static final String box3Json = a2q ("{'feline':" + fleabagJson + "}" );
71
+ private static final String box4Json = a2q ("{'feline':null}" );
72
+ private static final String box5Json = a2q ("{}" );
73
+ private static final String arrayOfCatsJson = a2q ("[" + liveCatJson + "," + deadCatJson + "]" );
74
+ private static final String mapOfCatsJson = a2q ("{'live':" + liveCatJson + "}" );
75
75
76
76
/*
77
77
/**********************************************************
78
78
/* Test methods
79
79
/**********************************************************
80
80
*/
81
81
82
+ private final ObjectMapper MAPPER = newJsonMapper ();
83
+
82
84
public void testSimpleInference () throws Exception {
83
- Cat cat = sharedMapper () .readValue (liveCatJson , Cat .class );
85
+ Cat cat = MAPPER .readValue (liveCatJson , Cat .class );
84
86
assertTrue (cat instanceof LiveCat );
85
87
assertSame (cat .getClass (), LiveCat .class );
86
88
assertEquals ("Felix" , cat .name );
87
89
assertTrue (((LiveCat )cat ).angry );
88
90
89
- cat = sharedMapper () .readValue (deadCatJson , Cat .class );
91
+ cat = MAPPER .readValue (deadCatJson , Cat .class );
90
92
assertTrue (cat instanceof DeadCat );
91
93
assertSame (cat .getClass (), DeadCat .class );
92
94
assertEquals ("Felix" , cat .name );
@@ -95,7 +97,7 @@ public void testSimpleInference() throws Exception {
95
97
96
98
public void testSimpleInferenceOfEmptySubtype () throws Exception {
97
99
// Given:
98
- ObjectMapper mapper = sharedMapper () ;
100
+ ObjectMapper mapper = MAPPER ;
99
101
// When:
100
102
Feline feline = mapper .readValue (fleabagJson , Feline .class );
101
103
// Then:
@@ -104,7 +106,7 @@ public void testSimpleInferenceOfEmptySubtype() throws Exception {
104
106
105
107
public void testSimpleInferenceOfEmptySubtypeDoesntMatchNull () throws Exception {
106
108
// Given:
107
- ObjectMapper mapper = sharedMapper () ;
109
+ ObjectMapper mapper = MAPPER ;
108
110
// When:
109
111
Feline feline = mapper .readValue ("null" , Feline .class );
110
112
// Then:
@@ -136,52 +138,52 @@ public void testCaseInsensitiveInference() throws Exception {
136
138
// }
137
139
138
140
public void testContainedInference () throws Exception {
139
- Box box = sharedMapper () .readValue (box1Json , Box .class );
141
+ Box box = MAPPER .readValue (box1Json , Box .class );
140
142
assertTrue (box .feline instanceof LiveCat );
141
143
assertSame (box .feline .getClass (), LiveCat .class );
142
144
assertEquals ("Felix" , ((LiveCat )box .feline ).name );
143
145
assertTrue (((LiveCat )box .feline ).angry );
144
146
145
- box = sharedMapper () .readValue (box2Json , Box .class );
147
+ box = MAPPER .readValue (box2Json , Box .class );
146
148
assertTrue (box .feline instanceof DeadCat );
147
149
assertSame (box .feline .getClass (), DeadCat .class );
148
150
assertEquals ("Felix" , ((DeadCat )box .feline ).name );
149
151
assertEquals ("entropy" , ((DeadCat )box .feline ).causeOfDeath );
150
152
}
151
153
152
154
public void testContainedInferenceOfEmptySubtype () throws Exception {
153
- Box box = sharedMapper () .readValue (box3Json , Box .class );
155
+ Box box = MAPPER .readValue (box3Json , Box .class );
154
156
assertTrue (box .feline instanceof Fleabag );
155
157
156
- box = sharedMapper () .readValue (box4Json , Box .class );
158
+ box = MAPPER .readValue (box4Json , Box .class );
157
159
assertNull ("null != {}" , box .feline );
158
160
159
- box = sharedMapper () .readValue (box5Json , Box .class );
161
+ box = MAPPER .readValue (box5Json , Box .class );
160
162
assertNull ("<absent> != {}" , box .feline );
161
163
}
162
164
163
165
public void testListInference () throws Exception {
164
166
JavaType listOfCats = TypeFactory .defaultInstance ().constructParametricType (List .class , Cat .class );
165
- List <Cat > boxes = sharedMapper () .readValue (arrayOfCatsJson , listOfCats );
167
+ List <Cat > boxes = MAPPER .readValue (arrayOfCatsJson , listOfCats );
166
168
assertTrue (boxes .get (0 ) instanceof LiveCat );
167
169
assertTrue (boxes .get (1 ) instanceof DeadCat );
168
170
}
169
171
170
172
public void testMapInference () throws Exception {
171
173
JavaType mapOfCats = TypeFactory .defaultInstance ().constructParametricType (Map .class , String .class , Cat .class );
172
- Map <String , Cat > map = sharedMapper () .readValue (mapOfCatsJson , mapOfCats );
174
+ Map <String , Cat > map = MAPPER .readValue (mapOfCatsJson , mapOfCats );
173
175
assertEquals (1 , map .size ());
174
176
assertTrue (map .entrySet ().iterator ().next ().getValue () instanceof LiveCat );
175
177
}
176
178
177
179
public void testArrayInference () throws Exception {
178
- Cat [] boxes = sharedMapper () .readValue (arrayOfCatsJson , Cat [].class );
180
+ Cat [] boxes = MAPPER .readValue (arrayOfCatsJson , Cat [].class );
179
181
assertTrue (boxes [0 ] instanceof LiveCat );
180
182
assertTrue (boxes [1 ] instanceof DeadCat );
181
183
}
182
184
183
185
public void testIgnoreProperties () throws Exception {
184
- Cat cat = sharedMapper () .reader ()
186
+ Cat cat = MAPPER .reader ()
185
187
.without (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
186
188
.readValue (luckyCatJson , Cat .class );
187
189
assertTrue (cat instanceof LiveCat );
@@ -210,7 +212,7 @@ public void testAmbiguousClasses() throws Exception {
210
212
211
213
public void testAmbiguousProperties () throws Exception {
212
214
try {
213
- /*Cat cat =*/ sharedMapper () .readValue (ambiguousCatJson , Cat .class );
215
+ /*Cat cat =*/ MAPPER .readValue (ambiguousCatJson , Cat .class );
214
216
fail ("Should not get here" );
215
217
} catch (InvalidTypeIdException e ) {
216
218
verifyException (e , "Cannot deduce unique subtype" );
@@ -250,20 +252,20 @@ public void testDefaultImpl() throws Exception {
250
252
public void testSimpleSerialization () throws Exception {
251
253
// Given:
252
254
JavaType listOfCats = TypeFactory .defaultInstance ().constructParametricType (List .class , Cat .class );
253
- List <Cat > list = sharedMapper () .readValue (arrayOfCatsJson , listOfCats );
255
+ List <Cat > list = MAPPER .readValue (arrayOfCatsJson , listOfCats );
254
256
Cat cat = list .get (0 );
255
257
// When:
256
- String json = sharedMapper () .writeValueAsString (cat );
258
+ String json = MAPPER .writeValueAsString (cat );
257
259
// Then:
258
260
assertEquals (liveCatJson , json );
259
261
}
260
262
261
263
public void testListSerialization () throws Exception {
262
264
// Given:
263
265
JavaType listOfCats = TypeFactory .defaultInstance ().constructParametricType (List .class , Cat .class );
264
- List <Cat > list = sharedMapper () .readValue (arrayOfCatsJson , listOfCats );
266
+ List <Cat > list = MAPPER .readValue (arrayOfCatsJson , listOfCats );
265
267
// When:
266
- String json = sharedMapper () .writeValueAsString (list );
268
+ String json = MAPPER .writeValueAsString (list );
267
269
// Then:
268
270
assertEquals (arrayOfCatsJson , json );
269
271
}
0 commit comments