|
| 1 | +package com.fasterxml.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.math.BigInteger; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 7 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 9 | + |
| 10 | +// [dataformat-xml#473]: 2.12 coercion of empty to "default" |
| 11 | +public class EmptyWithScalarsTest extends XmlTestBase |
| 12 | +{ |
| 13 | + @JacksonXmlRootElement(localName = "w") |
| 14 | + static class NumbersPrimitive { |
| 15 | + public int i = 1; |
| 16 | + public long l = 2L; |
| 17 | + |
| 18 | + public double d = 0.5; |
| 19 | + public float f = 0.25f; |
| 20 | + } |
| 21 | + |
| 22 | + @JacksonXmlRootElement(localName = "w") |
| 23 | + static class NumbersWrapper { |
| 24 | + public Integer I = Integer.valueOf(1); |
| 25 | + public Long L = Long.valueOf(1L); |
| 26 | + |
| 27 | + public Double D = Double.valueOf(0.5); |
| 28 | + public Float F = Float.valueOf(0.5f); |
| 29 | + } |
| 30 | + |
| 31 | + @JacksonXmlRootElement(localName = "w") |
| 32 | + static class NumbersOther { |
| 33 | + public BigInteger bi = BigInteger.ONE; |
| 34 | + public BigDecimal bd = BigDecimal.ONE; |
| 35 | + } |
| 36 | + |
| 37 | + @JacksonXmlRootElement(localName = "w") |
| 38 | + static class MiscOther { |
| 39 | + public Boolean B = Boolean.TRUE; |
| 40 | + } |
| 41 | + |
| 42 | + private final XmlMapper MAPPER = newMapper(); |
| 43 | + |
| 44 | + /* |
| 45 | + /********************************************************************** |
| 46 | + /* Test methods, Numbers / primitive |
| 47 | + /********************************************************************** |
| 48 | + */ |
| 49 | + |
| 50 | + public void testPrimitiveIntsWithEmpty() throws Exception |
| 51 | + { |
| 52 | + NumbersPrimitive p = MAPPER.readValue(_emptyWrapped("i"), |
| 53 | + NumbersPrimitive.class); |
| 54 | + assertEquals(0, p.i); |
| 55 | + p = MAPPER.readValue(_emptyWrapped("l"), |
| 56 | + NumbersPrimitive.class); |
| 57 | + assertEquals(0L, p.l); |
| 58 | + } |
| 59 | + |
| 60 | + public void testPrimitiveFPsWithEmpty() throws Exception |
| 61 | + { |
| 62 | + NumbersPrimitive p = MAPPER.readValue(_emptyWrapped("d"), |
| 63 | + NumbersPrimitive.class); |
| 64 | + assertEquals(0d, p.d); |
| 65 | + p = MAPPER.readValue(_emptyWrapped("f"), |
| 66 | + NumbersPrimitive.class); |
| 67 | + assertEquals(0f, p.f); |
| 68 | + } |
| 69 | + |
| 70 | + /* |
| 71 | + /********************************************************************** |
| 72 | + /* Test methods, Numbers / wrapper (or Object) |
| 73 | + /********************************************************************** |
| 74 | + */ |
| 75 | + |
| 76 | + public void testIntegralsWithEmpty() throws Exception |
| 77 | + { |
| 78 | + NumbersWrapper w = MAPPER.readValue(_emptyWrapped("I"), |
| 79 | + NumbersWrapper.class); |
| 80 | + assertNull(w.I); |
| 81 | + w = MAPPER.readValue(_emptyWrapped("L"), |
| 82 | + NumbersWrapper.class); |
| 83 | + assertNull(w.L); |
| 84 | + |
| 85 | + NumbersOther o = MAPPER.readValue(_emptyWrapped("bi"), |
| 86 | + NumbersOther.class); |
| 87 | + assertNull(o.bi); |
| 88 | + } |
| 89 | + |
| 90 | + public void testFPWithEmpty() throws Exception |
| 91 | + { |
| 92 | + NumbersWrapper w = MAPPER.readValue(_emptyWrapped("D"), |
| 93 | + NumbersWrapper.class); |
| 94 | + assertNull(w.D); |
| 95 | + w = MAPPER.readValue(_emptyWrapped("F"), |
| 96 | + NumbersWrapper.class); |
| 97 | + assertNull(w.F); |
| 98 | + |
| 99 | + NumbersOther o = MAPPER.readValue(_emptyWrapped("bd"), |
| 100 | + NumbersOther.class); |
| 101 | + assertNull(o.bd); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + /********************************************************************** |
| 106 | + /* Test methods, otber Scalars |
| 107 | + /********************************************************************** |
| 108 | + */ |
| 109 | + |
| 110 | + public void testOtherScalarWithEmpty() throws Exception |
| 111 | + { |
| 112 | + MiscOther o = MAPPER.readValue(_emptyWrapped("B"), |
| 113 | + MiscOther.class); |
| 114 | + assertNull(o.B); |
| 115 | + } |
| 116 | + |
| 117 | + /* |
| 118 | + /********************************************************************** |
| 119 | + /* Internal methods |
| 120 | + /********************************************************************** |
| 121 | + */ |
| 122 | + |
| 123 | + private String _emptyWrapped(String name) { |
| 124 | + return _simpleWrapped(name, ""); |
| 125 | + } |
| 126 | + |
| 127 | + private String _simpleWrapped(String name, String value) { |
| 128 | + return "<w>\n" |
| 129 | + +"<"+name+">"+value+"</"+name+">\n" |
| 130 | + +"</w>\n"; |
| 131 | + } |
| 132 | +} |
0 commit comments