|
9 | 9 | import javax.xml.parsers.DocumentBuilder;
|
10 | 10 | import javax.xml.parsers.DocumentBuilderFactory;
|
11 | 11 | import javax.xml.parsers.ParserConfigurationException;
|
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.InputStream; |
12 | 14 | import java.io.StringReader;
|
| 15 | +import java.nio.charset.StandardCharsets; |
13 | 16 |
|
14 | 17 | import static org.junit.jupiter.api.Assertions.*;
|
15 | 18 |
|
16 | 19 | public class XmlDocumentUnitTest {
|
17 | 20 |
|
18 | 21 | @Test
|
19 |
| - public void givenXmlString_whenConvertToDocument_thenSuccess() throws Exception { |
| 22 | + public void givenXmlString_whenConvertToDocumentViaString_thenSuccess() throws Exception { |
20 | 23 | String xmlString = "<root><child>Example</child></root>";
|
21 | 24 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
22 | 25 | DocumentBuilder builder = factory.newDocumentBuilder();
|
@@ -64,4 +67,41 @@ public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() thr
|
64 | 67 | builder.parse(new InputSource(new StringReader(invalidXmlString)));
|
65 | 68 | });
|
66 | 69 | }
|
| 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 | + |
67 | 107 | }
|
0 commit comments