KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > dom > TestDOM


1 /*
2  * Copyright 2001 (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.2 2003/07/12 12:13:06 per_nyfelt Exp $
8  */

9
10 package test.dom4j.dom;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.dom.DOMDocument;
16 import org.dom4j.dom.DOMDocumentFactory;
17 import org.dom4j.io.DOMWriter;
18 import org.dom4j.io.SAXReader;
19 import org.w3c.dom.DOMException JavaDoc;
20 import org.w3c.dom.NamedNodeMap JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
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.2 $
31   */

32 public class TestDOM extends test.dom4j.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 JavaDoc[] args ) {
48         TestRunner.run( suite() );
49     }
50
51     public static Test suite() {
52         return new TestSuite( TestDOM.class );
53     }
54
55     public TestDOM(String JavaDoc name) {
56         super(name);
57     }
58
59     // Test case(s)
60
//-------------------------------------------------------------------------
61
public void testCount() throws Exception JavaDoc {
62         DOMWriter domWriter = new DOMWriter();
63
64         long start = System.currentTimeMillis();
65         org.w3c.dom.Document JavaDoc 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 JavaDoc {
80         DOMDocument oDocument = new DOMDocument("Root");
81         org.w3c.dom.Element JavaDoc 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 JavaDoc {
91         DOMDocument document = new DOMDocument("Root");
92         org.w3c.dom.Element JavaDoc parent = document.createElement("Parent");
93         org.w3c.dom.Element JavaDoc first = document.createElement("FirstChild");
94         org.w3c.dom.Element JavaDoc second = document.createElement("SecondChild");
95         org.w3c.dom.Element JavaDoc 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 JavaDoc newFirst = document.createElement("NewFirst");
103         org.w3c.dom.Element JavaDoc oldFirst = (org.w3c.dom.Element JavaDoc) 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 JavaDoc children = parent.getChildNodes();
110         Node JavaDoc 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 JavaDoc 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 JavaDoc e) {
120             assertEquals(DOMException.NOT_FOUND_ERR, e.code);
121         }
122     }
123
124     // Implementation methods
125
//-------------------------------------------------------------------------
126

127     protected void setUp() throws Exception JavaDoc {
128         // todo create an ozone version of the DOMDocumentFactory
129
SAXReader reader = new SAXReader( DOMDocumentFactory.getInstance() );
130         InputStream JavaDoc testDocument = getClass().getResourceAsStream("/xml/contents.xml");
131         if (testDocument == null) {
132             document = reader.read( new File JavaDoc( "xml/contents.xml" ) );
133         } else {
134             document = reader.read( testDocument );
135         }
136     }
137     /** Traverses the specified node, recursively. */
138     protected void traverse(Node JavaDoc node) {
139
140         // is there anything to do?
141
if (node == null) {
142             return;
143         }
144
145         int type = node.getNodeType();
146         switch (type) {
147             case Node.DOCUMENT_NODE: {
148                 elements = 0;
149                 attributes = 0;
150                 characters = 0;
151                 traverse(((org.w3c.dom.Document JavaDoc)node).getDocumentElement());
152                 break;
153             }
154
155             case Node.ELEMENT_NODE: {
156                 elements++;
157                 NamedNodeMap JavaDoc attrs = node.getAttributes();
158                 if (attrs != null) {
159                     attributes += attrs.getLength();
160                 }
161                 NodeList JavaDoc children = node.getChildNodes();
162                 if (children != null) {
163                     int len = children.getLength();
164                     for (int i = 0; i < len; i++) {
165                         traverse(children.item(i));
166                     }
167                 }
168                 break;
169             }
170
171             case Node.ENTITY_REFERENCE_NODE: {
172                 NodeList JavaDoc children = node.getChildNodes();
173                 if (children != null) {
174                     int len = children.getLength();
175                     for (int i = 0; i < len; i++) {
176                         traverse(children.item(i));
177                     }
178                 }
179                 break;
180             }
181
182             case Node.CDATA_SECTION_NODE: {
183                 characters += node.getNodeValue().length();
184                 break;
185             }
186
187             case Node.TEXT_NODE: {
188                 characters += node.getNodeValue().length();
189                 break;
190             }
191         }
192     }
193 }
194
195
196
197
198 /*
199  * Redistribution and use of this software and associated documentation
200  * ("Software"), with or without modification, are permitted provided
201  * that the following conditions are met:
202  *
203  * 1. Redistributions of source code must retain copyright
204  * statements and notices. Redistributions must also contain a
205  * copy of this document.
206  *
207  * 2. Redistributions in binary form must reproduce the
208  * above copyright notice, this list of conditions and the
209  * following disclaimer in the documentation and/or other
210  * materials provided with the distribution.
211  *
212  * 3. The name "DOM4J" must not be used to endorse or promote
213  * products derived from this Software without prior written
214  * permission of MetaStuff, Ltd. For written permission,
215  * please contact dom4j-info@metastuff.com.
216  *
217  * 4. Products derived from this Software may not be called "DOM4J"
218  * nor may "DOM4J" appear in their names without prior written
219  * permission of MetaStuff, Ltd. DOM4J is a registered
220  * trademark of MetaStuff, Ltd.
221  *
222  * 5. Due credit should be given to the DOM4J Project
223  * (http://dom4j.org/).
224  *
225  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
226  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
227  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
228  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
229  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
230  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
231  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
232  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
234  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
235  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
236  * OF THE POSSIBILITY OF SUCH DAMAGE.
237  *
238  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
239  *
240  * $Id: TestDOM.java,v 1.2 2003/07/12 12:13:06 per_nyfelt Exp $
241  */

242
Popular Tags