1   /*
2    * Copyright 2001-2005 (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   
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      // Test case(s)
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          // <-- Fails here when the code is broken.
85          oParent.setAttribute("name", "N01");
86          oParent.setAttribute("id", "ID01");
87  
88          oDocument.appendChild(oParent); // <-- Fails here, Error message is
89          // below
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         /* check the return value of replaceChild */
109         assertEquals(oldFirst, first);
110 
111         /* make sure the old node has been replaced */
112         NodeList children = parent.getChildNodes();
113         Node firstChild = children.item(0);
114         assertEquals(Node.ELEMENT_NODE, firstChild.getNodeType());
115         assertEquals(newFirst, firstChild);
116 
117         /* try to replace a node that doesn't exist */
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     // Implementation methods
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         // is there anything to do?
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  * Redistribution and use of this software and associated documentation
217  * ("Software"), with or without modification, are permitted provided that the
218  * following conditions are met:
219  * 
220  * 1. Redistributions of source code must retain copyright statements and
221  * notices. Redistributions must also contain a copy of this document.
222  * 
223  * 2. Redistributions in binary form must reproduce the above copyright notice,
224  * this list of conditions and the following disclaimer in the documentation
225  * and/or other materials provided with the distribution.
226  * 
227  * 3. The name "DOM4J" must not be used to endorse or promote products derived
228  * from this Software without prior written permission of MetaStuff, Ltd. For
229  * written permission, please contact dom4j-info@metastuff.com.
230  * 
231  * 4. Products derived from this Software may not be called "DOM4J" nor may
232  * "DOM4J" appear in their names without prior written permission of MetaStuff,
233  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
234  * 
235  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
236  * 
237  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
238  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
241  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
243  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
244  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
245  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
246  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
247  * POSSIBILITY OF SUCH DAMAGE.
248  * 
249  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
250  */