|
| 1 | +package tools.jackson.dataformat.xml.stream; |
| 2 | + |
| 3 | +import javax.xml.namespace.QName; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import tools.jackson.core.*; |
| 8 | +import tools.jackson.databind.*; |
| 9 | +import tools.jackson.databind.annotation.JsonDeserialize; |
| 10 | + |
| 11 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 12 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 13 | +import tools.jackson.dataformat.xml.deser.FromXmlParser; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +// [dataformat-xml#496] Root element name not accessible from custom deserializer |
| 18 | +// when root has no attributes |
| 19 | +public class RootElementName496Test extends XmlTestUtil |
| 20 | +{ |
| 21 | + @JsonDeserialize(using = RootNameDeserializer.class) |
| 22 | + static class RootNameHolder { |
| 23 | + public QName rootName; |
| 24 | + |
| 25 | + RootNameHolder(QName rootName) { |
| 26 | + this.rootName = rootName; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static class RootNameDeserializer extends ValueDeserializer<RootNameHolder> { |
| 31 | + @Override |
| 32 | + public RootNameHolder deserialize(JsonParser p, DeserializationContext ctxt) |
| 33 | + { |
| 34 | + QName rootName = ((FromXmlParser) p).getRootElementName(); |
| 35 | + // consume the rest |
| 36 | + while (p.nextToken() != null) { } |
| 37 | + return new RootNameHolder(rootName); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private final XmlMapper MAPPER = newMapper(); |
| 42 | + |
| 43 | + // [dataformat-xml#496]: root name accessible without attributes |
| 44 | + @Test |
| 45 | + public void testRootNameWithoutAttributes() throws Exception |
| 46 | + { |
| 47 | + RootNameHolder result = MAPPER.readValue( |
| 48 | + "<root><field>value</field></root>", RootNameHolder.class); |
| 49 | + assertEquals("root", result.rootName.getLocalPart()); |
| 50 | + } |
| 51 | + |
| 52 | + // [dataformat-xml#496]: root name accessible with attributes |
| 53 | + @Test |
| 54 | + public void testRootNameWithAttributes() throws Exception |
| 55 | + { |
| 56 | + RootNameHolder result = MAPPER.readValue( |
| 57 | + "<root foo='bar'><field>value</field></root>", RootNameHolder.class); |
| 58 | + assertEquals("root", result.rootName.getLocalPart()); |
| 59 | + } |
| 60 | + |
| 61 | + // [dataformat-xml#496]: verify via parser directly, stable across full parse |
| 62 | + @Test |
| 63 | + public void testRootNameViaParser() throws Exception |
| 64 | + { |
| 65 | + try (JsonParser p = MAPPER.createParser("<myRoot><child>text</child></myRoot>")) { |
| 66 | + FromXmlParser xp = (FromXmlParser) p; |
| 67 | + QName rootName = xp.getRootElementName(); |
| 68 | + assertEquals("myRoot", rootName.getLocalPart()); |
| 69 | + // Advance past all tokens |
| 70 | + while (p.nextToken() != null) { } |
| 71 | + // Still accessible after parsing |
| 72 | + assertEquals("myRoot", xp.getRootElementName().getLocalPart()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // [dataformat-xml#496]: empty root element |
| 77 | + @Test |
| 78 | + public void testRootNameEmptyElement() throws Exception |
| 79 | + { |
| 80 | + try (JsonParser p = MAPPER.createParser("<emptyRoot/>")) { |
| 81 | + FromXmlParser xp = (FromXmlParser) p; |
| 82 | + assertEquals("emptyRoot", xp.getRootElementName().getLocalPart()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // [dataformat-xml#496]: root with text-only content (scalar root value) |
| 87 | + @Test |
| 88 | + public void testRootNameTextOnly() throws Exception |
| 89 | + { |
| 90 | + try (JsonParser p = MAPPER.createParser("<textRoot>hello</textRoot>")) { |
| 91 | + FromXmlParser xp = (FromXmlParser) p; |
| 92 | + assertEquals("textRoot", xp.getRootElementName().getLocalPart()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // [dataformat-xml#496]: root with namespace — verify all QName components |
| 97 | + @Test |
| 98 | + public void testRootNameWithNamespace() throws Exception |
| 99 | + { |
| 100 | + try (JsonParser p = MAPPER.createParser( |
| 101 | + "<ns:root xmlns:ns='http://example.com'><ns:child>val</ns:child></ns:root>")) { |
| 102 | + FromXmlParser xp = (FromXmlParser) p; |
| 103 | + QName rootName = xp.getRootElementName(); |
| 104 | + assertEquals("root", rootName.getLocalPart()); |
| 105 | + assertEquals("http://example.com", rootName.getNamespaceURI()); |
| 106 | + assertEquals("ns", rootName.getPrefix()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // [dataformat-xml#496]: root with default namespace (no prefix) |
| 111 | + @Test |
| 112 | + public void testRootNameWithDefaultNamespace() throws Exception |
| 113 | + { |
| 114 | + try (JsonParser p = MAPPER.createParser( |
| 115 | + "<root xmlns='http://example.com'><child>val</child></root>")) { |
| 116 | + FromXmlParser xp = (FromXmlParser) p; |
| 117 | + QName rootName = xp.getRootElementName(); |
| 118 | + assertEquals("root", rootName.getLocalPart()); |
| 119 | + assertEquals("http://example.com", rootName.getNamespaceURI()); |
| 120 | + assertEquals("", rootName.getPrefix()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // [dataformat-xml#496]: root with multiple children (no attributes) |
| 125 | + @Test |
| 126 | + public void testRootNameMultipleChildren() throws Exception |
| 127 | + { |
| 128 | + RootNameHolder result = MAPPER.readValue( |
| 129 | + "<document><a>1</a><b>2</b><c>3</c></document>", RootNameHolder.class); |
| 130 | + assertEquals("document", result.rootName.getLocalPart()); |
| 131 | + } |
| 132 | +} |
0 commit comments