diff --git a/xml-modules/xml-3/pom.xml b/xml-modules/xml-3/pom.xml index ddb87e781906..ecf48bd59442 100644 --- a/xml-modules/xml-3/pom.xml +++ b/xml-modules/xml-3/pom.xml @@ -13,6 +13,27 @@ 1.0.0-SNAPSHOT + + + org.assertj + assertj-core + 3.26.0 + test + + + org.xmlunit + xmlunit-assertj + 2.10.0 + test + + + org.xmlunit + xmlunit-core + 2.10.0 + test + + + xml-3 diff --git a/xml-modules/xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java b/xml-modules/xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java index 1d50cf331ba5..56824a848c37 100644 --- a/xml-modules/xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java +++ b/xml-modules/xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java @@ -6,15 +6,20 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXParseException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import java.nio.charset.StandardCharsets; import static org.junit.jupiter.api.Assertions.*; +import static org.xmlunit.assertj.XmlAssert.assertThat; public class XmlDocumentUnitTest { @@ -57,6 +62,42 @@ public void givenDocument_whenInsertNode_thenNodeAdded() throws Exception { assertEquals("child", existingDocument.getDocumentElement().getChildNodes().item(0).getNodeName()); } + @Test + public void givenXmlFile_whenConvertToOneLineString_thenSuccess() throws IOException { + + String filePath = "posts.xml"; + ClassLoader classLoader = getClass().getClassLoader(); + FileReader fileReader = new FileReader(classLoader + .getResource(filePath) + .getFile()); + StringBuilder xmlContentBuilder = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(fileReader)) { + String line; + while ((line = reader.readLine()) != null) { + xmlContentBuilder.append(line); + } + } + String xmlString = xmlContentBuilder.toString(); + // Remove tabs + String oneLineXml = xmlString.replaceAll("\\t", ""); + + // Replace multiple spaces with a single space + oneLineXml = oneLineXml.replaceAll(" +", " "); + + // Remove spaces before/after tags (e.g., "> <" becomes "><") + // This is important to ensure truly minimal whitespace + oneLineXml = oneLineXml.replaceAll(">\\s+<", "><"); + + // Trim leading/trailing whitespace from the entire string + String actualXml = oneLineXml.trim(); + + String expectedXml = """ + Parsing XML as a String in JavaJohn Doe + """; + + assertThat(actualXml).and(expectedXml).areIdentical(); + } + @Test public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); diff --git a/xml-modules/xml-3/src/test/resources/posts.xml b/xml-modules/xml-3/src/test/resources/posts.xml new file mode 100644 index 000000000000..527231229f69 --- /dev/null +++ b/xml-modules/xml-3/src/test/resources/posts.xml @@ -0,0 +1,7 @@ + + + + Parsing XML as a String in Java + John Doe + +