|
| 1 | +package tools.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonRootName; |
| 6 | + |
| 7 | +import tools.jackson.databind.DatabindException; |
| 8 | + |
| 9 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 10 | +import tools.jackson.dataformat.xml.XmlReadFeature; |
| 11 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 12 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for [dataformat-xml#247]: verification of root element name |
| 18 | + * against expected (from annotation or class name) when |
| 19 | + * {@link XmlReadFeature#ENFORCE_ROOT_ELEMENT_NAME} is enabled. |
| 20 | + */ |
| 21 | +public class RootElementNameValidation247Test extends XmlTestUtil |
| 22 | +{ |
| 23 | + static class Root { |
| 24 | + public int value; |
| 25 | + } |
| 26 | + |
| 27 | + @JsonRootName("MyRoot") |
| 28 | + static class AnnotatedRoot { |
| 29 | + public int value; |
| 30 | + } |
| 31 | + |
| 32 | + @JacksonXmlRootElement(localName = "XmlRoot") |
| 33 | + static class XmlAnnotatedRoot { |
| 34 | + public int value; |
| 35 | + } |
| 36 | + |
| 37 | + @JacksonXmlRootElement(localName = "NsRoot", namespace = "http://example.com/test") |
| 38 | + static class NamespacedRoot { |
| 39 | + public int value; |
| 40 | + } |
| 41 | + |
| 42 | + private final XmlMapper ENFORCING_MAPPER = XmlMapper.builder() |
| 43 | + .enable(XmlReadFeature.ENFORCE_ROOT_ELEMENT_NAME) |
| 44 | + .build(); |
| 45 | + |
| 46 | + private final XmlMapper DEFAULT_MAPPER = newMapper(); |
| 47 | + |
| 48 | + // Matching class name should pass |
| 49 | + @Test |
| 50 | + public void testMatchingClassNameSucceeds() throws Exception |
| 51 | + { |
| 52 | + Root root = ENFORCING_MAPPER.readValue( |
| 53 | + "<Root><value>42</value></Root>", Root.class); |
| 54 | + assertEquals(42, root.value); |
| 55 | + } |
| 56 | + |
| 57 | + // Mismatched root name should fail when feature is enabled |
| 58 | + @Test |
| 59 | + public void testMismatchedNameFails() throws Exception |
| 60 | + { |
| 61 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 62 | + ENFORCING_MAPPER.readValue( |
| 63 | + "<Boot><value>42</value></Boot>", Root.class)); |
| 64 | + verifyException(e, "Root name"); |
| 65 | + verifyException(e, "Boot"); |
| 66 | + verifyException(e, "Root"); |
| 67 | + } |
| 68 | + |
| 69 | + // Mismatched root name should succeed when feature is disabled (default) |
| 70 | + @Test |
| 71 | + public void testMismatchedNameSucceedsWhenDisabled() throws Exception |
| 72 | + { |
| 73 | + Root root = DEFAULT_MAPPER.readValue( |
| 74 | + "<Boot><value>42</value></Boot>", Root.class); |
| 75 | + assertEquals(42, root.value); |
| 76 | + } |
| 77 | + |
| 78 | + // @JsonRootName annotation should be used for expected name |
| 79 | + @Test |
| 80 | + public void testJsonRootNameAnnotation() throws Exception |
| 81 | + { |
| 82 | + AnnotatedRoot root = ENFORCING_MAPPER.readValue( |
| 83 | + "<MyRoot><value>42</value></MyRoot>", AnnotatedRoot.class); |
| 84 | + assertEquals(42, root.value); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testJsonRootNameAnnotationMismatch() throws Exception |
| 89 | + { |
| 90 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 91 | + ENFORCING_MAPPER.readValue( |
| 92 | + "<AnnotatedRoot><value>42</value></AnnotatedRoot>", |
| 93 | + AnnotatedRoot.class)); |
| 94 | + verifyException(e, "Root name"); |
| 95 | + verifyException(e, "AnnotatedRoot"); |
| 96 | + verifyException(e, "MyRoot"); |
| 97 | + } |
| 98 | + |
| 99 | + // @JacksonXmlRootElement annotation should be used for expected name |
| 100 | + @Test |
| 101 | + public void testJacksonXmlRootElementAnnotation() throws Exception |
| 102 | + { |
| 103 | + XmlAnnotatedRoot root = ENFORCING_MAPPER.readValue( |
| 104 | + "<XmlRoot><value>42</value></XmlRoot>", XmlAnnotatedRoot.class); |
| 105 | + assertEquals(42, root.value); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testJacksonXmlRootElementAnnotationMismatch() throws Exception |
| 110 | + { |
| 111 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 112 | + ENFORCING_MAPPER.readValue( |
| 113 | + "<WrongName><value>42</value></WrongName>", |
| 114 | + XmlAnnotatedRoot.class)); |
| 115 | + verifyException(e, "Root name"); |
| 116 | + verifyException(e, "WrongName"); |
| 117 | + verifyException(e, "XmlRoot"); |
| 118 | + } |
| 119 | + |
| 120 | + // Empty element should also be validated |
| 121 | + @Test |
| 122 | + public void testMismatchedEmptyElement() throws Exception |
| 123 | + { |
| 124 | + assertThrows(DatabindException.class, () -> |
| 125 | + ENFORCING_MAPPER.readValue("<Wrong/>", Root.class)); |
| 126 | + } |
| 127 | + |
| 128 | + // Namespace URI verification: matching namespace should pass |
| 129 | + @Test |
| 130 | + public void testMatchingNamespaceSucceeds() throws Exception |
| 131 | + { |
| 132 | + NamespacedRoot root = ENFORCING_MAPPER.readValue( |
| 133 | + "<ns:NsRoot xmlns:ns=\"http://example.com/test\"><ns:value>42</ns:value></ns:NsRoot>", |
| 134 | + NamespacedRoot.class); |
| 135 | + assertEquals(42, root.value); |
| 136 | + } |
| 137 | + |
| 138 | + // Namespace URI verification: wrong namespace should fail |
| 139 | + @Test |
| 140 | + public void testMismatchedNamespaceFails() throws Exception |
| 141 | + { |
| 142 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 143 | + ENFORCING_MAPPER.readValue( |
| 144 | + "<ns:NsRoot xmlns:ns=\"http://example.com/wrong\"><ns:value>42</ns:value></ns:NsRoot>", |
| 145 | + NamespacedRoot.class)); |
| 146 | + verifyException(e, "Root namespace"); |
| 147 | + verifyException(e, "http://example.com/wrong"); |
| 148 | + verifyException(e, "http://example.com/test"); |
| 149 | + } |
| 150 | + |
| 151 | + // No namespace in XML when one is expected should fail |
| 152 | + @Test |
| 153 | + public void testMissingNamespaceFails() throws Exception |
| 154 | + { |
| 155 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 156 | + ENFORCING_MAPPER.readValue( |
| 157 | + "<NsRoot><value>42</value></NsRoot>", |
| 158 | + NamespacedRoot.class)); |
| 159 | + verifyException(e, "Root namespace"); |
| 160 | + } |
| 161 | + |
| 162 | + // Unexpected namespace in XML when none is expected should fail |
| 163 | + @Test |
| 164 | + public void testUnexpectedNamespaceFails() throws Exception |
| 165 | + { |
| 166 | + DatabindException e = assertThrows(DatabindException.class, () -> |
| 167 | + ENFORCING_MAPPER.readValue( |
| 168 | + "<ns:Root xmlns:ns=\"http://example.com/unexpected\"><ns:value>42</ns:value></ns:Root>", |
| 169 | + Root.class)); |
| 170 | + verifyException(e, "Root namespace"); |
| 171 | + } |
| 172 | + |
| 173 | + // No namespace expected, none present should pass |
| 174 | + @Test |
| 175 | + public void testNoNamespaceExpectedNonePresentSucceeds() throws Exception |
| 176 | + { |
| 177 | + Root root = ENFORCING_MAPPER.readValue( |
| 178 | + "<Root><value>42</value></Root>", Root.class); |
| 179 | + assertEquals(42, root.value); |
| 180 | + } |
| 181 | +} |
0 commit comments