2020public class StringListRoundtripTest {
2121 private final static XmlMapper MAPPER = new XmlMapper ();
2222
23+ private final static String [] TEST_DATA = new String [] {"" , "test" , null , "test2" };
24+
2325 @ Test
2426 public void testStringArray () throws Exception
2527 {
@@ -41,10 +43,8 @@ public void testStringArray() throws Exception
4143
4244 private void stringArrayRoundtrip (boolean shouldBeNull ) throws Exception
4345 {
44- String [] array = new String [] {"" , "test" , null , "test2" };
45-
4646 // serialize to string
47- String xml = MAPPER .writeValueAsString (array );
47+ String xml = MAPPER .writeValueAsString (TEST_DATA );
4848 assertNotNull (xml );
4949
5050 // then bring it back
@@ -62,6 +62,61 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
6262 assertEquals ("test2" , result [3 ]);
6363 }
6464
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+
65120 @ Test
66121 public void testStringList () throws Exception
67122 {
@@ -83,7 +138,7 @@ public void testStringList() throws Exception
83138
84139 private void stringListRoundtrip (boolean shouldBeNull ) throws Exception
85140 {
86- List <String > list = asList ("" , "test" , null , "test2" );
141+ List <String > list = asList (TEST_DATA );
87142
88143 // serialize to string
89144 String xml = MAPPER .writeValueAsString (list );
@@ -105,25 +160,80 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
105160 }
106161
107162 @ 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
109219 {
110220 // default mode, should get back empty string
111221 MAPPER .disable (WRITE_NULLS_AS_XSI_NIL );
112222 MAPPER .enable (PROCESS_XSI_NIL );
113- stringMapRoundtrip (false );
223+ stringMapPojoRoundtrip (false );
114224
115225 // xsi null enabled, should get back null
116226 MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
117227 MAPPER .enable (PROCESS_XSI_NIL );
118- stringMapRoundtrip (true );
228+ stringMapPojoRoundtrip (true );
119229
120230 // xsi null write enabled but processing disabled, should get back empty string
121231 MAPPER .enable (WRITE_NULLS_AS_XSI_NIL );
122232 MAPPER .disable (PROCESS_XSI_NIL );
123- stringMapRoundtrip (false );
233+ stringMapPojoRoundtrip (false );
124234 }
125235
126- private void stringMapRoundtrip (boolean shouldBeNull ) throws Exception
236+ private void stringMapPojoRoundtrip (boolean shouldBeNull ) throws Exception
127237 {
128238 Map <String , String > map = new HashMap <String , String >() {{
129239 put ("a" , "" );
@@ -132,7 +242,7 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
132242 put ("d" , "test2" );
133243 }};
134244 MapPojo mapPojo = new MapPojo ();
135- mapPojo .setMap ( map );
245+ mapPojo .setMap (map );
136246
137247 // serialize to string
138248 String xml = MAPPER .writeValueAsString (mapPojo );
@@ -156,9 +266,6 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
156266 private static class MapPojo {
157267 private Map <String , String > map ;
158268
159- public MapPojo () {
160- }
161-
162269 public Map <String , String > getMap () {
163270 return map ;
164271 }
@@ -167,4 +274,77 @@ public void setMap(Map<String, String> map) {
167274 this .map = map ;
168275 }
169276 }
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+ }
170350}
0 commit comments