From 89c0bc72b284c49ab48516c8bdb232d490f01442 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 3 May 2021 20:23:42 -0700 Subject: [PATCH] Add a test for #473 --- .../xml/deser/EmptyWithScalarsTest.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/dataformat/xml/deser/EmptyWithScalarsTest.java diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/EmptyWithScalarsTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/EmptyWithScalarsTest.java new file mode 100644 index 000000000..1c745ecae --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/EmptyWithScalarsTest.java @@ -0,0 +1,132 @@ +package com.fasterxml.jackson.dataformat.xml.deser; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +// [dataformat-xml#473]: 2.12 coercion of empty to "default" +public class EmptyWithScalarsTest extends XmlTestBase +{ + @JacksonXmlRootElement(localName = "w") + static class NumbersPrimitive { + public int i = 1; + public long l = 2L; + + public double d = 0.5; + public float f = 0.25f; + } + + @JacksonXmlRootElement(localName = "w") + static class NumbersWrapper { + public Integer I = Integer.valueOf(1); + public Long L = Long.valueOf(1L); + + public Double D = Double.valueOf(0.5); + public Float F = Float.valueOf(0.5f); + } + + @JacksonXmlRootElement(localName = "w") + static class NumbersOther { + public BigInteger bi = BigInteger.ONE; + public BigDecimal bd = BigDecimal.ONE; + } + + @JacksonXmlRootElement(localName = "w") + static class MiscOther { + public Boolean B = Boolean.TRUE; + } + + private final XmlMapper MAPPER = newMapper(); + + /* + /********************************************************************** + /* Test methods, Numbers / primitive + /********************************************************************** + */ + + public void testPrimitiveIntsWithEmpty() throws Exception + { + NumbersPrimitive p = MAPPER.readValue(_emptyWrapped("i"), + NumbersPrimitive.class); + assertEquals(0, p.i); + p = MAPPER.readValue(_emptyWrapped("l"), + NumbersPrimitive.class); + assertEquals(0L, p.l); + } + + public void testPrimitiveFPsWithEmpty() throws Exception + { + NumbersPrimitive p = MAPPER.readValue(_emptyWrapped("d"), + NumbersPrimitive.class); + assertEquals(0d, p.d); + p = MAPPER.readValue(_emptyWrapped("f"), + NumbersPrimitive.class); + assertEquals(0f, p.f); + } + + /* + /********************************************************************** + /* Test methods, Numbers / wrapper (or Object) + /********************************************************************** + */ + + public void testIntegralsWithEmpty() throws Exception + { + NumbersWrapper w = MAPPER.readValue(_emptyWrapped("I"), + NumbersWrapper.class); + assertNull(w.I); + w = MAPPER.readValue(_emptyWrapped("L"), + NumbersWrapper.class); + assertNull(w.L); + + NumbersOther o = MAPPER.readValue(_emptyWrapped("bi"), + NumbersOther.class); + assertNull(o.bi); + } + + public void testFPWithEmpty() throws Exception + { + NumbersWrapper w = MAPPER.readValue(_emptyWrapped("D"), + NumbersWrapper.class); + assertNull(w.D); + w = MAPPER.readValue(_emptyWrapped("F"), + NumbersWrapper.class); + assertNull(w.F); + + NumbersOther o = MAPPER.readValue(_emptyWrapped("bd"), + NumbersOther.class); + assertNull(o.bd); + } + + /* + /********************************************************************** + /* Test methods, otber Scalars + /********************************************************************** + */ + + public void testOtherScalarWithEmpty() throws Exception + { + MiscOther o = MAPPER.readValue(_emptyWrapped("B"), + MiscOther.class); + assertNull(o.B); + } + + /* + /********************************************************************** + /* Internal methods + /********************************************************************** + */ + + private String _emptyWrapped(String name) { + return _simpleWrapped(name, ""); + } + + private String _simpleWrapped(String name, String value) { + return "\n" + +"<"+name+">"+value+"\n" + +"\n"; + } +}