Debian Codegraph @

src/test/java/ezvcard/util/XmlUtilsTest.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertTrue;
8
9 import java.io.Writer;
10 import java.nio.charset.StandardCharsets;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.xml.transform.OutputKeys;
18
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.TemporaryFolder;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NodeList;
25 import org.w3c.dom.Text;
26 import org.xml.sax.SAXException;
27
28 /*
29 Copyright (c) 2012-2023, Michael Angstadt
30 All rights reserved.
31
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions are met:
34
35 1. Redistributions of source code must retain the above copyright notice, this
36 list of conditions and the following disclaimer.
37 2. Redistributions in binary form must reproduce the above copyright notice,
38 this list of conditions and the following disclaimer in the documentation
39 and/or other materials provided with the distribution.
40
41 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
42 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
45 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52 The views and conclusions contained in the software and documentation are those
53 of the authors and should not be interpreted as representing official policies,
54 either expressed or implied, of the FreeBSD Project.
55 */
56
57 /**
58 * @author Michael Angstadt
59 */
60 public class XmlUtilsTest {
61 @Rule
62 public TemporaryFolder tempFolder = new TemporaryFolder();
63
64 //@formatter:off
65 private final String xml =
66 "<root>" +
67 " \n" +
68 "<child1 />" +
69 "some text" +
70 "<n:child2 xmlns:n=\"http://example.com\" />" +
71 "<!-- comment -->" +
72 "More text" +
73 "</root>";
74 //@formatter:on
75
76 @Test
77 public void createDocument() throws Exception {
78 Document document = XmlUtils.createDocument();
79 assertNotNull(document);
80 }
81
82 @Test
83 public void toDocument() throws Exception {
84 Document document = XmlUtils.toDocument(xml);
85
86 Element root = (Element) document.getFirstChild();
87 assertEquals("root", root.getLocalName());
88 assertNull(root.getNamespaceURI());
89
90 NodeList nodes = root.getChildNodes();
91 assertEquals(5, nodes.getLength());
92
93 int i = 0;
94 Text text = (Text) nodes.item(i++);
95 assertEquals(" \n", text.getTextContent());
96
97 Element element = (Element) nodes.item(i++);
98 assertEquals("child1", element.getLocalName());
99 assertNull(element.getNamespaceURI());
100
101 text = (Text) nodes.item(i++);
102 assertEquals("some text", text.getTextContent());
103
104 element = (Element) nodes.item(i++);
105 assertEquals("child2", element.getLocalName());
106 assertEquals("http://example.com", element.getNamespaceURI());
107
108 text = (Text) nodes.item(i++);
109 assertEquals("More text", text.getTextContent());
110 }
111
112 @Test
113 public void toDocument_utf8() throws Exception {
114 //@formatter:off
115 String xml =
116 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
117 "<root><child>\u1e08hild</child></root>";
118 //@formatter:on
119
120 Path file = tempFolder.newFile().toPath();
121 try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
122 writer.write(xml);
123 }
124
125 Document document = XmlUtils.toDocument(file);
126 Element root = (Element) document.getFirstChild();
127 NodeList nodes = root.getChildNodes();
128 Element child = (Element) nodes.item(0);
129 assertEquals("\u1e08hild", child.getTextContent());
130 }
131
132 @Test(expected = SAXException.class)
133 public void toDocument_invalid_xml() throws Exception {
134 String xml = "not-xml";
135 XmlUtils.toDocument(xml);
136 }
137
138 @Test
139 public void toString_() throws Exception {
140 Document expected = XmlUtils.toDocument(xml);
141 String string = XmlUtils.toString(expected);
142 Document actual = XmlUtils.toDocument(string);
143 assertXMLEqual(expected, actual);
144 }
145
146 @Test
147 public void toString_output_properties() throws Exception {
148 Document expected = XmlUtils.toDocument(xml);
149 Map<String, String> outputProperties = new HashMap<>();
150 outputProperties.put(OutputKeys.STANDALONE, "no");
151 String string = XmlUtils.toString(expected, outputProperties);
152 Document actual = XmlUtils.toDocument(string);
153 assertXMLEqual(expected, actual);
154 }
155
156 @Test
157 public void toElementList() throws Exception {
158 Document document = XmlUtils.toDocument(xml);
159 List<Element> elements = XmlUtils.toElementList(document.getFirstChild().getChildNodes());
160 assertEquals(2, elements.size());
161 assertEquals("child1", elements.get(0).getLocalName());
162 assertEquals("child2", elements.get(1).getLocalName());
163 document = XmlUtils.toDocument(xml);
164
165 document = XmlUtils.toDocument("<root />");
166 elements = XmlUtils.toElementList(document.getFirstChild().getChildNodes());
167 assertTrue(elements.isEmpty());
168 }
169
170 @Test
171 public void toElementList_no_children() throws Exception {
172 String xml = "<root/>";
173 Document document = XmlUtils.toDocument(xml);
174
175 List<Element> elements = XmlUtils.toElementList(document.getFirstChild().getChildNodes());
176 assertTrue(elements.isEmpty());
177 }
178
179 @Test
180 public void getFirstChildElement() throws Exception {
181 Document document = XmlUtils.toDocument(xml);
182 Element root = (Element) document.getFirstChild();
183 Element element = XmlUtils.getFirstChildElement(root);
184 assertEquals("child1", element.getLocalName());
185 }
186
187 @Test
188 public void getFirstChildElement_no_children() throws Exception {
189 Document document = XmlUtils.toDocument(xml);
190 Element root = (Element) document.getFirstChild();
191 Element child1 = XmlUtils.getFirstChildElement(root);
192 assertNull(XmlUtils.getFirstChildElement(child1));
193 }
194 }