|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util.xml; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.xml.parsers.DocumentBuilder; |
| 23 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.w3c.dom.Document; |
| 27 | +import org.w3c.dom.Element; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link DomUtils}. |
| 33 | + * |
| 34 | + * @author Stephane Nicoll |
| 35 | + * @author Kunal Jani |
| 36 | + */ |
| 37 | +class DomUtilsTests { |
| 38 | + |
| 39 | + private static final Element SCHOOL_ELEMENT = getDocumentElement(""" |
| 40 | + <?xml version="1.0"?> |
| 41 | + <school>TestSchool |
| 42 | + <class teacher="Happy Teacher">Test Teacher One</class> |
| 43 | + <class teacher="Sad Teacher">Test Teacher Two</class> |
| 44 | + <principal>Test Principal</principal> |
| 45 | + <guard>Fox Test</guard> |
| 46 | + </school>"""); |
| 47 | + |
| 48 | + |
| 49 | + @Test |
| 50 | + void getChildElementsByTagNameWithSeveralMatchingTags() { |
| 51 | + List<Element> childElements = DomUtils.getChildElementsByTagName(SCHOOL_ELEMENT, "class", "principal"); |
| 52 | + assertThat(childElements).map(Element::getNodeName).containsExactly("class", "class", "principal"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void getChildElementsByTagNameWhenTagDoesNotExist() { |
| 57 | + assertThat(DomUtils.getChildElementsByTagName(SCHOOL_ELEMENT, "teacher")).isEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void getChildElementByTagNameWithMatchingTag() { |
| 62 | + Element principalElement = DomUtils.getChildElementByTagName(SCHOOL_ELEMENT, "principal"); |
| 63 | + assertThat(principalElement).isNotNull(); |
| 64 | + assertThat(principalElement.getTextContent()).isEqualTo("Test Principal"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void getChildElementByTagNameWithNonMatchingTag() { |
| 69 | + assertThat(DomUtils.getChildElementByTagName(SCHOOL_ELEMENT, "teacher")).isNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void getChildElementValueByTagName() { |
| 74 | + assertThat(DomUtils.getChildElementValueByTagName(SCHOOL_ELEMENT, "guard")).isEqualTo("Fox Test"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void getChildElementValueByTagNameWithNonMatchingTag() { |
| 79 | + assertThat(DomUtils.getChildElementValueByTagName(SCHOOL_ELEMENT, "math tutor")).isNull(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void getChildElements() { |
| 84 | + List<Element> childElements = DomUtils.getChildElements(SCHOOL_ELEMENT); |
| 85 | + assertThat(childElements).map(Element::getNodeName).containsExactly("class", "class", "principal", "guard"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void getTextValueWithCharacterDataNode() { |
| 90 | + assertThat(DomUtils.getTextValue(SCHOOL_ELEMENT)).isEqualToIgnoringWhitespace("TestSchool"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void getTextValueWithCommentInXml() { |
| 95 | + Element elementWithComment = getDocumentElement(""" |
| 96 | + <?xml version="1.0"?> |
| 97 | + <state> |
| 98 | + <!-- This is a comment --> |
| 99 | + <person>Alice</person> |
| 100 | + </state>"""); |
| 101 | + assertThat(DomUtils.getTextValue(elementWithComment)).isBlank(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void getTextValueWithEntityReference() { |
| 106 | + Element elementWithEntityReference = getDocumentElement(""" |
| 107 | + <?xml version="1.0"?> |
| 108 | + <state> |
| 109 | + & |
| 110 | + <person>Alice</person> |
| 111 | + </state>"""); |
| 112 | + assertThat(DomUtils.getTextValue(elementWithEntityReference)).contains("&"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void getTextValueWithEmptyElement() { |
| 117 | + Element emptyElement = getDocumentElement(""" |
| 118 | + <?xml version="1.0"?> |
| 119 | + <person></person>"""); |
| 120 | + assertThat(DomUtils.getTextValue(emptyElement)).isBlank(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void nodeNameEqualsWhenTrue() { |
| 125 | + assertThat(DomUtils.nodeNameEquals(SCHOOL_ELEMENT, "school")).isTrue(); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void nodeNameEqualsWhenFalse() { |
| 130 | + assertThat(DomUtils.nodeNameEquals(SCHOOL_ELEMENT, "college")).isFalse(); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private static Element getDocumentElement(String xmlContent) { |
| 135 | + try { |
| 136 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 137 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 138 | + Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes())); |
| 139 | + return document.getDocumentElement(); |
| 140 | + } |
| 141 | + catch (Exception ex) { |
| 142 | + throw new IllegalStateException("Failed to parse xml content:%n%s".formatted(xmlContent), ex); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments