7
7
import org .junit .Test ;
8
8
9
9
import tools .jackson .core .type .TypeReference ;
10
+ import tools .jackson .databind .ObjectMapper ;
10
11
import tools .jackson .dataformat .xml .XmlMapper ;
11
12
12
13
import static tools .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
+ private final static String [] TEST_DATA = new String [] { "" , "test" , null , "test2" } ;
22
23
23
- @ 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
24
38
public void testStringArray () throws Exception
25
39
{
26
40
// default mode, should get back empty string
27
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
28
- MAPPER .enable (PROCESS_XSI_NIL );
29
- stringArrayRoundtrip (false );
41
+ _stringArrayRoundtrip (MAPPER_READ_NULLS , false );
30
42
31
43
// xsi null enabled, should get back null
32
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
33
- MAPPER .enable (PROCESS_XSI_NIL );
34
- stringArrayRoundtrip (true );
44
+ _stringArrayRoundtrip (MAPPER_READ_WRITE_NULLS , true );
35
45
36
46
// xsi null write enabled but processing disabled, should get back empty string
37
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
38
- MAPPER .disable (PROCESS_XSI_NIL );
39
- stringArrayRoundtrip (false );
47
+ _stringArrayRoundtrip (MAPPER_WRITE_NULLS , false );
40
48
}
41
49
42
- private void stringArrayRoundtrip ( boolean shouldBeNull ) throws Exception
50
+ private void _stringArrayRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
43
51
{
44
- String [] array = new String [] {"" , "test" , null , "test2" };
45
-
46
52
// serialize to string
47
- String xml = MAPPER .writeValueAsString (array );
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 ]);
@@ -62,35 +68,78 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
62
68
assertEquals ("test2" , result [3 ]);
63
69
}
64
70
71
+ @ Test
72
+ public void testStringArrayPojo () throws Exception
73
+ {
74
+ // default mode, should get back empty string
75
+ _stringArrayPojoRoundtrip (MAPPER_READ_NULLS , false );
76
+
77
+ // xsi null enabled, should get back null
78
+ _stringArrayPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
79
+
80
+ // xsi null write enabled but processing disabled, should get back empty string
81
+ _stringArrayPojoRoundtrip (MAPPER_WRITE_NULLS , false );
82
+ }
83
+
84
+ private void _stringArrayPojoRoundtrip (ObjectMapper mapper , 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
+ static class ArrayPojo {
109
+ 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
{
68
123
// default mode, should get back empty string
69
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
70
- MAPPER .enable (PROCESS_XSI_NIL );
71
- stringListRoundtrip (false );
124
+ _stringListRoundtrip (MAPPER_READ_NULLS , false );
72
125
73
126
// xsi null enabled, should get back null
74
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
75
- MAPPER .enable (PROCESS_XSI_NIL );
76
- stringListRoundtrip (true );
127
+ _stringListRoundtrip (MAPPER_READ_WRITE_NULLS , true );
77
128
78
129
// 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
- stringListRoundtrip (false );
130
+ _stringListRoundtrip (MAPPER_WRITE_NULLS , false );
82
131
}
83
132
84
- private void stringListRoundtrip ( boolean shouldBeNull ) throws Exception
133
+ private void _stringListRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
85
134
{
86
- List <String > list = asList ("" , "test" , null , "test2" );
135
+ List <String > list = asList (TEST_DATA );
87
136
88
137
// serialize to string
89
- String xml = MAPPER .writeValueAsString (list );
138
+ String xml = mapper .writeValueAsString (list );
90
139
assertNotNull (xml );
91
140
92
141
// then bring it back
93
- List <String > result = MAPPER .readValue (xml , new TypeReference <List <String >>() {});
142
+ List <String > result = mapper .readValue (xml , new TypeReference <List <String >>() {});
94
143
assertEquals (4 , result .size ());
95
144
assertEquals ("" , result .get (0 ));
96
145
assertEquals ("test" , result .get (1 ));
@@ -105,25 +154,68 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
105
154
}
106
155
107
156
@ Test
108
- public void testStringMap () throws Exception
157
+ public void testStringListPojo () throws Exception
109
158
{
110
159
// default mode, should get back empty string
111
- MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
112
- MAPPER .enable (PROCESS_XSI_NIL );
113
- stringMapRoundtrip (false );
160
+ _stringListPojoRoundtrip (MAPPER_READ_NULLS , false );
114
161
115
162
// xsi null enabled, should get back null
116
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
117
- MAPPER .enable (PROCESS_XSI_NIL );
118
- stringMapRoundtrip (true );
163
+ _stringListPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
119
164
120
165
// xsi null write enabled but processing disabled, should get back empty string
121
- MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
122
- MAPPER .disable (PROCESS_XSI_NIL );
123
- stringMapRoundtrip (false );
166
+ _stringListPojoRoundtrip (MAPPER_WRITE_NULLS , false );
124
167
}
125
168
126
- private void stringMapRoundtrip (boolean shouldBeNull ) throws Exception
169
+ private void _stringListPojoRoundtrip (ObjectMapper mapper , boolean shouldBeNull ) throws Exception
170
+ {
171
+ ListPojo listPojo = new ListPojo ();
172
+ listPojo .setList (asList (TEST_DATA ));
173
+
174
+ // serialize to string
175
+ String xml = mapper .writeValueAsString (listPojo );
176
+ assertNotNull (xml );
177
+
178
+ // then bring it back
179
+ ListPojo result = mapper .readValue (xml , ListPojo .class );
180
+ assertEquals (4 , result .list .size ());
181
+ assertEquals ("" , result .list .get (0 ));
182
+ assertEquals ("test" , result .list .get (1 ));
183
+ if (shouldBeNull )
184
+ {
185
+ assertNull (result .list .get (2 ));
186
+ } else
187
+ {
188
+ assertEquals ("" , result .list .get (2 ));
189
+ }
190
+ assertEquals ("test2" , result .list .get (3 ));
191
+ }
192
+
193
+ static class ListPojo {
194
+ List <String > list ;
195
+
196
+ public List <String > getList () {
197
+ return list ;
198
+ }
199
+
200
+ public void setList (List <String > list ) {
201
+ this .list = list ;
202
+ }
203
+ }
204
+
205
+ @ Test
206
+ public void testStringMapPojo () throws Exception
207
+ {
208
+ // default mode, should get back empty string
209
+ _stringMapPojoRoundtrip (MAPPER_READ_NULLS , false );
210
+
211
+ // xsi null enabled, should get back null
212
+ _stringMapPojoRoundtrip (MAPPER_READ_WRITE_NULLS , true );
213
+
214
+ // xsi null write enabled but processing disabled, should get back empty string
215
+ _stringMapPojoRoundtrip (MAPPER_WRITE_NULLS , false );
216
+ }
217
+
218
+ private void _stringMapPojoRoundtrip (ObjectMapper mapper , boolean shouldBeNull ) throws Exception
127
219
{
128
220
Map <String , String > map = new HashMap <String , String >() {{
129
221
put ("a" , "" );
@@ -132,14 +224,14 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
132
224
put ("d" , "test2" );
133
225
}};
134
226
MapPojo mapPojo = new MapPojo ();
135
- mapPojo .setMap ( map );
227
+ mapPojo .setMap (map );
136
228
137
229
// serialize to string
138
- String xml = MAPPER .writeValueAsString (mapPojo );
230
+ String xml = mapper .writeValueAsString (mapPojo );
139
231
assertNotNull (xml );
140
232
141
233
// then bring it back
142
- MapPojo result = MAPPER .readValue (xml , MapPojo .class );
234
+ MapPojo result = mapper .readValue (xml , MapPojo .class );
143
235
assertEquals (4 , result .map .size ());
144
236
assertEquals ("" , result .map .get ("a" ));
145
237
assertEquals ("test" , result .map .get ("b" ));
@@ -153,11 +245,8 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
153
245
assertEquals ("test2" , result .map .get ("d" ));
154
246
}
155
247
156
- private static class MapPojo {
157
- private Map <String , String > map ;
158
-
159
- public MapPojo () {
160
- }
248
+ static class MapPojo {
249
+ Map <String , String > map ;
161
250
162
251
public Map <String , String > getMap () {
163
252
return map ;
@@ -167,4 +256,75 @@ public void setMap(Map<String, String> map) {
167
256
this .map = map ;
168
257
}
169
258
}
259
+
260
+ @ Test
261
+ public void testStringPojo () throws Exception
262
+ {
263
+ // default mode, should get back empty string
264
+ _stringPojoRoundtrip (MAPPER_READ_NULLS , false );
265
+
266
+ // xsi null enabled, should get back null
267
+
268
+ // 19-Mar-2023, tatu: I think this fails due to [dataformat-xml#359]:
269
+ // need to comment out for now
270
+
271
+ // _stringPojoRoundtrip(MAPPER_READ_WRITE_NULLS, true);
272
+
273
+ // xsi null write enabled but processing disabled, should get back empty string
274
+ _stringPojoRoundtrip (MAPPER_WRITE_NULLS , false );
275
+ }
276
+
277
+ private void _stringPojoRoundtrip (ObjectMapper mapper , boolean shouldBeNull ) throws Exception
278
+ {
279
+ StringPojo stringPojo = new StringPojo ();
280
+ stringPojo .setNormalString ("test" );
281
+ stringPojo .setEmptyString ("" );
282
+ stringPojo .setNullString (null );
283
+
284
+ // serialize to string
285
+ String xml = mapper .writeValueAsString (stringPojo );
286
+ assertNotNull (xml );
287
+
288
+ // then bring it back
289
+ StringPojo result = mapper .readValue (xml , StringPojo .class );
290
+ assertEquals ("test" , result .normalString );
291
+ assertEquals ("" , result .emptyString );
292
+ if (shouldBeNull )
293
+ {
294
+ assertNull (result .nullString );
295
+ } else
296
+ {
297
+ assertEquals ("" , result .nullString );
298
+ }
299
+ }
300
+
301
+ static class StringPojo {
302
+ String normalString ;
303
+ String emptyString ;
304
+ String nullString ;
305
+
306
+ public String getNormalString () {
307
+ return normalString ;
308
+ }
309
+
310
+ public void setNormalString (String normalString ) {
311
+ this .normalString = normalString ;
312
+ }
313
+
314
+ public String getEmptyString () {
315
+ return emptyString ;
316
+ }
317
+
318
+ public void setEmptyString (String emptyString ) {
319
+ this .emptyString = emptyString ;
320
+ }
321
+
322
+ public String getNullString () {
323
+ return nullString ;
324
+ }
325
+
326
+ public void setNullString (String nullString ) {
327
+ this .nullString = nullString ;
328
+ }
329
+ }
170
330
}
0 commit comments