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