Skip to content

Commit 9951c43

Browse files
committed
Add @JsonSetter tests wrt #285
1 parent ce20b7f commit 9951c43

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed

src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java

+27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import junit.framework.TestCase;
77

88
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
9+
910
import com.fasterxml.jackson.core.*;
11+
1012
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
1113

1214
public abstract class XmlTestBase
@@ -138,6 +140,31 @@ public IntWrapper(int value) {
138140
i = value;
139141
}
140142
}
143+
144+
// since 2.9.6
145+
public static class Point {
146+
public int x, y;
147+
148+
protected Point() { } // for deser
149+
public Point(int x0, int y0) {
150+
x = x0;
151+
y = y0;
152+
}
153+
154+
@Override
155+
public boolean equals(Object o) {
156+
if (!(o instanceof Point)) {
157+
return false;
158+
}
159+
Point other = (Point) o;
160+
return (other.x == x) && (other.y == y);
161+
}
162+
163+
@Override
164+
public String toString() {
165+
return String.format("[x=%d, y=%d]", x, y);
166+
}
167+
}
141168

142169
/*
143170
/**********************************************************
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.fasterxml.jackson.dataformat.xml.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonSetter;
4+
import com.fasterxml.jackson.annotation.Nulls;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
9+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
10+
11+
// Copied from `com.fasterxml.jackson.databind.deser.filter.` in `jackson-databind`
12+
public class NullConversionsGenericTest extends XmlTestBase
13+
{
14+
static class PointWrapper {
15+
@JsonSetter(nulls=Nulls.AS_EMPTY)
16+
public Point p;
17+
}
18+
19+
static class GeneralEmpty<T> {
20+
// 09-Feb-2017, tatu: Should only need annotation either for field OR setter, not both:
21+
// @JsonSetter(nulls=Nulls.AS_EMPTY)
22+
T value;
23+
24+
protected GeneralEmpty() { }
25+
public GeneralEmpty(T v) { value = v; }
26+
27+
@JsonSetter(nulls=Nulls.AS_EMPTY)
28+
public void setValue(T v) {
29+
value = v;
30+
}
31+
32+
public T getValue() { return value; }
33+
}
34+
35+
static class NoCtorWrapper {
36+
@JsonSetter(nulls=Nulls.AS_EMPTY)
37+
public NoCtorPOJO value;
38+
}
39+
40+
static class NoCtorPOJO {
41+
public NoCtorPOJO(boolean b) { }
42+
}
43+
44+
/*
45+
/**********************************************************
46+
/* Test methods
47+
/**********************************************************
48+
*/
49+
50+
private final XmlMapper MAPPER = newMapper();
51+
52+
private final static String EMPTY_XML = "<GeneralEmpty><value /></GeneralEmpty>";
53+
54+
public void testNullsToEmptyPojo() throws Exception
55+
{
56+
PointWrapper pw = MAPPER.readValue("<PointWrapper><p /></PointWrapper>",
57+
PointWrapper.class);
58+
assertNotNull(pw);
59+
assertNotNull(pw.p);
60+
assertEquals(0, pw.p.x);
61+
assertEquals(0, pw.p.y);
62+
}
63+
64+
public void testNullsToGenericPojo() throws Exception
65+
{
66+
String xml = MAPPER.writeValueAsString(new GeneralEmpty<Point>(new Point(1, 2)));
67+
GeneralEmpty<Point> result = MAPPER.readValue(EMPTY_XML,
68+
new TypeReference<GeneralEmpty<Point>>() { });
69+
assertNotNull(result.value);
70+
Point p = result.value;
71+
assertEquals(0, p.x);
72+
assertEquals(0, p.y);
73+
74+
// and then also failing case with no suitable creator:
75+
try {
76+
/* NoCtorWrapper nogo =*/ MAPPER.readValue(EMPTY_XML,
77+
NoCtorWrapper.class);
78+
fail("Should not pass");
79+
} catch (JsonMappingException e) {
80+
verifyException(e, "Cannot create empty instance");
81+
}
82+
}
83+
84+
// 04-May-2018, tatu: In theory could be supportable, but wrapping (or not)
85+
// of Collections, other requirements, make it... not that easy.
86+
/*
87+
public void testNullsToEmptyCollection() throws Exception
88+
{
89+
GeneralEmpty<List<String>> result = MAPPER.readValue(EMPTY_XML,
90+
new TypeReference<GeneralEmpty<List<String>>>() { });
91+
assertNotNull(result.value);
92+
assertEquals(0, result.value.size());
93+
94+
// but also non-String type, since impls vary
95+
GeneralEmpty<List<Integer>> result2 = MAPPER.readValue(EMPTY_XML,
96+
new TypeReference<GeneralEmpty<List<Integer>>>() { });
97+
assertNotNull(result2.value);
98+
assertEquals(0, result2.value.size());
99+
}
100+
*/
101+
102+
// 04-May-2018, tatu: Maps and XML do not mix well, alas:
103+
/*
104+
public void testNullsToEmptyMap() throws Exception
105+
{
106+
GeneralEmpty<Map<String,String>> result = MAPPER.readValue(EMPTY_XML,
107+
new TypeReference<GeneralEmpty<Map<String,String>>>() { });
108+
assertNotNull(result.value);
109+
assertEquals(0, result.value.size());
110+
}
111+
*/
112+
113+
public void testNullsToEmptyArrays() throws Exception
114+
{
115+
final String doc = EMPTY_XML;
116+
117+
GeneralEmpty<Object[]> result = MAPPER.readValue(doc,
118+
new TypeReference<GeneralEmpty<Object[]>>() { });
119+
assertNotNull(result.value);
120+
assertEquals(0, result.value.length);
121+
122+
GeneralEmpty<String[]> result2 = MAPPER.readValue(doc,
123+
new TypeReference<GeneralEmpty<String[]>>() { });
124+
assertNotNull(result2.value);
125+
assertEquals(0, result2.value.length);
126+
127+
GeneralEmpty<int[]> result3 = MAPPER.readValue(doc,
128+
new TypeReference<GeneralEmpty<int[]>>() { });
129+
assertNotNull(result3.value);
130+
assertEquals(0, result3.value.length);
131+
132+
GeneralEmpty<double[]> result4 = MAPPER.readValue(doc,
133+
new TypeReference<GeneralEmpty<double[]>>() { });
134+
assertNotNull(result4.value);
135+
assertEquals(0, result4.value.length);
136+
137+
GeneralEmpty<boolean[]> result5 = MAPPER.readValue(doc,
138+
new TypeReference<GeneralEmpty<boolean[]>>() { });
139+
assertNotNull(result5.value);
140+
assertEquals(0, result5.value.length);
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.fasterxml.jackson.dataformat.xml.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonSetter;
4+
import com.fasterxml.jackson.annotation.Nulls;
5+
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
7+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
8+
9+
// for [databind#1402]; configurable null handling, specifically with SKIP
10+
public class NullConversionsSkipTest extends XmlTestBase
11+
{
12+
static class NullSkipField {
13+
public String nullsOk = "a";
14+
15+
@JsonSetter(nulls=Nulls.SKIP)
16+
public String noNulls = "b";
17+
}
18+
19+
static class NullSkipMethod {
20+
String _nullsOk = "a";
21+
String _noNulls = "b";
22+
23+
public void setNullsOk(String v) {
24+
_nullsOk = v;
25+
}
26+
27+
@JsonSetter(nulls=Nulls.SKIP)
28+
public void setNoNulls(String v) {
29+
_noNulls = v;
30+
}
31+
}
32+
33+
static class StringValue {
34+
String value = "default";
35+
36+
public void setValue(String v) {
37+
value = v;
38+
}
39+
}
40+
41+
/*
42+
/**********************************************************
43+
/* Test methods, straight annotation
44+
/**********************************************************
45+
*/
46+
47+
private final XmlMapper MAPPER = newMapper();
48+
49+
public void testSkipNullField1() throws Exception
50+
{
51+
// first, ok if assigning non-null to not-nullable, null for nullable
52+
NullSkipField result = MAPPER.readValue(
53+
//"<NullSkipField><noNulls>foo</noNulls><nullsOk></nullsOk></NullSkipField>",
54+
"<NullSkipField><noNulls>foo</noNulls><nullsOk /></NullSkipField>",
55+
NullSkipField.class);
56+
assertEquals("foo", result.noNulls);
57+
assertNull(result.nullsOk);
58+
}
59+
60+
public void testSkipNullField2() throws Exception
61+
{
62+
// and then see that nulls are not ok for non-nullable
63+
NullSkipField result = MAPPER.readValue("<NullSkipField><noNulls /></NullSkipField>",
64+
NullSkipField.class);
65+
assertEquals("b", result.noNulls);
66+
assertEquals("a", result.nullsOk);
67+
}
68+
69+
public void testSkipNullMethod1() throws Exception
70+
{
71+
NullSkipMethod result = MAPPER.readValue(
72+
//"<NullSkipMethod><noNulls>foo<noNulls><nullsOk></nullsOk></NullSkipMethod>",
73+
"<NullSkipMethod><noNulls>foo</noNulls><nullsOk /></NullSkipMethod>",
74+
NullSkipMethod.class);
75+
assertEquals("foo", result._noNulls);
76+
assertNull(result._nullsOk);
77+
}
78+
79+
public void testSkipNullMethod2() throws Exception
80+
{
81+
NullSkipMethod result = MAPPER.readValue("<NullSkipMethod><noNulls /></NullSkipMethod>",
82+
NullSkipMethod.class);
83+
assertEquals("b", result._noNulls);
84+
assertEquals("a", result._nullsOk);
85+
}
86+
87+
/*
88+
/**********************************************************
89+
/* Test methods, defaulting
90+
/**********************************************************
91+
*/
92+
93+
public void testSkipNullWithDefaults() throws Exception
94+
{
95+
// String doc = "<StringValue><value></value></StringValue>";
96+
String doc = "<StringValue><value /></StringValue>";
97+
StringValue result = MAPPER.readValue(doc, StringValue.class);
98+
assertNull(result.value);
99+
100+
ObjectMapper mapper = newMapper();
101+
mapper.configOverride(String.class)
102+
.setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.SKIP));
103+
result = mapper.readValue(doc, StringValue.class);
104+
assertEquals("default", result.value);
105+
}
106+
}

0 commit comments

Comments
 (0)