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