diff --git a/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTest.java b/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTest.java deleted file mode 100644 index 51acd7fa299..00000000000 --- a/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTest.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.springframework.util.xml; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.SAXException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.IOException; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - -public class DomUtilsTest { - - private Element elementWithText; - - private Element elementWithComment; - - private Element elementWithEntityReference; - - private Element elementWithEmptyReference; - - private static final String CLASS = "class"; - - private static final String PRINCIPAL = "principal"; - - private static final String HEAD_MASTER = "headMaster"; - - @BeforeEach - void setup() throws ParserConfigurationException, IOException, SAXException { - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); - Document documentWithText = documentBuilder.parse(new File("src/test/resources/scanned-resources/resource#element-with-text.xml")); - Document documentWithComment = documentBuilder.parse(new File("src/test/resources/scanned-resources/resource#element-with-comment.xml")); - Document documentWithEntityReference = documentBuilder.parse(new File("src/test/resources/scanned-resources/resource#element-with-entity-reference.xml")); - Document documentWithEmptyValue = documentBuilder.parse(new File("src/test/resources/scanned-resources/resource#element-with-empty-reference.xml")); - elementWithText = documentWithText.getDocumentElement(); - elementWithComment = documentWithComment.getDocumentElement(); - elementWithEntityReference = documentWithEntityReference.getDocumentElement(); - elementWithEmptyReference = documentWithEmptyValue.getDocumentElement(); - } - - @Test - void getChildElementsByTagNameTestNodeInstanceOfElementAndDesiredElementsPresent() { - List childElements = DomUtils.getChildElementsByTagName(elementWithText, CLASS, PRINCIPAL); - assertAll( - () -> assertEquals(3, childElements.size()), - () -> assertEquals(CLASS, childElements.get(0).getNodeName()), - () -> assertEquals(CLASS, childElements.get(1).getNodeName()), - () -> assertEquals(PRINCIPAL, childElements.get(2).getNodeName()) - ); - } - - @Test - void getChildElementsByTagNameTestNodeInstanceOfElementAndDesiredElementsNotPresent() { - List childElements = DomUtils.getChildElementsByTagName(elementWithText, HEAD_MASTER); - assertEquals(0, childElements.size()); - } - - @Test - void getChildElementByTagNameTestElementPresentInChildNodeList() { - Element childElement = DomUtils.getChildElementByTagName(elementWithText, PRINCIPAL); - assertNotNull(childElement); - } - - @Test - void getChildElementByTagNameTestElementPresentInChildNodeListAndChildElementsDoNotHaveSampleTag() { - Element childElement = DomUtils.getChildElementByTagName(elementWithText, HEAD_MASTER); - assertNull(childElement); - } - - @Test - void getChildElementValueByTagNameTestElementPresentInChildNodeList() { - assertEquals("Fox Test", DomUtils.getChildElementValueByTagName(elementWithText, "guard")); - } - - @Test - void getChildElementValueByTagNameTestElementWithoutChild() { - assertNull(DomUtils.getChildElementValueByTagName(elementWithText, "math tutor")); - } - - @Test - void getChildElementsTestWithValidChildNodes() { - List childElements = DomUtils.getChildElements(elementWithText); - assertAll( - () -> assertEquals(4, childElements.size()) - ); - } - - @Test - void getTextValueTestWithCharacterDataNode() { - assertTrue(DomUtils.getTextValue(elementWithText).contains("TestSchool")); - } - - @Test - void getTextValueWithCommentInXml() { - assertTrue(DomUtils.getTextValue(elementWithComment).isBlank()); - } - - @Test - void getTextValueWithEntityReferenceInXml() { - assertTrue(DomUtils.getTextValue(elementWithEntityReference).contains("&")); - } - - @Test - void getTextValueWithEmptyReferenceInXml() { - assertTrue(DomUtils.getTextValue(elementWithEmptyReference).isBlank()); - } - - @Test - void nodeNameTestTrueCondition() { - assertTrue(DomUtils.nodeNameEquals(elementWithText, "school")); - } - - @Test - void nodeNameTestFalseCondition() { - assertFalse(DomUtils.nodeNameEquals(elementWithText, "college")); - } - - @Test - void createContentHandlerTest() { - assertNotNull(DomUtils.createContentHandler(elementWithText)); - } -} diff --git a/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTests.java b/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTests.java new file mode 100644 index 00000000000..51b2d9c5f3b --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/xml/DomUtilsTests.java @@ -0,0 +1,146 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.util.xml; + +import java.io.ByteArrayInputStream; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DomUtils}. + * + * @author Stephane Nicoll + * @author Kunal Jani + */ +class DomUtilsTests { + + private static final Element SCHOOL_ELEMENT = getDocumentElement(""" + + TestSchool + Test Teacher One + Test Teacher Two + Test Principal + Fox Test + """); + + + @Test + void getChildElementsByTagNameWithSeveralMatchingTags() { + List childElements = DomUtils.getChildElementsByTagName(SCHOOL_ELEMENT, "class", "principal"); + assertThat(childElements).map(Element::getNodeName).containsExactly("class", "class", "principal"); + } + + @Test + void getChildElementsByTagNameWhenTagDoesNotExist() { + assertThat(DomUtils.getChildElementsByTagName(SCHOOL_ELEMENT, "teacher")).isEmpty(); + } + + @Test + void getChildElementByTagNameWithMatchingTag() { + Element principalElement = DomUtils.getChildElementByTagName(SCHOOL_ELEMENT, "principal"); + assertThat(principalElement).isNotNull(); + assertThat(principalElement.getTextContent()).isEqualTo("Test Principal"); + } + + @Test + void getChildElementByTagNameWithNonMatchingTag() { + assertThat(DomUtils.getChildElementByTagName(SCHOOL_ELEMENT, "teacher")).isNull(); + } + + @Test + void getChildElementValueByTagName() { + assertThat(DomUtils.getChildElementValueByTagName(SCHOOL_ELEMENT, "guard")).isEqualTo("Fox Test"); + } + + @Test + void getChildElementValueByTagNameWithNonMatchingTag() { + assertThat(DomUtils.getChildElementValueByTagName(SCHOOL_ELEMENT, "math tutor")).isNull(); + } + + @Test + void getChildElements() { + List childElements = DomUtils.getChildElements(SCHOOL_ELEMENT); + assertThat(childElements).map(Element::getNodeName).containsExactly("class", "class", "principal", "guard"); + } + + @Test + void getTextValueWithCharacterDataNode() { + assertThat(DomUtils.getTextValue(SCHOOL_ELEMENT)).isEqualToIgnoringWhitespace("TestSchool"); + } + + @Test + void getTextValueWithCommentInXml() { + Element elementWithComment = getDocumentElement(""" + + + + Alice + """); + assertThat(DomUtils.getTextValue(elementWithComment)).isBlank(); + } + + @Test + void getTextValueWithEntityReference() { + Element elementWithEntityReference = getDocumentElement(""" + + + & + Alice + """); + assertThat(DomUtils.getTextValue(elementWithEntityReference)).contains("&"); + } + + @Test + void getTextValueWithEmptyElement() { + Element emptyElement = getDocumentElement(""" + + """); + assertThat(DomUtils.getTextValue(emptyElement)).isBlank(); + } + + @Test + void nodeNameEqualsWhenTrue() { + assertThat(DomUtils.nodeNameEquals(SCHOOL_ELEMENT, "school")).isTrue(); + } + + @Test + void nodeNameEqualsWhenFalse() { + assertThat(DomUtils.nodeNameEquals(SCHOOL_ELEMENT, "college")).isFalse(); + } + + + private static Element getDocumentElement(String xmlContent) { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes())); + return document.getDocumentElement(); + } + catch (Exception ex) { + throw new IllegalStateException("Failed to parse xml content:%n%s".formatted(xmlContent), ex); + } + } + +} diff --git a/spring-core/src/test/resources/scanned-resources/resource#element-with-comment.xml b/spring-core/src/test/resources/scanned-resources/resource#element-with-comment.xml deleted file mode 100644 index 45f708e3df8..00000000000 --- a/spring-core/src/test/resources/scanned-resources/resource#element-with-comment.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - Alice - \ No newline at end of file diff --git a/spring-core/src/test/resources/scanned-resources/resource#element-with-empty-reference.xml b/spring-core/src/test/resources/scanned-resources/resource#element-with-empty-reference.xml deleted file mode 100644 index 9e75c51992a..00000000000 --- a/spring-core/src/test/resources/scanned-resources/resource#element-with-empty-reference.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/spring-core/src/test/resources/scanned-resources/resource#element-with-entity-reference.xml b/spring-core/src/test/resources/scanned-resources/resource#element-with-entity-reference.xml deleted file mode 100644 index 5232e5b27e2..00000000000 --- a/spring-core/src/test/resources/scanned-resources/resource#element-with-entity-reference.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - & - Alice - \ No newline at end of file diff --git a/spring-core/src/test/resources/scanned-resources/resource#element-with-text.xml b/spring-core/src/test/resources/scanned-resources/resource#element-with-text.xml deleted file mode 100644 index ba21a1e97a4..00000000000 --- a/spring-core/src/test/resources/scanned-resources/resource#element-with-text.xml +++ /dev/null @@ -1,18 +0,0 @@ - -TestSchool - Test Teacher One - Abe Sample - Jane Doe - John Doe - - Test Teacher Two - Def Sample - Jane Dane - John Dane - - Test Principal - Head - Master - - Fox Test -