4 * Copyright 2010 Codist Monk.
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
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
;
43 * Automated tests using JUnit 4 for {@link XMLTools}.
45 * @author codistmonk (creation 2010-07-01)
47 public final class XMLToolsTest
{
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()));
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/>"));
81 public final void testNewDocument() {
82 assertNotNull(newDocument());
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());
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());
130 public final void testGetNode() throws Exception
{
131 final Document document
= parse(
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']"));
143 public final void testGetOrCreateNode() {
144 final Document document
= newDocument();
146 getOrCreateNode(document
, "aa[]");
152 getOrCreateNode(document
, "aa/b");
160 getOrCreateNode(document
, "aa/b[@c='d']");
169 getOrCreateNode(document
, "aa/b[]");
179 getOrCreateNode(document
, "aa/b[position()=2]/e");
184 "<b c=\"d\"><e/></b>" +
189 getOrCreateNode(document
, "aa/b[position()=2]/e/@f");
194 "<b c=\"d\"><e f=\"\"/></b>" +
201 public final void testValidate() {
203 testValidate("test.xml", "test.dtd", false);
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();
220 private static final String NAMESPACE_URI
= "urn:net.sourceforge.aprog";
226 * @param dtdOrSchemaFileName
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
) {
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()));
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());