|
| 1 | +package com.fasterxml.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.core.JacksonException; |
| 13 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 14 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 15 | + |
| 16 | +import jakarta.xml.bind.annotation.XmlAccessType; |
| 17 | +import jakarta.xml.bind.annotation.XmlAccessorType; |
| 18 | +import jakarta.xml.bind.annotation.XmlAnyElement; |
| 19 | +import jakarta.xml.bind.annotation.XmlAttribute; |
| 20 | +import jakarta.xml.bind.annotation.XmlElement; |
| 21 | +import jakarta.xml.bind.annotation.XmlMixed; |
| 22 | +import jakarta.xml.bind.annotation.XmlRootElement; |
| 23 | +import jakarta.xml.bind.annotation.XmlType; |
| 24 | + |
| 25 | +public class UnexpectedNonWhitespaceText509Test { |
| 26 | + @XmlRootElement(name = "data") |
| 27 | + @XmlAccessorType(XmlAccessType.FIELD) |
| 28 | + @XmlType(name = "Data", propOrder = { "key", "content" }) |
| 29 | + public static class Data { |
| 30 | + @XmlMixed |
| 31 | + @XmlAnyElement(lax = true) |
| 32 | + protected List<Object> content; |
| 33 | + @XmlAttribute(name = "key", required = true) |
| 34 | + protected java.lang.String key; |
| 35 | + |
| 36 | + public List<Object> getContent() { |
| 37 | + if (content == null) { |
| 38 | + content = new ArrayList<>(); |
| 39 | + } |
| 40 | + return this.content; |
| 41 | + } |
| 42 | + |
| 43 | + public void setContent(List<Object> content) { |
| 44 | + this.content = content; |
| 45 | + } |
| 46 | + |
| 47 | + public java.lang.String getKey() { |
| 48 | + return key; |
| 49 | + } |
| 50 | + |
| 51 | + public void setKey(java.lang.String value) { |
| 52 | + this.key = value; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @XmlRootElement(name = "metaData") |
| 57 | + @XmlAccessorType(XmlAccessType.FIELD) |
| 58 | + @XmlType(name = "MetaData", propOrder = { "data" }) |
| 59 | + public static class MetaData { |
| 60 | + @XmlElement(required = true) |
| 61 | + protected List<Data> data; |
| 62 | + |
| 63 | + public List<Data> getData() { |
| 64 | + if (data == null) { |
| 65 | + data = new ArrayList<>(); |
| 66 | + } |
| 67 | + return this.data; |
| 68 | + } |
| 69 | + |
| 70 | + public void setData(List<Data> data) { |
| 71 | + this.data = data; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + return Objects.toString(data); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testDeSerData() { |
| 82 | + Data value = deSer("<data key=\"MadeWith\">Text Editor</data>", Data.class); |
| 83 | + assertEquals("\"key\" attribute not correctly deserialized", value.getKey(), "MadeWith"); |
| 84 | + assertEquals("\"content\" not correctly deserialized", value.getContent(), List.of("Text Editor")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testDeSerMetaData() { |
| 89 | + MetaData value = deSer("<metaData>\n" // |
| 90 | + + " <data key=\"MadeWith\">Text Editor</data>\n" // |
| 91 | + + " <data key=\"Version\">1.0.0</data>\n" // |
| 92 | + + "</metaData>", MetaData.class); |
| 93 | + List<Data> entries = value.getData(); |
| 94 | + assertEquals("\"data\" not correctly deserialized", entries.size(), 2); |
| 95 | + Data entry = entries.get(0); |
| 96 | + assertEquals("\"key\" attribute not correctly deserialized", entry.getKey(), "MadeWith"); |
| 97 | + assertEquals("\"content\" not correctly deserialized", entry.getContent(), List.of("Text Editor")); |
| 98 | + entry = entries.get(1); |
| 99 | + assertEquals("\"key\" attribute not correctly deserialized", entry.getKey(), "Version"); |
| 100 | + assertEquals("\"content\" not correctly deserialized", entry.getContent(), List.of("1.0.0")); |
| 101 | + } |
| 102 | + |
| 103 | + private <T> T deSer(String xmlString, Class<T> clazz) { |
| 104 | + try { |
| 105 | + XmlMapper objectMapper = new XmlMapper(); |
| 106 | + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 107 | + return objectMapper.readValue(xmlString, clazz); |
| 108 | + } catch (JacksonException e) { |
| 109 | + fail(e.getMessage()); |
| 110 | + return null; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments