Skip to content

Commit 1597517

Browse files
committed
Merge branch '2.15'
2 parents e5fbf10 + 457ade0 commit 1597517

File tree

1 file changed

+208
-48
lines changed

1 file changed

+208
-48
lines changed

src/test/java/tools/jackson/dataformat/xml/lists/StringListRoundtripTest.java

Lines changed: 208 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88

99
import tools.jackson.core.type.TypeReference;
10+
import tools.jackson.databind.ObjectMapper;
1011
import tools.jackson.dataformat.xml.XmlMapper;
1112

1213
import static tools.jackson.dataformat.xml.deser.FromXmlParser.Feature.PROCESS_XSI_NIL;
@@ -18,37 +19,42 @@
1819

1920
// [dataformat-xml#584]
2021
public class StringListRoundtripTest {
21-
private final static XmlMapper MAPPER = new XmlMapper();
22+
private final static String[] TEST_DATA = new String[] {"", "test", null, "test2"};
2223

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
2438
public void testStringArray() throws Exception
2539
{
2640
// 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);
3042

3143
// 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);
3545

3646
// 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);
4048
}
4149

42-
private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
50+
private void _stringArrayRoundtrip(ObjectMapper mapper, boolean shouldBeNull) throws Exception
4351
{
44-
String[] array = new String[] {"", "test", null, "test2"};
45-
4652
// serialize to string
47-
String xml = MAPPER.writeValueAsString(array);
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]);
@@ -62,35 +68,78 @@ private void stringArrayRoundtrip(boolean shouldBeNull) throws Exception
6268
assertEquals("test2", result[3]);
6369
}
6470

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+
65120
@Test
66121
public void testStringList() throws Exception
67122
{
68123
// 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);
72125

73126
// 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);
77128

78129
// 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);
82131
}
83132

84-
private void stringListRoundtrip(boolean shouldBeNull) throws Exception
133+
private void _stringListRoundtrip(ObjectMapper mapper, boolean shouldBeNull) throws Exception
85134
{
86-
List<String> list = asList("", "test", null, "test2");
135+
List<String> list = asList(TEST_DATA);
87136

88137
// serialize to string
89-
String xml = MAPPER.writeValueAsString(list);
138+
String xml = mapper.writeValueAsString(list);
90139
assertNotNull(xml);
91140

92141
// 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>>() {});
94143
assertEquals(4, result.size());
95144
assertEquals("", result.get(0));
96145
assertEquals("test", result.get(1));
@@ -105,25 +154,68 @@ private void stringListRoundtrip(boolean shouldBeNull) throws Exception
105154
}
106155

107156
@Test
108-
public void testStringMap() throws Exception
157+
public void testStringListPojo() throws Exception
109158
{
110159
// 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);
114161

115162
// 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);
119164

120165
// 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);
124167
}
125168

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
127219
{
128220
Map<String, String> map = new HashMap<String, String>() {{
129221
put("a", "");
@@ -132,14 +224,14 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
132224
put("d", "test2");
133225
}};
134226
MapPojo mapPojo = new MapPojo();
135-
mapPojo.setMap( map );
227+
mapPojo.setMap(map);
136228

137229
// serialize to string
138-
String xml = MAPPER.writeValueAsString(mapPojo);
230+
String xml = mapper.writeValueAsString(mapPojo);
139231
assertNotNull(xml);
140232

141233
// then bring it back
142-
MapPojo result = MAPPER.readValue(xml, MapPojo.class);
234+
MapPojo result = mapper.readValue(xml, MapPojo.class);
143235
assertEquals(4, result.map.size());
144236
assertEquals("", result.map.get("a"));
145237
assertEquals("test", result.map.get("b"));
@@ -153,11 +245,8 @@ private void stringMapRoundtrip(boolean shouldBeNull) throws Exception
153245
assertEquals("test2", result.map.get("d"));
154246
}
155247

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;
161250

162251
public Map<String, String> getMap() {
163252
return map;
@@ -167,4 +256,75 @@ public void setMap(Map<String, String> map) {
167256
this.map = map;
168257
}
169258
}
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+
}
170330
}

0 commit comments

Comments
 (0)