20
20
public class StringListRoundtripTest {
21
21
private final static XmlMapper MAPPER = new XmlMapper ();
22
22
23
+ private final static String [] TEST_DATA = new String [] {"" , "test" , null , "test2" };
24
+
23
25
@ Test
24
26
public void testStringArray () throws Exception
25
27
{
@@ -41,10 +43,8 @@ public void testStringArray() throws Exception
41
43
42
44
private void stringArrayRoundtrip (boolean shouldBeNull ) throws Exception
43
45
{
44
- String [] array = new String [] {"" , "test" , null , "test2" };
45
-
46
46
// serialize to string
47
- String xml = MAPPER .writeValueAsString (array );
47
+ String xml = MAPPER .writeValueAsString (TEST_DATA );
48
48
assertNotNull (xml );
49
49
50
50
// then bring it back
@@ -62,6 +62,61 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
62
62
assertEquals ("test2" , result [3 ]);
63
63
}
64
64
65
+ @ Test
66
+ public void testStringArrayPojo () throws Exception
67
+ {
68
+ // default mode, should get back empty string
69
+ MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
70
+ MAPPER .enable (PROCESS_XSI_NIL );
71
+ stringArrayPojoRoundtrip (false );
72
+
73
+ // xsi null enabled, should get back null
74
+ MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
75
+ MAPPER .enable (PROCESS_XSI_NIL );
76
+ stringArrayPojoRoundtrip (true );
77
+
78
+ // 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 );
82
+ }
83
+
84
+ private void stringArrayPojoRoundtrip (boolean shouldBeNull ) throws Exception
85
+ {
86
+ ArrayPojo arrayPojo = new ArrayPojo ();
87
+ arrayPojo .setArray (TEST_DATA );
88
+
89
+ // serialize to string
90
+ String xml = MAPPER .writeValueAsString (arrayPojo );
91
+ assertNotNull (xml );
92
+
93
+ // then bring it back
94
+ ArrayPojo result = MAPPER .readValue (xml , ArrayPojo .class );
95
+ assertEquals (4 , result .array .length );
96
+ assertEquals ("" , result .array [0 ]);
97
+ assertEquals ("test" , result .array [1 ]);
98
+ if (shouldBeNull )
99
+ {
100
+ assertNull (result .array [2 ]);
101
+ } else
102
+ {
103
+ assertEquals ("" , result .array [2 ]);
104
+ }
105
+ assertEquals ("test2" , result .array [3 ]);
106
+ }
107
+
108
+ private static class ArrayPojo {
109
+ private String [] array ;
110
+
111
+ public String [] getArray () {
112
+ return array ;
113
+ }
114
+
115
+ public void setArray (String [] array ) {
116
+ this .array = array ;
117
+ }
118
+ }
119
+
65
120
@ Test
66
121
public void testStringList () throws Exception
67
122
{
@@ -83,7 +138,7 @@ public void testStringList() throws Exception
83
138
84
139
private void stringListRoundtrip (boolean shouldBeNull ) throws Exception
85
140
{
86
- List <String > list = asList ("" , "test" , null , "test2" );
141
+ List <String > list = asList (TEST_DATA );
87
142
88
143
// serialize to string
89
144
String xml = MAPPER .writeValueAsString (list );
@@ -105,25 +160,80 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
105
160
}
106
161
107
162
@ Test
108
- public void testStringMap () throws Exception
163
+ public void testStringListPojo () throws Exception
164
+ {
165
+ // default mode, should get back empty string
166
+ MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
167
+ MAPPER .enable (PROCESS_XSI_NIL );
168
+ stringListPojoRoundtrip (false );
169
+
170
+ // xsi null enabled, should get back null
171
+ MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
172
+ MAPPER .enable (PROCESS_XSI_NIL );
173
+ stringListPojoRoundtrip (true );
174
+
175
+ // 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 );
179
+ }
180
+
181
+ private void stringListPojoRoundtrip (boolean shouldBeNull ) throws Exception
182
+ {
183
+ ListPojo listPojo = new ListPojo ();
184
+ listPojo .setList (asList (TEST_DATA ));
185
+
186
+ // serialize to string
187
+ String xml = MAPPER .writeValueAsString (listPojo );
188
+ assertNotNull (xml );
189
+
190
+ // then bring it back
191
+ ListPojo result = MAPPER .readValue (xml , ListPojo .class );
192
+ assertEquals (4 , result .list .size ());
193
+ assertEquals ("" , result .list .get (0 ));
194
+ assertEquals ("test" , result .list .get (1 ));
195
+ if (shouldBeNull )
196
+ {
197
+ assertNull (result .list .get (2 ));
198
+ } else
199
+ {
200
+ assertEquals ("" , result .list .get (2 ));
201
+ }
202
+ assertEquals ("test2" , result .list .get (3 ));
203
+ }
204
+
205
+ private static class ListPojo {
206
+ private List <String > list ;
207
+
208
+ public List <String > getList () {
209
+ return list ;
210
+ }
211
+
212
+ public void setList (List <String > list ) {
213
+ this .list = list ;
214
+ }
215
+ }
216
+
217
+ @ Test
218
+ public void testStringMapPojo () throws Exception
109
219
{
110
220
// default mode, should get back empty string
111
221
MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
112
222
MAPPER .enable (PROCESS_XSI_NIL );
113
- stringMapRoundtrip (false );
223
+ stringMapPojoRoundtrip (false );
114
224
115
225
// xsi null enabled, should get back null
116
226
MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
117
227
MAPPER .enable (PROCESS_XSI_NIL );
118
- stringMapRoundtrip (true );
228
+ stringMapPojoRoundtrip (true );
119
229
120
230
// xsi null write enabled but processing disabled, should get back empty string
121
231
MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
122
232
MAPPER .disable (PROCESS_XSI_NIL );
123
- stringMapRoundtrip (false );
233
+ stringMapPojoRoundtrip (false );
124
234
}
125
235
126
- private void stringMapRoundtrip (boolean shouldBeNull ) throws Exception
236
+ private void stringMapPojoRoundtrip (boolean shouldBeNull ) throws Exception
127
237
{
128
238
Map <String , String > map = new HashMap <String , String >() {{
129
239
put ("a" , "" );
@@ -132,7 +242,7 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
132
242
put ("d" , "test2" );
133
243
}};
134
244
MapPojo mapPojo = new MapPojo ();
135
- mapPojo .setMap ( map );
245
+ mapPojo .setMap (map );
136
246
137
247
// serialize to string
138
248
String xml = MAPPER .writeValueAsString (mapPojo );
@@ -156,9 +266,6 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
156
266
private static class MapPojo {
157
267
private Map <String , String > map ;
158
268
159
- public MapPojo () {
160
- }
161
-
162
269
public Map <String , String > getMap () {
163
270
return map ;
164
271
}
@@ -167,4 +274,77 @@ public void setMap(Map<String, String> map) {
167
274
this .map = map ;
168
275
}
169
276
}
277
+
278
+ @ Test
279
+ public void testStringPojo () throws Exception
280
+ {
281
+ // default mode, should get back empty string
282
+ MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
283
+ MAPPER .enable (PROCESS_XSI_NIL );
284
+ stringPojoRoundtrip (false );
285
+
286
+ // xsi null enabled, should get back null
287
+ MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
288
+ MAPPER .enable (PROCESS_XSI_NIL );
289
+ stringPojoRoundtrip (true );
290
+
291
+ // 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 );
295
+ }
296
+
297
+ private void stringPojoRoundtrip (boolean shouldBeNull ) throws Exception
298
+ {
299
+ StringPojo stringPojo = new StringPojo ();
300
+ stringPojo .setNormalString ("test" );
301
+ stringPojo .setEmptyString ("" );
302
+ stringPojo .setNullString (null );
303
+
304
+ // serialize to string
305
+ String xml = MAPPER .writeValueAsString (stringPojo );
306
+ assertNotNull (xml );
307
+
308
+ // then bring it back
309
+ StringPojo result = MAPPER .readValue (xml , StringPojo .class );
310
+ assertEquals ("test" , result .normalString );
311
+ assertEquals ("" , result .emptyString );
312
+ if (shouldBeNull )
313
+ {
314
+ assertNull (result .nullString );
315
+ } else
316
+ {
317
+ assertEquals ("" , result .nullString );
318
+ }
319
+ }
320
+
321
+ private static class StringPojo {
322
+ private String normalString ;
323
+ private String emptyString ;
324
+ private String nullString ;
325
+
326
+ public String getNormalString () {
327
+ return normalString ;
328
+ }
329
+
330
+ public void setNormalString (String normalString ) {
331
+ this .normalString = normalString ;
332
+ }
333
+
334
+ public String getEmptyString () {
335
+ return emptyString ;
336
+ }
337
+
338
+ public void setEmptyString (String emptyString ) {
339
+ this .emptyString = emptyString ;
340
+ }
341
+
342
+ public String getNullString () {
343
+ return nullString ;
344
+ }
345
+
346
+ public void setNullString (String nullString ) {
347
+ this .nullString = nullString ;
348
+ }
349
+ }
170
350
}
0 commit comments