[Aprog]
[aprog.git] / Aprog / test / net / sourceforge / aprog / xml / XMLToolsTest.java
blob0d1a26ab5b60ddef286a16df35582861175bd7d0
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 package net.sourceforge.aprog.xml;
27 import static net.sourceforge.aprog.tools.Tools.*;
28 import static net.sourceforge.aprog.xml.XMLTools.*;
30 import static org.junit.Assert.*;
32 import java.io.ByteArrayOutputStream;
33 import java.util.logging.Level;
35 import javax.xml.transform.Result;
36 import javax.xml.transform.stream.StreamResult;
38 import org.junit.Test;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Node;
42 /**
43 * Automated tests using JUnit 4 for {@link XMLTools}.
45 * @author codistmonk (creation 2010-07-01)
47 public final class XMLToolsTest {
49 @Test
50 public final void testGetQualifiedName() {
51 final Document document = XMLTools.parse("<root/>");
53 assertEquals("root", getQualifiedName(document.getDocumentElement()));
55 document.renameNode(document.getDocumentElement(), null, "renamed-root");
57 assertEquals("renamed-root", getQualifiedName(document.getDocumentElement()));
59 document.renameNode(document.getDocumentElement(), NAMESPACE_URI, "renamed:root");
61 assertEquals("renamed:root", getQualifiedName(document.getDocumentElement()));
64 @Test
65 public final void testParse() {
66 assertNotNull(parse("<a/>"));
67 assertNotNull(parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a/>"));
70 @Test(expected=RuntimeException.class)
71 public final void testParseFailureMissingClosingTag() {
72 assertNotNull(parse("<a>"));
75 @Test(expected=RuntimeException.class)
76 public final void testParseFailureMultipleRoots() {
77 assertNotNull(parse("<a/><b/>"));
80 @Test
81 public final void testNewDocument() {
82 assertNotNull(newDocument());
85 @Test
86 public final void testNormalize() {
87 final Document document = parse("<a/>");
88 final Node root = document.getDocumentElement();
90 assertEquals("a", root.getNodeName());
92 // Make the document "not normalized" by adding an empty text node
93 root.appendChild(document.createTextNode(""));
95 assertEquals(1, root.getChildNodes().getLength());
96 assertSame(document, normalize(document));
97 assertEquals(0, root.getChildNodes().getLength());
100 @Test
101 public final void testWrite() {
102 final String xmlInput = "<a/>";
103 final Document document = parse(xmlInput);
106 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
108 XMLTools.write(document, buffer, 0);
110 assertEquals(XML_1_UTF8_NOT_STANDALONE + xmlInput, buffer.toString());
113 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
115 XMLTools.write(standalone(document), buffer, 0);
117 assertEquals(XML_1_UTF8_STANDALONE + xmlInput, buffer.toString());
120 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
122 XMLTools.write(document.getDocumentElement(), buffer, 0);
124 assertEquals(xmlInput, buffer.toString());
129 @Test
130 public final void testGetNode() throws Exception {
131 final Document document = parse(
132 "<a>" +
133 " <b c='d'/>" +
134 "</a>"
137 assertEquals("d", getNode(document, "a/b/@c").getNodeValue());
138 assertEquals("b", getNode(document, "a/b[@c='d']").getNodeName());
139 assertNull(getNode(document, "a/b[@c='e']"));
142 @Test
143 public final void testGetOrCreateNode() {
144 final Document document = newDocument();
146 getOrCreateNode(document, "aa[]");
148 assertXMLEquals(
149 "<aa/>"
150 , document);
152 getOrCreateNode(document, "aa/b");
154 assertXMLEquals(
155 "<aa>" +
156 "<b/>" +
157 "</aa>"
158 , document);
160 getOrCreateNode(document, "aa/b[@c='d']");
162 assertXMLEquals(
163 "<aa>" +
164 "<b/>" +
165 "<b c=\"d\"/>" +
166 "</aa>"
167 , document);
169 getOrCreateNode(document, "aa/b[]");
171 assertXMLEquals(
172 "<aa>" +
173 "<b/>" +
174 "<b c=\"d\"/>" +
175 "<b/>" +
176 "</aa>"
177 , document);
179 getOrCreateNode(document, "aa/b[position()=2]/e");
181 assertXMLEquals(
182 "<aa>" +
183 "<b/>" +
184 "<b c=\"d\"><e/></b>" +
185 "<b/>" +
186 "</aa>"
187 , document);
189 getOrCreateNode(document, "aa/b[position()=2]/e/@f");
191 assertXMLEquals(
192 "<aa>" +
193 "<b/>" +
194 "<b c=\"d\"><e f=\"\"/></b>" +
195 "<b/>" +
196 "</aa>"
197 , document);
200 @Test
201 public final void testValidate() {
202 // DTD validation
203 testValidate("test.xml", "test.dtd", false);
205 // XSD validation
206 testValidate("test.xml", "test.xsd", false);
208 // Relax-NG validation, if available
209 testValidate("test.xml", "test.rng", true);
211 // Compact Relax-NG validation, if available
212 testValidate("test.xml", "test.rnc", true);
215 private static final String PATH = getThisPackagePath();
218 * {@value}.
220 private static final String NAMESPACE_URI = "urn:net.sourceforge.aprog";
224 * @param xmlFileName
225 * <br>Not null
226 * @param dtdOrSchemaFileName
227 * <br>Not null
228 * @param canBeUnavailable {@code true} if the validation can fail because the schema language is not available
230 private static final void testValidate(final String xmlFileName, final String dtdOrSchemaFileName,
231 final boolean canBeUnavailable) {
232 try {
233 assertEquals(0, validate(
234 getResourceAsStream(PATH + xmlFileName),
235 getResourceAsSource(PATH + dtdOrSchemaFileName)).size());
236 } catch (final IllegalArgumentException exception) {
237 if (canBeUnavailable &&
238 exception.getMessage().startsWith("No SchemaFactory that implements the schema language specified")) {
239 getLoggerForThisMethod().log(Level.INFO, debug(3, exception.getMessage()));
240 } else {
241 throw exception;
248 * @param expectedXML
249 * <br>Not null
250 * @param document
251 * <br>Not null
253 private static final void assertXMLEquals(final String expectedXML, final Document document) {
254 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
255 final Result result = new StreamResult(buffer);
257 write(normalize(document).getDocumentElement(), result, 0);
259 assertEquals(expectedXML, buffer.toString());