|
| 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 | +} |
0 commit comments