[Aprog]
[aprog.git] / Aprog / test / net / sourceforge / aprog / xml / XMLToolsTest.java
blob5445ba5a483fb065c5c3b2040adaa28ba57e8899
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.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;
48 /**
49 * Automated tests using JUnit 4 for {@link XMLTools}.
51 * @author codistmonk (creation 2010-07-01)
53 public final class XMLToolsTest {
55 @Test
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());
100 @Test
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()));
115 @Test
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/>"));
131 @Test
132 public final void testNewDocument() {
133 assertNotNull(newDocument());
136 @Test
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());
151 @Test
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());
180 @Test
181 public final void testGetNode() throws Exception {
182 final Document document = parse(
183 "<a>" +
184 " <b c='d'/>" +
185 "</a>"
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']"));
193 @Test
194 public final void testGetOrCreateNode() {
195 final Document document = newDocument();
197 getOrCreateNode(document, "aa[]");
199 assertXMLEquals(
200 "<aa/>"
201 , document);
203 getOrCreateNode(document, "aa/b");
205 assertXMLEquals(
206 "<aa>" +
207 "<b/>" +
208 "</aa>"
209 , document);
211 getOrCreateNode(document, "aa/b[@c='d']");
213 assertXMLEquals(
214 "<aa>" +
215 "<b/>" +
216 "<b c=\"d\"/>" +
217 "</aa>"
218 , document);
220 getOrCreateNode(document, "aa/b[]");
222 assertXMLEquals(
223 "<aa>" +
224 "<b/>" +
225 "<b c=\"d\"/>" +
226 "<b/>" +
227 "</aa>"
228 , document);
230 getOrCreateNode(document, "aa/b[position()=2]/e");
232 assertXMLEquals(
233 "<aa>" +
234 "<b/>" +
235 "<b c=\"d\"><e/></b>" +
236 "<b/>" +
237 "</aa>"
238 , document);
240 getOrCreateNode(document, "aa/b[position()=2]/e/@f");
242 assertXMLEquals(
243 "<aa>" +
244 "<b/>" +
245 "<b c=\"d\"><e f=\"\"/></b>" +
246 "<b/>" +
247 "</aa>"
248 , document);
251 @Test
252 public final void testValidate() {
253 // DTD validation
254 testValidate("test.xml", "test.dtd", false);
256 // XSD validation
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();
269 * {@value}.
271 private static final String NAMESPACE_URI = "urn:net.sourceforge.aprog";
275 * @param xmlFileName
276 * <br>Not null
277 * @param dtdOrSchemaFileName
278 * <br>Not null
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) {
283 try {
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()));
291 } else {
292 throw exception;
299 * @param expectedXML
300 * <br>Not null
301 * @param document
302 * <br>Not null
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());