1
2
3
4
5
6
7
8
9
10 package org.dom4j.dom;
11
12 import java.io.File;
13 import java.io.InputStream;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18
19 import org.dom4j.AbstractTestCase;
20 import org.dom4j.io.DOMWriter;
21 import org.dom4j.io.SAXReader;
22 import org.w3c.dom.DOMException;
23 import org.w3c.dom.NamedNodeMap;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 /*** A test harness to test the native DOM implementation of dom4j
28 *
29 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
30 * @version $Revision: 1.8 $
31 */
32 public class TestDOM extends AbstractTestCase {
33
34 protected static boolean VERBOSE = false;
35
36 /*** Elements. */
37 private long elements;
38
39 /*** Attributes. */
40 private long attributes;
41
42 /*** Characters. */
43 private long characters;
44
45
46
47 public static void main( String[] args ) {
48 TestRunner.run( suite() );
49 }
50
51 public static Test suite() {
52 return new TestSuite( TestDOM.class );
53 }
54
55 public TestDOM(String name) {
56 super(name);
57 }
58
59
60
61 public void testCount() throws Exception {
62 DOMWriter domWriter = new DOMWriter();
63
64 long start = System.currentTimeMillis();
65 org.w3c.dom.Document domDocument = domWriter.write( document );
66 long end = System.currentTimeMillis();
67
68 System.out.println( "Converting to a W3C Document took: " + (end - start) + " milliseconds" );
69
70 traverse( domDocument );
71
72 log( "elements: " + elements
73 + " attributes: " + attributes
74 + " characters: " + characters
75 );
76 }
77
78 /*** Tests the bug found by Soumanjoy */
79 public void testClassCastBug() throws Exception {
80 DOMDocument oDocument = new DOMDocument("Root");
81 org.w3c.dom.Element oParent = oDocument.createElement("Parent");
82
83
84 oParent.setAttribute("name", "N01");
85 oParent.setAttribute("id", "ID01");
86
87 oDocument.appendChild(oParent);
88 }
89
90 public void testReplaceChild() throws Exception {
91 DOMDocument document = new DOMDocument("Root");
92 org.w3c.dom.Element parent = document.createElement("Parent");
93 org.w3c.dom.Element first = document.createElement("FirstChild");
94 org.w3c.dom.Element second = document.createElement("SecondChild");
95 org.w3c.dom.Element third = document.createElement("ThirdChild");
96
97 document.appendChild(parent);
98 parent.appendChild(first);
99 parent.appendChild(second);
100 parent.appendChild(third);
101
102 org.w3c.dom.Element newFirst = document.createElement("NewFirst");
103 org.w3c.dom.Element oldFirst = (org.w3c.dom.Element) parent.replaceChild(newFirst, first);
104
105
106 assertEquals(oldFirst, first);
107
108
109 NodeList children = parent.getChildNodes();
110 Node firstChild = children.item(0);
111 assertEquals(Node.ELEMENT_NODE, firstChild.getNodeType());
112 assertEquals(newFirst, firstChild);
113
114
115 org.w3c.dom.Element badNode = document.createElement("No Child");
116 try {
117 parent.replaceChild(newFirst, badNode);
118 fail("DOMException should be throwed when trying to replace non existing child");
119 } catch (DOMException e) {
120 assertEquals(DOMException.NOT_FOUND_ERR, e.code);
121 }
122 }
123
124
125
126
127 protected void setUp() throws Exception {
128 SAXReader reader = new SAXReader( DOMDocumentFactory.getInstance() );
129 InputStream testDocument = getClass().getResourceAsStream("/xml/contents.xml");
130 if (testDocument == null) {
131 document = reader.read( new File( "xml/contents.xml" ) );
132 } else {
133 document = reader.read( testDocument );
134 }
135 }
136 /*** Traverses the specified node, recursively. */
137 protected void traverse(Node node) {
138
139
140 if (node == null) {
141 return;
142 }
143
144 int type = node.getNodeType();
145 switch (type) {
146 case Node.DOCUMENT_NODE: {
147 elements = 0;
148 attributes = 0;
149 characters = 0;
150 traverse(((org.w3c.dom.Document)node).getDocumentElement());
151 break;
152 }
153
154 case Node.ELEMENT_NODE: {
155 elements++;
156 NamedNodeMap attrs = node.getAttributes();
157 if (attrs != null) {
158 attributes += attrs.getLength();
159 }
160 NodeList children = node.getChildNodes();
161 if (children != null) {
162 int len = children.getLength();
163 for (int i = 0; i < len; i++) {
164 traverse(children.item(i));
165 }
166 }
167 break;
168 }
169
170 case Node.ENTITY_REFERENCE_NODE: {
171 NodeList children = node.getChildNodes();
172 if (children != null) {
173 int len = children.getLength();
174 for (int i = 0; i < len; i++) {
175 traverse(children.item(i));
176 }
177 }
178 break;
179 }
180
181 case Node.CDATA_SECTION_NODE: {
182 characters += node.getNodeValue().length();
183 break;
184 }
185
186 case Node.TEXT_NODE: {
187 characters += node.getNodeValue().length();
188 break;
189 }
190 }
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240