77import org .junit .Test ;
88
99import com .fasterxml .jackson .core .type .TypeReference ;
10+ import com .fasterxml .jackson .databind .ObjectMapper ;
1011import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
1112
1213import static com .fasterxml .jackson .dataformat .xml .deser .FromXmlParser .Feature .PROCESS_XSI_NIL ;
1819
1920// [dataformat-xml#584]
2021public class StringListRoundtripTest {
21- private final static XmlMapper MAPPER = new XmlMapper ();
22-
2322 private final static String [] TEST_DATA = new String [] {"" , "test" , null , "test2" };
2423
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
2638 public void testStringArray () throws Exception
2739 {
2840 // 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 );
3242
3343 // 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 );
3745
3846 // 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 );
4248 }
4349
44- private void stringArrayRoundtrip ( boolean shouldBeNull ) throws Exception
50+ private void _stringArrayRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
4551 {
4652 // serialize to string
47- String xml = MAPPER .writeValueAsString (TEST_DATA );
53+ String xml = mapper .writeValueAsString (TEST_DATA );
4854 assertNotNull (xml );
4955
5056 // then bring it back
51- String [] result = MAPPER .readValue (xml , String [].class );
57+ String [] result = mapper .readValue (xml , String [].class );
5258 assertEquals (4 , result .length );
5359 assertEquals ("" , result [0 ]);
5460 assertEquals ("test" , result [1 ]);
@@ -66,32 +72,26 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
6672 public void testStringArrayPojo () throws Exception
6773 {
6874 // 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 );
7276
7377 // 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 );
7779
7880 // 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 );
8282 }
8383
84- private void stringArrayPojoRoundtrip ( boolean shouldBeNull ) throws Exception
84+ private void _stringArrayPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
8585 {
8686 ArrayPojo arrayPojo = new ArrayPojo ();
8787 arrayPojo .setArray (TEST_DATA );
8888
8989 // serialize to string
90- String xml = MAPPER .writeValueAsString (arrayPojo );
90+ String xml = mapper .writeValueAsString (arrayPojo );
9191 assertNotNull (xml );
9292
9393 // then bring it back
94- ArrayPojo result = MAPPER .readValue (xml , ArrayPojo .class );
94+ ArrayPojo result = mapper .readValue (xml , ArrayPojo .class );
9595 assertEquals (4 , result .array .length );
9696 assertEquals ("" , result .array [0 ]);
9797 assertEquals ("test" , result .array [1 ]);
@@ -105,8 +105,8 @@ private void stringArrayPojoRoundtrip(boolean shouldBeNull) throws Exception
105105 assertEquals ("test2" , result .array [3 ]);
106106 }
107107
108- private static class ArrayPojo {
109- private String [] array ;
108+ static class ArrayPojo {
109+ String [] array ;
110110
111111 public String [] getArray () {
112112 return array ;
@@ -121,31 +121,25 @@ public void setArray(String[] array) {
121121 public void testStringList () throws Exception
122122 {
123123 // 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 );
127125
128126 // 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 );
132128
133129 // 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 );
137131 }
138132
139- private void stringListRoundtrip ( boolean shouldBeNull ) throws Exception
133+ private void _stringListRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
140134 {
141135 List <String > list = asList (TEST_DATA );
142136
143137 // serialize to string
144- String xml = MAPPER .writeValueAsString (list );
138+ String xml = mapper .writeValueAsString (list );
145139 assertNotNull (xml );
146140
147141 // 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 >>() {});
149143 assertEquals (4 , result .size ());
150144 assertEquals ("" , result .get (0 ));
151145 assertEquals ("test" , result .get (1 ));
@@ -163,32 +157,26 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
163157 public void testStringListPojo () throws Exception
164158 {
165159 // 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 );
169161
170162 // 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 );
174164
175165 // 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 );
179167 }
180168
181- private void stringListPojoRoundtrip ( boolean shouldBeNull ) throws Exception
169+ private void _stringListPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
182170 {
183171 ListPojo listPojo = new ListPojo ();
184172 listPojo .setList (asList (TEST_DATA ));
185173
186174 // serialize to string
187- String xml = MAPPER .writeValueAsString (listPojo );
175+ String xml = mapper .writeValueAsString (listPojo );
188176 assertNotNull (xml );
189177
190178 // then bring it back
191- ListPojo result = MAPPER .readValue (xml , ListPojo .class );
179+ ListPojo result = mapper .readValue (xml , ListPojo .class );
192180 assertEquals (4 , result .list .size ());
193181 assertEquals ("" , result .list .get (0 ));
194182 assertEquals ("test" , result .list .get (1 ));
@@ -202,8 +190,8 @@ private void stringListPojoRoundtrip(boolean shouldBeNull) throws Exception
202190 assertEquals ("test2" , result .list .get (3 ));
203191 }
204192
205- private static class ListPojo {
206- private List <String > list ;
193+ static class ListPojo {
194+ List <String > list ;
207195
208196 public List <String > getList () {
209197 return list ;
@@ -218,22 +206,16 @@ public void setList(List<String> list) {
218206 public void testStringMapPojo () throws Exception
219207 {
220208 // 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 );
224210
225211 // 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 );
229213
230214 // 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 );
234216 }
235217
236- private void stringMapPojoRoundtrip ( boolean shouldBeNull ) throws Exception
218+ private void _stringMapPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
237219 {
238220 Map <String , String > map = new HashMap <String , String >() {{
239221 put ("a" , "" );
@@ -245,11 +227,11 @@ private void stringMapPojoRoundtrip(boolean shouldBeNull) throws Exception
245227 mapPojo .setMap (map );
246228
247229 // serialize to string
248- String xml = MAPPER .writeValueAsString (mapPojo );
230+ String xml = mapper .writeValueAsString (mapPojo );
249231 assertNotNull (xml );
250232
251233 // then bring it back
252- MapPojo result = MAPPER .readValue (xml , MapPojo .class );
234+ MapPojo result = mapper .readValue (xml , MapPojo .class );
253235 assertEquals (4 , result .map .size ());
254236 assertEquals ("" , result .map .get ("a" ));
255237 assertEquals ("test" , result .map .get ("b" ));
@@ -263,8 +245,8 @@ private void stringMapPojoRoundtrip(boolean shouldBeNull) throws Exception
263245 assertEquals ("test2" , result .map .get ("d" ));
264246 }
265247
266- private static class MapPojo {
267- private Map <String , String > map ;
248+ static class MapPojo {
249+ Map <String , String > map ;
268250
269251 public Map <String , String > getMap () {
270252 return map ;
@@ -279,34 +261,28 @@ public void setMap(Map<String, String> map) {
279261 public void testStringPojo () throws Exception
280262 {
281263 // 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 );
285265
286266 // 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 );
290268
291269 // 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 );
295271 }
296272
297- private void stringPojoRoundtrip ( boolean shouldBeNull ) throws Exception
273+ private void _stringPojoRoundtrip ( ObjectMapper mapper , boolean shouldBeNull ) throws Exception
298274 {
299275 StringPojo stringPojo = new StringPojo ();
300276 stringPojo .setNormalString ("test" );
301277 stringPojo .setEmptyString ("" );
302278 stringPojo .setNullString (null );
303279
304280 // serialize to string
305- String xml = MAPPER .writeValueAsString (stringPojo );
281+ String xml = mapper .writeValueAsString (stringPojo );
306282 assertNotNull (xml );
307283
308284 // then bring it back
309- StringPojo result = MAPPER .readValue (xml , StringPojo .class );
285+ StringPojo result = mapper .readValue (xml , StringPojo .class );
310286 assertEquals ("test" , result .normalString );
311287 assertEquals ("" , result .emptyString );
312288 if (shouldBeNull )
@@ -318,10 +294,10 @@ private void stringPojoRoundtrip(boolean shouldBeNull) throws Exception
318294 }
319295 }
320296
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 ;
325301
326302 public String getNormalString () {
327303 return normalString ;
0 commit comments