1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: TestDOMWriter.java,v 1.5 2004/06/25 08:03:50 maartenc Exp $
8 */
9
10 package org.dom4j.io;
11
12 import java.io.StringWriter;
13 import java.net.URL;
14 import junit.framework.*;
15 import junit.textui.TestRunner;
16 import org.dom4j.AbstractTestCase;
17 import org.w3c.dom.NamedNodeMap;
18
19 /***
20 *
21 * @author Maarten
22 */
23 public class TestDOMWriter extends AbstractTestCase {
24
25 public TestDOMWriter(java.lang.String testName) {
26 super(testName);
27 }
28
29 public static void main( String[] args ) {
30 TestRunner.run( suite() );
31 }
32
33 public static Test suite() {
34 TestSuite suite = new TestSuite(TestDOMWriter.class);
35 return suite;
36 }
37
38 // TODO add test methods here, they have to start with 'test' name.
39 // for example:
40 // public void testHello() {}
41 public void testNamespaceBug() throws Exception {
42 org.dom4j.Document doc = parseDocument("/xml/namespaces.xml");
43 DOMWriter writer = new DOMWriter(org.dom4j.dom.DOMDocument.class);
44 org.w3c.dom.Document result = writer.write(doc);
45
46 NamedNodeMap atts = result.getDocumentElement().getAttributes();
47 assertEquals(4, atts.getLength());
48
49 XMLWriter wr = new XMLWriter();
50 wr.setOutputStream(System.out);
51 wr.write((org.dom4j.Document) result);
52 }
53
54 public void testBug905745() throws Exception {
55 org.dom4j.Document doc = parseDocument("/xml/namespaces.xml");
56 DOMWriter writer = new DOMWriter();
57 org.w3c.dom.Document result = writer.write(doc);
58
59 NamedNodeMap atts = result.getDocumentElement().getAttributes();
60 org.w3c.dom.Node versionAttr = atts.getNamedItem("version");
61 assertNotNull(versionAttr);
62 assertNotNull(versionAttr.getLocalName());
63 assertEquals("version", versionAttr.getLocalName());
64 assertEquals("version", versionAttr.getNodeName());
65 }
66
67 public void testBug926752() throws Exception {
68 org.dom4j.Document doc = parseDocument("/xml/test/defaultNamespace.xml");
69 DOMWriter writer = new DOMWriter(org.dom4j.dom.DOMDocument.class);
70 org.w3c.dom.Document result = writer.write(doc);
71
72 NamedNodeMap atts = result.getDocumentElement().getAttributes();
73 assertEquals(1, atts.getLength());
74
75 OutputFormat format = OutputFormat.createCompactFormat();
76 format.setSuppressDeclaration(true);
77 XMLWriter wr = new XMLWriter(format);
78 StringWriter strWriter = new StringWriter();
79 wr.setWriter(strWriter);
80 wr.write((org.dom4j.Document) result);
81 assertEquals("<a xmlns=\"dummyNamespace\"><b><c>Hello</c></b></a>", strWriter.toString());
82 }
83
84 protected org.dom4j.Document parseDocument(String file) throws Exception {
85 URL url = getClass().getResource(file);
86 SAXReader reader = new SAXReader();
87 return reader.read(url);
88 }
89
90 }
91
92
93
94
95 /*
96 * Redistribution and use of this software and associated documentation
97 * ("Software"), with or without modification, are permitted provided
98 * that the following conditions are met:
99 *
100 * 1. Redistributions of source code must retain copyright
101 * statements and notices. Redistributions must also contain a
102 * copy of this document.
103 *
104 * 2. Redistributions in binary form must reproduce the
105 * above copyright notice, this list of conditions and the
106 * following disclaimer in the documentation and/or other
107 * materials provided with the distribution.
108 *
109 * 3. The name "DOM4J" must not be used to endorse or promote
110 * products derived from this Software without prior written
111 * permission of MetaStuff, Ltd. For written permission,
112 * please contact dom4j-info@metastuff.com.
113 *
114 * 4. Products derived from this Software may not be called "DOM4J"
115 * nor may "DOM4J" appear in their names without prior written
116 * permission of MetaStuff, Ltd. DOM4J is a registered
117 * trademark of MetaStuff, Ltd.
118 *
119 * 5. Due credit should be given to the DOM4J Project -
120 * http://www.dom4j.org
121 *
122 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
123 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
124 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
125 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
126 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
127 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
128 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
129 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
130 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
131 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
132 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
133 * OF THE POSSIBILITY OF SUCH DAMAGE.
134 *
135 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
136 *
137 * $Id: TestDOMWriter.java,v 1.5 2004/06/25 08:03:50 maartenc Exp $
138 */