Skip to content

Commit 444662e

Browse files
authored
[BAEL-6533] XML from string unit tests (#17770)
* [BAEL-6533] Unit tests for creating XML from string * [BAEL-6533] Updated unit tests method names
1 parent ceee800 commit 444662e

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import javax.xml.parsers.DocumentBuilder;
1010
import javax.xml.parsers.DocumentBuilderFactory;
1111
import javax.xml.parsers.ParserConfigurationException;
12+
import java.io.ByteArrayInputStream;
13+
import java.io.InputStream;
1214
import java.io.StringReader;
15+
import java.nio.charset.StandardCharsets;
1316

1417
import static org.junit.jupiter.api.Assertions.*;
1518

1619
public class XmlDocumentUnitTest {
1720

1821
@Test
19-
public void givenXmlString_whenConvertToDocument_thenSuccess() throws Exception {
22+
public void givenXmlString_whenConvertToDocumentViaString_thenSuccess() throws Exception {
2023
String xmlString = "<root><child>Example</child></root>";
2124
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2225
DocumentBuilder builder = factory.newDocumentBuilder();
@@ -64,4 +67,41 @@ public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() thr
6467
builder.parse(new InputSource(new StringReader(invalidXmlString)));
6568
});
6669
}
70+
71+
@Test
72+
public void givenXmlString_whenConvertToDocumentViaCharStream_thenSuccess() throws Exception {
73+
String xmlString = "<posts><post postId='1'><title>Example Post</title><author>John Doe</author></post></posts>";
74+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
75+
DocumentBuilder builder = factory.newDocumentBuilder();
76+
77+
InputSource inputSource = new InputSource(new StringReader(xmlString));
78+
Document document = builder.parse(inputSource);
79+
80+
assertNotNull(document);
81+
assertEquals("posts", document.getDocumentElement().getNodeName());
82+
83+
Element rootElement = document.getDocumentElement();
84+
var childElements = rootElement.getElementsByTagName("post");
85+
assertNotNull(childElements);
86+
assertEquals(1, childElements.getLength());
87+
}
88+
89+
@Test
90+
public void givenXmlString_whenConvertToDocumentViaByteArray_thenSuccess() throws Exception {
91+
String xmlString = "<posts><post postId='1'><title>Example Post</title><author>John Doe</author></post></posts>";
92+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
93+
DocumentBuilder builder = factory.newDocumentBuilder();
94+
95+
InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8));
96+
Document document = builder.parse(inputStream);
97+
98+
assertNotNull(document);
99+
assertEquals("posts", document.getDocumentElement().getNodeName());
100+
101+
Element rootElement = document.getDocumentElement();
102+
var childElements = rootElement.getElementsByTagName("post");
103+
assertNotNull(childElements);
104+
assertEquals(1, childElements.getLength());
105+
}
106+
67107
}

0 commit comments

Comments
 (0)