Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions xml-modules/xml-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-assertj</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>xml-3</finalName>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
xmlString = xmlString.replaceAll("\\t", "");

// Replace multiple spaces with a single space
xmlString = xmlString.replaceAll(" +", " ");

// Remove spaces before/after tags (e.g., "> <" becomes "><")
// This is important to ensure truly minimal whitespace
xmlString = xmlString.replaceAll(">\\s+<", "><");

// Trim leading/trailing whitespace from the entire string
String oneLineXml = xmlString.trim();

String expectedXml = """
<?xml version="1.0" encoding="UTF-8"?><posts><post postId="1"><title>Parsing XML as a String in Java</title><author>John Doe</author></post></posts>
""";

assertThat(oneLineXml).and(expectedXml).areIdentical();
}

@Test
public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand Down
7 changes: 7 additions & 0 deletions xml-modules/xml-3/src/test/resources/posts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<posts>
<post postId="1">
<title>Parsing XML as a String in Java</title>
<author>John Doe</author>
</post>
</posts>