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