1
2
3
4
5
6
7
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
39
40
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138