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
.events
.EventsTestingTools
.*;
28 import static net
.sourceforge
.aprog
.tools
.Tools
.*;
29 import static net
.sourceforge
.aprog
.xml
.XMLTools
.*;
31 import static org
.junit
.Assert
.*;
33 import java
.io
.ByteArrayOutputStream
;
34 import java
.util
.logging
.Level
;
36 import javax
.xml
.transform
.Result
;
37 import javax
.xml
.transform
.stream
.StreamResult
;
39 import net
.sourceforge
.aprog
.events
.EventsTestingTools
.EventRecorder
;
41 import org
.junit
.Test
;
42 import org
.w3c
.dom
.Document
;
43 import org
.w3c
.dom
.Node
;
44 import org
.w3c
.dom
.events
.Event
;
45 import org
.w3c
.dom
.events
.EventListener
;
46 import org
.w3c
.dom
.events
.MutationEvent
;
49 * Automated tests using JUnit 4 for {@link XMLTools}.
51 * @author codistmonk (creation 2010-07-01)
53 public final class XMLToolsTest
{
56 public final <R
extends EventRecorder
<Event
> & EventListener
> void testEvents() {
57 final Document document
= parse("<a><b c='d'/></a>");
58 @SuppressWarnings("unchecked")
59 final R recorder
= (R
) newEventRecorder(EventListener
.class);
61 addDOMEventListener(document
, recorder
);
62 getNode(document
, "a/b/@c").setNodeValue("e");
65 final MutationEvent event
= recorder
.getEvent(0);
67 assertEquals(DOM_EVENT_ATTRIBUTE_MODIFIED
, event
.getType());
68 assertSame(getNode(document
, "a/b"), event
.getTarget());
69 assertEquals(MutationEvent
.MODIFICATION
, event
.getAttrChange());
70 assertEquals("c", event
.getRelatedNode().getNodeName());
71 assertEquals("d", event
.getPrevValue());
72 assertEquals("e", event
.getNewValue());
76 final MutationEvent event
= recorder
.getEvent(1);
78 assertEquals(DOM_EVENT_SUBTREE_MODIFIED
, event
.getType());
79 assertSame(getNode(document
, "a/b"), event
.getTarget());
80 assertNull(event
.getRelatedNode());
81 assertEquals(0, event
.getAttrChange());
82 assertNull(event
.getPrevValue());
83 assertNull(event
.getNewValue());
86 rename(getNode(document
, "a/b"), null, "renamed-b");
89 final MutationEvent event
= recorder
.getEvent(2);
91 assertEquals(DOM_EVENT_SUBTREE_MODIFIED
, event
.getType());
92 assertSame(getNode(document
, "a/renamed-b"), event
.getTarget());
93 assertEquals("b", event
.getPrevValue());
94 assertEquals("renamed-b", event
.getNewValue());
97 assertEquals(3, recorder
.getEvents().size());
101 public final void testGetQualifiedName() {
102 final Document document
= XMLTools
.parse("<root/>");
104 assertEquals("root", getQualifiedName(document
.getDocumentElement()));
106 document
.renameNode(document
.getDocumentElement(), null, "renamed-root");
108 assertEquals("renamed-root", getQualifiedName(document
.getDocumentElement()));
110 document
.renameNode(document
.getDocumentElement(), NAMESPACE_URI
, "renamed:root");
112 assertEquals("renamed:root", getQualifiedName(document
.getDocumentElement()));
116 public final void testParse() {
117 assertNotNull(parse("<a/>"));
118 assertNotNull(parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a/>"));
121 @Test(expected
=RuntimeException
.class)
122 public final void testParseFailureMissingClosingTag() {
123 assertNotNull(parse("<a>"));
126 @Test(expected
=RuntimeException
.class)
127 public final void testParseFailureMultipleRoots() {
128 assertNotNull(parse("<a/><b/>"));
132 public final void testNewDocument() {
133 assertNotNull(newDocument());
137 public final void testNormalize() {
138 final Document document
= parse("<a/>");
139 final Node root
= document
.getDocumentElement();
141 assertEquals("a", root
.getNodeName());
143 // Make the document "not normalized" by adding an empty text node
144 root
.appendChild(document
.createTextNode(""));
146 assertEquals(1, root
.getChildNodes().getLength());
147 assertSame(document
, normalize(document
));
148 assertEquals(0, root
.getChildNodes().getLength());
152 public final void testWrite() {
153 final String xmlInput
= "<a/>";
154 final Document document
= parse(xmlInput
);
157 final ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
159 XMLTools
.write(document
, buffer
, 0);
161 assertEquals(XML_1_0_UTF8_STANDALONE_NO
+ xmlInput
, buffer
.toString());
164 final ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
166 XMLTools
.write(standalone(document
), buffer
, 0);
168 assertEquals(XML_1_0_UTF8_STANDALONE_YES
+ xmlInput
, buffer
.toString());
171 final ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
173 XMLTools
.write(document
.getDocumentElement(), buffer
, 0);
175 assertEquals(xmlInput
, buffer
.toString());
181 public final void testGetNode() throws Exception
{
182 final Document document
= parse(
188 assertEquals("d", getNode(document
, "a/b/@c").getNodeValue());
189 assertEquals("b", getNode(document
, "a/b[@c='d']").getNodeName());
190 assertNull(getNode(document
, "a/b[@c='e']"));
194 public final void testGetOrCreateNode() {
195 final Document document
= newDocument();
197 getOrCreateNode(document
, "aa[]");
203 getOrCreateNode(document
, "aa/b");
211 getOrCreateNode(document
, "aa/b[@c='d']");
220 getOrCreateNode(document
, "aa/b[]");
230 getOrCreateNode(document
, "aa/b[position()=2]/e");
235 "<b c=\"d\"><e/></b>" +
240 getOrCreateNode(document
, "aa/b[position()=2]/e/@f");
245 "<b c=\"d\"><e f=\"\"/></b>" +
252 public final void testValidate() {
254 testValidate("test.xml", "test.dtd", false);
257 testValidate("test.xml", "test.xsd", false);
259 // Relax-NG validation, if available
260 testValidate("test.xml", "test.rng", true);
262 // Compact Relax-NG validation, if available
263 testValidate("test.xml", "test.rnc", true);
266 private static final String PATH
= getThisPackagePath();
271 private static final String NAMESPACE_URI
= "urn:net.sourceforge.aprog";
277 * @param dtdOrSchemaFileName
279 * @param canBeUnavailable {@code true} if the validation can fail because the schema language is not available
281 private static final void testValidate(final String xmlFileName
, final String dtdOrSchemaFileName
,
282 final boolean canBeUnavailable
) {
284 assertEquals(0, validate(
285 getResourceAsStream(PATH
+ xmlFileName
),
286 getResourceAsSource(PATH
+ dtdOrSchemaFileName
)).size());
287 } catch (final IllegalArgumentException exception
) {
288 if (canBeUnavailable
&&
289 exception
.getMessage().startsWith("No SchemaFactory that implements the schema language specified")) {
290 getLoggerForThisMethod().log(Level
.INFO
, debug(3, exception
.getMessage()));
304 private static final void assertXMLEquals(final String expectedXML
, final Document document
) {
305 final ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
306 final Result result
= new StreamResult(buffer
);
308 write(normalize(document
).getDocumentElement(), result
, 0);
310 assertEquals(expectedXML
, buffer
.toString());