| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class XmlUtilsTest { |
| 61 |
@Rule |
| 62 |
public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 63 |
|
| 64 |
|
| 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 |
|
| 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 |
|
| 115 |
String xml = |
| 116 |
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + |
| 117 |
"<root><child>\u1e08hild</child></root>"; |
| 118 |
|
| 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 |
} |