|
6 | 6 | import org.xml.sax.InputSource;
|
7 | 7 | import org.xml.sax.SAXParseException;
|
8 | 8 |
|
9 |
| -import javax.xml.parsers.DocumentBuilder; |
10 |
| -import javax.xml.parsers.DocumentBuilderFactory; |
11 |
| -import javax.xml.parsers.ParserConfigurationException; |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.FileReader; |
| 11 | +import java.io.IOException; |
12 | 12 | import java.io.ByteArrayInputStream;
|
13 | 13 | import java.io.InputStream;
|
14 | 14 | import java.io.StringReader;
|
| 15 | + |
| 16 | +import javax.xml.parsers.DocumentBuilder; |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 18 | +import javax.xml.parsers.ParserConfigurationException; |
15 | 19 | import java.nio.charset.StandardCharsets;
|
16 | 20 |
|
17 | 21 | import static org.junit.jupiter.api.Assertions.*;
|
| 22 | +import static org.xmlunit.assertj.XmlAssert.assertThat; |
18 | 23 |
|
19 | 24 | public class XmlDocumentUnitTest {
|
20 | 25 |
|
@@ -57,6 +62,42 @@ public void givenDocument_whenInsertNode_thenNodeAdded() throws Exception {
|
57 | 62 | assertEquals("child", existingDocument.getDocumentElement().getChildNodes().item(0).getNodeName());
|
58 | 63 | }
|
59 | 64 |
|
| 65 | + @Test |
| 66 | + public void givenXmlFile_whenConvertToOneLineString_thenSuccess() throws IOException { |
| 67 | + |
| 68 | + String filePath = "posts.xml"; |
| 69 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 70 | + FileReader fileReader = new FileReader(classLoader |
| 71 | + .getResource(filePath) |
| 72 | + .getFile()); |
| 73 | + StringBuilder xmlContentBuilder = new StringBuilder(); |
| 74 | + try (BufferedReader reader = new BufferedReader(fileReader)) { |
| 75 | + String line; |
| 76 | + while ((line = reader.readLine()) != null) { |
| 77 | + xmlContentBuilder.append(line); |
| 78 | + } |
| 79 | + } |
| 80 | + String xmlString = xmlContentBuilder.toString(); |
| 81 | + // Remove tabs |
| 82 | + xmlString = xmlString.replaceAll("\\t", ""); |
| 83 | + |
| 84 | + // Replace multiple spaces with a single space |
| 85 | + xmlString = xmlString.replaceAll(" +", " "); |
| 86 | + |
| 87 | + // Remove spaces before/after tags (e.g., "> <" becomes "><") |
| 88 | + // This is important to ensure truly minimal whitespace |
| 89 | + xmlString = xmlString.replaceAll(">\\s+<", "><"); |
| 90 | + |
| 91 | + // Trim leading/trailing whitespace from the entire string |
| 92 | + String oneLineXml = xmlString.trim(); |
| 93 | + |
| 94 | + String expectedXml = """ |
| 95 | + <?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> |
| 96 | + """; |
| 97 | + |
| 98 | + assertThat(oneLineXml).and(expectedXml).areIdentical(); |
| 99 | + } |
| 100 | + |
60 | 101 | @Test
|
61 | 102 | public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() throws ParserConfigurationException {
|
62 | 103 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
0 commit comments