KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestCopy


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: TestCopy.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Attribute;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18
19 import java.util.List JavaDoc;
20
21 /** A test harness to test the copy() methods on Element
22   *
23   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
24   * @version $Revision: 1.1 $
25   */

26 public class TestCopy extends AbstractTestCase {
27
28     public static void main( String JavaDoc[] args ) {
29         TestRunner.run( suite() );
30     }
31
32     public static Test suite() {
33         return new TestSuite( TestCopy.class );
34     }
35
36     public TestCopy(String JavaDoc name) {
37         super(name);
38     }
39
40     // Test case(s)
41
//-------------------------------------------------------------------------
42
public void testRoot() throws Exception JavaDoc {
43         document.setName( "doc1" );
44
45         Element root = document.getRootElement();
46         List JavaDoc authors = root.elements( "author" );
47
48         assertTrue( "Should be at least 2 authors", authors.size() == 2 );
49
50         Element author1 = (Element) authors.get(0);
51         Element author2 = (Element) authors.get(1);
52
53         testCopy( root );
54         testCopy( author1 );
55         testCopy( author2 );
56     }
57
58
59     protected void testCopy(Element element) throws Exception JavaDoc {
60         assertTrue( "Not null", element != null );
61
62         int attributeCount = element.attributeCount();
63         int nodeCount = element.nodeCount();
64
65         Element copy = element.createCopy();
66
67         assertTrue( "Same node size after copy", element.nodeCount() == nodeCount );
68         assertTrue( "Same attribute size after copy", element.attributeCount() == attributeCount );
69
70         assertTrue( "Copy has same node size", copy.nodeCount() == nodeCount );
71         assertTrue( "Copy has same attribute size", copy.attributeCount() == attributeCount );
72
73         for (int i = 0; i < attributeCount; i++ ) {
74             Attribute attr1 = element.attribute(i);
75             Attribute attr2 = copy.attribute(i);
76
77             assertTrue( "Attribute: " + i + " name is equal", attr1.getName().equals( attr2.getName() ) );
78             assertTrue( "Attribute: " + i + " value is equal", attr1.getValue().equals( attr2.getValue() ) );
79         }
80
81         for (int i = 0; i < nodeCount; i++ ) {
82             Node node1 = element.node(i);
83             Node node2 = copy.node(i);
84
85             assertTrue( "Node: " + i + " type is equal", node1.getNodeType() == node2.getNodeType() );
86             assertTrue( "Node: " + i + " value is equal", node1.getText().equals( node2.getText() ) );
87         }
88     }
89 }
90
91
92
93
94 /*
95  * Redistribution and use of this software and associated documentation
96  * ("Software"), with or without modification, are permitted provided
97  * that the following conditions are met:
98  *
99  * 1. Redistributions of source code must retain copyright
100  * statements and notices. Redistributions must also contain a
101  * copy of this document.
102  *
103  * 2. Redistributions in binary form must reproduce the
104  * above copyright notice, this list of conditions and the
105  * following disclaimer in the documentation and/or other
106  * materials provided with the distribution.
107  *
108  * 3. The name "DOM4J" must not be used to endorse or promote
109  * products derived from this Software without prior written
110  * permission of MetaStuff, Ltd. For written permission,
111  * please contact dom4j-info@metastuff.com.
112  *
113  * 4. Products derived from this Software may not be called "DOM4J"
114  * nor may "DOM4J" appear in their names without prior written
115  * permission of MetaStuff, Ltd. DOM4J is a registered
116  * trademark of MetaStuff, Ltd.
117  *
118  * 5. Due credit should be given to the DOM4J Project
119  * (http://dom4j.org/).
120  *
121  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
122  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
123  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
124  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
125  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
126  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
127  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
128  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132  * OF THE POSSIBILITY OF SUCH DAMAGE.
133  *
134  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
135  *
136  * $Id: TestCopy.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
137  */

138
Popular Tags