|
| 1 | +package tools.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 8 | + |
| 9 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 10 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 11 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +// [dataformat-xml#561] Empty timestamp/date attribute should deserialize as null |
| 16 | +public class EmptyTimestampDeser561Test extends XmlTestUtil |
| 17 | +{ |
| 18 | + static class DateAttrBean { |
| 19 | + @JacksonXmlProperty(isAttribute = true) |
| 20 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| 21 | + public Date timestampDate; |
| 22 | + } |
| 23 | + |
| 24 | + static class DateElementBean { |
| 25 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| 26 | + public Date timestampDate; |
| 27 | + } |
| 28 | + |
| 29 | + static class DateBean { |
| 30 | + @JacksonXmlProperty(isAttribute = true) |
| 31 | + @JsonFormat(pattern = "yyyy-MM-dd") |
| 32 | + public Date dateValue; |
| 33 | + } |
| 34 | + |
| 35 | + private final XmlMapper MAPPER = newMapper(); |
| 36 | + |
| 37 | + // [dataformat-xml#561]: empty attribute |
| 38 | + @Test |
| 39 | + public void testEmptyDateAttributeWithTimestampFormat() throws Exception |
| 40 | + { |
| 41 | + DateAttrBean result = MAPPER.readValue( |
| 42 | + "<DateAttrBean timestampDate=\"\" />", |
| 43 | + DateAttrBean.class); |
| 44 | + assertNotNull(result); |
| 45 | + assertNull(result.timestampDate); |
| 46 | + } |
| 47 | + |
| 48 | + // [dataformat-xml#561]: empty element |
| 49 | + @Test |
| 50 | + public void testEmptyDateElement() throws Exception |
| 51 | + { |
| 52 | + DateElementBean result = MAPPER.readValue( |
| 53 | + "<DateElementBean><timestampDate></timestampDate></DateElementBean>", |
| 54 | + DateElementBean.class); |
| 55 | + assertNotNull(result); |
| 56 | + assertNull(result.timestampDate); |
| 57 | + } |
| 58 | + |
| 59 | + // [dataformat-xml#561]: empty Date attribute |
| 60 | + @Test |
| 61 | + public void testEmptyDateAttribute() throws Exception |
| 62 | + { |
| 63 | + DateBean result = MAPPER.readValue( |
| 64 | + "<DateBean dateValue=\"\" />", |
| 65 | + DateBean.class); |
| 66 | + assertNotNull(result); |
| 67 | + assertNull(result.dateValue); |
| 68 | + } |
| 69 | +} |
0 commit comments