7
7
import org .junit .Test ;
8
8
9
9
import com .fasterxml .jackson .core .type .TypeReference ;
10
+ import com .fasterxml .jackson .databind .ObjectMapper ;
10
11
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
11
12
12
13
import static com .fasterxml .jackson .dataformat .xml .deser .FromXmlParser .Feature .PROCESS_XSI_NIL ;
18
19
19
20
// [dataformat-xml#584]
20
21
public class StringListRoundtripTest {
21
- private final static XmlMapper MAPPER = new XmlMapper ();
22
-
23
22
private final static String [] TEST_DATA = new String [] {"" , "test" , null , "test2" };
24
23
25
- @ Test
24
+ private final static XmlMapper MAPPER_READ_WRITE_NULLS = XmlMapper .builder ()
25
+ .enable (PROCESS_XSI_NIL )
26
+ .enable (WRITE_NULLS_AS_XSI_NIL )
27
+ .build ();
28
+ private final static XmlMapper MAPPER_READ_NULLS = XmlMapper .builder ()
29
+ .enable (PROCESS_XSI_NIL )
30
+ .disable (WRITE_NULLS_AS_XSI_NIL )
31
+ .build ();
32
+ private final static XmlMapper MAPPER_WRITE_NULLS = XmlMapper .builder ()
33
+ .disable (PROCESS_XSI_NIL )
34
+ .enable (WRITE_NULLS_AS_XSI_NIL )
35
+ .build ();
36
+
37
+ @ Test
26
38
public void testStringArray () throws Exception
27
39
{
28
40
// default mode, should get back empty string
29
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
30
- MAPPER .enable (PROCESS_XSI_NIL );
31
- stringArrayRoundtrip (false );
41
+ _stringArrayRoundtrip (MAPPER_READ_NULLS , false );
32
42
33
43
// xsi null enabled, should get back null
34
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
35
- MAPPER .enable (PROCESS_XSI_NIL );
36
- stringArrayRoundtrip (true );
44
+ _stringArrayRoundtrip (MAPPER_READ_WRITE_NULLS , true );
37
45
38
46
// xsi null write enabled but processing disabled, should get back empty string
39
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
40
- MAPPER .disable (PROCESS_XSI_NIL );
41
- stringArrayRoundtrip (false );
47
+ _stringArrayRoundtrip (MAPPER_WRITE_NULLS , false );
42
48
}
43
49
44
- private void stringArrayRoundtrip ( boolean shouldBeNull ) throws Exception
50
+ private void _stringArrayRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
45
51
{
46
52
// serialize to string
47
- String xml = MAPPER .writeValueAsString (TEST_DATA );
53
+ String xml = mapper .writeValueAsString (TEST_DATA );
48
54
assertNotNull (xml );
49
55
50
56
// then bring it back
51
- String [] result = MAPPER .readValue (xml , String [].class );
57
+ String [] result = mapper .readValue (xml , String [].class );
52
58
assertEquals (4 , result .length );
53
59
assertEquals ("" , result [0 ]);
54
60
assertEquals ("test" , result [1 ]);
@@ -66,32 +72,26 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
66
72
public void testStringArrayPojo () throws Exception
67
73
{
68
74
// default mode, should get back empty string
69
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
70
- MAPPER .enable (PROCESS_XSI_NIL );
71
- stringArrayPojoRoundtrip (false );
75
+ _stringArrayPojoRoundtrip (MAPPER_READ_NULLS , false );
72
76
73
77
// xsi null enabled, should get back null
74
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
75
- MAPPER .enable (PROCESS_XSI_NIL );
76
- stringArrayPojoRoundtrip (true );
78
+ _stringArrayPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
77
79
78
80
// xsi null write enabled but processing disabled, should get back empty string
79
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
80
- MAPPER .disable (PROCESS_XSI_NIL );
81
- stringArrayPojoRoundtrip (false );
81
+ _stringArrayPojoRoundtrip (MAPPER_WRITE_NULLS , false );
82
82
}
83
83
84
- private void stringArrayPojoRoundtrip ( boolean shouldBeNull ) throws Exception
84
+ private void _stringArrayPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
85
85
{
86
86
ArrayPojo arrayPojo = new ArrayPojo ();
87
87
arrayPojo .setArray (TEST_DATA );
88
88
89
89
// serialize to string
90
- String xml = MAPPER .writeValueAsString (arrayPojo );
90
+ String xml = mapper .writeValueAsString (arrayPojo );
91
91
assertNotNull (xml );
92
92
93
93
// then bring it back
94
- ArrayPojo result = MAPPER .readValue (xml , ArrayPojo .class );
94
+ ArrayPojo result = mapper .readValue (xml , ArrayPojo .class );
95
95
assertEquals (4 , result .array .length );
96
96
assertEquals ("" , result .array [0 ]);
97
97
assertEquals ("test" , result .array [1 ]);
@@ -105,8 +105,8 @@ private void stringArrayPojoRoundtrip(boolean shouldBeNull) throws Exception
105
105
assertEquals ("test2" , result .array [3 ]);
106
106
}
107
107
108
- private static class ArrayPojo {
109
- private String [] array ;
108
+ static class ArrayPojo {
109
+ String [] array ;
110
110
111
111
public String [] getArray () {
112
112
return array ;
@@ -121,31 +121,25 @@ public void setArray(String[] array) {
121
121
public void testStringList () throws Exception
122
122
{
123
123
// default mode, should get back empty string
124
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
125
- MAPPER .enable (PROCESS_XSI_NIL );
126
- stringListRoundtrip (false );
124
+ _stringListRoundtrip (MAPPER_READ_NULLS , false );
127
125
128
126
// xsi null enabled, should get back null
129
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
130
- MAPPER .enable (PROCESS_XSI_NIL );
131
- stringListRoundtrip (true );
127
+ _stringListRoundtrip (MAPPER_READ_WRITE_NULLS , true );
132
128
133
129
// xsi null write enabled but processing disabled, should get back empty string
134
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
135
- MAPPER .disable (PROCESS_XSI_NIL );
136
- stringListRoundtrip (false );
130
+ _stringListRoundtrip (MAPPER_WRITE_NULLS , false );
137
131
}
138
132
139
- private void stringListRoundtrip ( boolean shouldBeNull ) throws Exception
133
+ private void _stringListRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
140
134
{
141
135
List <String > list = asList (TEST_DATA );
142
136
143
137
// serialize to string
144
- String xml = MAPPER .writeValueAsString (list );
138
+ String xml = mapper .writeValueAsString (list );
145
139
assertNotNull (xml );
146
140
147
141
// then bring it back
148
- List <String > result = MAPPER .readValue (xml , new TypeReference <List <String >>() {});
142
+ List <String > result = mapper .readValue (xml , new TypeReference <List <String >>() {});
149
143
assertEquals (4 , result .size ());
150
144
assertEquals ("" , result .get (0 ));
151
145
assertEquals ("test" , result .get (1 ));
@@ -163,32 +157,26 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
163
157
public void testStringListPojo () throws Exception
164
158
{
165
159
// default mode, should get back empty string
166
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
167
- MAPPER .enable (PROCESS_XSI_NIL );
168
- stringListPojoRoundtrip (false );
160
+ _stringListPojoRoundtrip (MAPPER_READ_NULLS , false );
169
161
170
162
// xsi null enabled, should get back null
171
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
172
- MAPPER .enable (PROCESS_XSI_NIL );
173
- stringListPojoRoundtrip (true );
163
+ _stringListPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
174
164
175
165
// xsi null write enabled but processing disabled, should get back empty string
176
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
177
- MAPPER .disable (PROCESS_XSI_NIL );
178
- stringListPojoRoundtrip (false );
166
+ _stringListPojoRoundtrip (MAPPER_WRITE_NULLS , false );
179
167
}
180
168
181
- private void stringListPojoRoundtrip ( boolean shouldBeNull ) throws Exception
169
+ private void _stringListPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
182
170
{
183
171
ListPojo listPojo = new ListPojo ();
184
172
listPojo .setList (asList (TEST_DATA ));
185
173
186
174
// serialize to string
187
- String xml = MAPPER .writeValueAsString (listPojo );
175
+ String xml = mapper .writeValueAsString (listPojo );
188
176
assertNotNull (xml );
189
177
190
178
// then bring it back
191
- ListPojo result = MAPPER .readValue (xml , ListPojo .class );
179
+ ListPojo result = mapper .readValue (xml , ListPojo .class );
192
180
assertEquals (4 , result .list .size ());
193
181
assertEquals ("" , result .list .get (0 ));
194
182
assertEquals ("test" , result .list .get (1 ));
@@ -202,8 +190,8 @@ private void stringListPojoRoundtrip(boolean shouldBeNull) throws Exception
202
190
assertEquals ("test2" , result .list .get (3 ));
203
191
}
204
192
205
- private static class ListPojo {
206
- private List <String > list ;
193
+ static class ListPojo {
194
+ List <String > list ;
207
195
208
196
public List <String > getList () {
209
197
return list ;
@@ -218,22 +206,16 @@ public void setList(List<String> list) {
218
206
public void testStringMapPojo () throws Exception
219
207
{
220
208
// default mode, should get back empty string
221
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
222
- MAPPER .enable (PROCESS_XSI_NIL );
223
- stringMapPojoRoundtrip (false );
209
+ _stringMapPojoRoundtrip (MAPPER_READ_NULLS , false );
224
210
225
211
// xsi null enabled, should get back null
226
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
227
- MAPPER .enable (PROCESS_XSI_NIL );
228
- stringMapPojoRoundtrip (true );
212
+ _stringMapPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
229
213
230
214
// xsi null write enabled but processing disabled, should get back empty string
231
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
232
- MAPPER .disable (PROCESS_XSI_NIL );
233
- stringMapPojoRoundtrip (false );
215
+ _stringMapPojoRoundtrip (MAPPER_WRITE_NULLS , false );
234
216
}
235
217
236
- private void stringMapPojoRoundtrip ( boolean shouldBeNull ) throws Exception
218
+ private void _stringMapPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
237
219
{
238
220
Map <String , String > map = new HashMap <String , String >() {{
239
221
put ("a" , "" );
@@ -245,11 +227,11 @@ private void stringMapPojoRoundtrip(boolean shouldBeNull) throws Exception
245
227
mapPojo .setMap (map );
246
228
247
229
// serialize to string
248
- String xml = MAPPER .writeValueAsString (mapPojo );
230
+ String xml = mapper .writeValueAsString (mapPojo );
249
231
assertNotNull (xml );
250
232
251
233
// then bring it back
252
- MapPojo result = MAPPER .readValue (xml , MapPojo .class );
234
+ MapPojo result = mapper .readValue (xml , MapPojo .class );
253
235
assertEquals (4 , result .map .size ());
254
236
assertEquals ("" , result .map .get ("a" ));
255
237
assertEquals ("test" , result .map .get ("b" ));
@@ -263,8 +245,8 @@ private void stringMapPojoRoundtrip(boolean shouldBeNull) throws Exception
263
245
assertEquals ("test2" , result .map .get ("d" ));
264
246
}
265
247
266
- private static class MapPojo {
267
- private Map <String , String > map ;
248
+ static class MapPojo {
249
+ Map <String , String > map ;
268
250
269
251
public Map <String , String > getMap () {
270
252
return map ;
@@ -279,34 +261,28 @@ public void setMap(Map<String, String> map) {
279
261
public void testStringPojo () throws Exception
280
262
{
281
263
// default mode, should get back empty string
282
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
283
- MAPPER .enable (PROCESS_XSI_NIL );
284
- stringPojoRoundtrip (false );
264
+ _stringPojoRoundtrip (MAPPER_READ_NULLS , false );
285
265
286
266
// xsi null enabled, should get back null
287
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
288
- MAPPER .enable (PROCESS_XSI_NIL );
289
- stringPojoRoundtrip (true );
267
+ _stringPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
290
268
291
269
// xsi null write enabled but processing disabled, should get back empty string
292
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
293
- MAPPER .disable (PROCESS_XSI_NIL );
294
- stringPojoRoundtrip (false );
270
+ _stringPojoRoundtrip (MAPPER_WRITE_NULLS , false );
295
271
}
296
272
297
- private void stringPojoRoundtrip ( boolean shouldBeNull ) throws Exception
273
+ private void _stringPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
298
274
{
299
275
StringPojo stringPojo = new StringPojo ();
300
276
stringPojo .setNormalString ("test" );
301
277
stringPojo .setEmptyString ("" );
302
278
stringPojo .setNullString (null );
303
279
304
280
// serialize to string
305
- String xml = MAPPER .writeValueAsString (stringPojo );
281
+ String xml = mapper .writeValueAsString (stringPojo );
306
282
assertNotNull (xml );
307
283
308
284
// then bring it back
309
- StringPojo result = MAPPER .readValue (xml , StringPojo .class );
285
+ StringPojo result = mapper .readValue (xml , StringPojo .class );
310
286
assertEquals ("test" , result .normalString );
311
287
assertEquals ("" , result .emptyString );
312
288
if (shouldBeNull )
@@ -318,10 +294,10 @@ private void stringPojoRoundtrip(boolean shouldBeNull) throws Exception
318
294
}
319
295
}
320
296
321
- private static class StringPojo {
322
- private String normalString ;
323
- private String emptyString ;
324
- private String nullString ;
297
+ static class StringPojo {
298
+ String normalString ;
299
+ String emptyString ;
300
+ String nullString ;
325
301
326
302
public String getNormalString () {
327
303
return normalString ;
0 commit comments