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: TestClone.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.util.Comparator;
13  
14  import junit.framework.Test;
15  import junit.framework.TestSuite;
16  import junit.textui.TestRunner;
17  
18  import org.dom4j.io.OutputFormat;
19  import org.dom4j.io.XMLWriter;
20  import org.dom4j.util.NodeComparator;
21  
22  /*** A test harness to test the clone() methods on Nodes
23    *
24    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25    * @version $Revision: 1.8 $
26    */
27  public class TestClone extends AbstractTestCase {
28  
29      private static final boolean VERBOSE = false;
30      
31      private Comparator comparator = new NodeComparator();
32      
33      
34      public static void main( String[] args ) {
35          TestRunner.run( suite() );
36      }
37      
38      public static Test suite() {
39          return new TestSuite( TestClone.class );
40      }
41      
42      public TestClone(String name) {
43          super(name);
44      }
45  
46      // Test case(s)
47      //-------------------------------------------------------------------------                    
48      public void testDocumentClone() throws Exception {
49          document.setName( "doc1" );
50          
51          Document doc2 = (Document) document.clone();
52          
53          assertTrue( "Returned a new document", document != doc2 );
54          
55          if ( VERBOSE ) {        
56              XMLWriter writer = new XMLWriter( 
57                  System.out, OutputFormat.createPrettyPrint() 
58              );
59          
60              log( "document1" );
61              writer.write( document );
62  
63              log( "document2" );
64              writer.write( doc2 );
65          }
66          
67          assertTrue( "Documents are equal", comparator.compare( document, doc2 ) == 0 );
68      }
69      
70      public void testRootElementClone() throws Exception {
71          testElementClone( document.getRootElement() );
72      }
73      
74      public void testAuthorElementClone() throws Exception {
75          testElementClone( (Element) document.selectSingleNode( "//author" ) );
76      }
77      
78      public void testRootCompare1() throws Exception {                
79          Document doc2 = (Document) document.clone();
80          Element author = doc2.getRootElement();
81          author.addAttribute( "foo", "bar" );
82          
83          assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 );
84      }
85      
86      public void testRootCompare2() throws Exception {                
87          Document doc2 = (Document) document.clone();
88          Element author = doc2.getRootElement();
89          
90          author.addText( "foo" );
91          
92          assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 );
93      }
94      
95      public void testAuthorCompare1() throws Exception {                
96          Document doc2 = (Document) document.clone();
97          Element author = (Element) doc2.selectSingleNode( "//author" );
98          author.addAttribute( "name", "James Strachan" );
99          
100         assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 );
101     }
102     
103     public void testAuthorCompare2() throws Exception {                
104         Document doc2 = (Document) document.clone();
105         Element author = (Element) doc2.selectSingleNode( "//author" );
106         
107         author.addText( "foo" );
108         
109         assertTrue( "Documents are not equal", comparator.compare( document, doc2 ) != 0 );
110     }
111     
112     
113     protected void testElementClone( Element element ) throws Exception {        
114         Element element2 = (Element) element.clone();
115         
116         assertTrue( "Returned a new Element", element2 != element );
117         assertTrue( "New element has no parent", element2.getParent() == null );
118         assertTrue( "New element has no Document", element2.getDocument() == null );
119         
120         assertTrue( "Element fragments are equal", comparator.compare( element, element2 ) == 0 );
121     }
122     
123 }
124 
125 
126 
127 
128 /*
129  * Redistribution and use of this software and associated documentation
130  * ("Software"), with or without modification, are permitted provided
131  * that the following conditions are met:
132  *
133  * 1. Redistributions of source code must retain copyright
134  *    statements and notices.  Redistributions must also contain a
135  *    copy of this document.
136  *
137  * 2. Redistributions in binary form must reproduce the
138  *    above copyright notice, this list of conditions and the
139  *    following disclaimer in the documentation and/or other
140  *    materials provided with the distribution.
141  *
142  * 3. The name "DOM4J" must not be used to endorse or promote
143  *    products derived from this Software without prior written
144  *    permission of MetaStuff, Ltd.  For written permission,
145  *    please contact dom4j-info@metastuff.com.
146  *
147  * 4. Products derived from this Software may not be called "DOM4J"
148  *    nor may "DOM4J" appear in their names without prior written
149  *    permission of MetaStuff, Ltd. DOM4J is a registered
150  *    trademark of MetaStuff, Ltd.
151  *
152  * 5. Due credit should be given to the DOM4J Project - 
153  *    http://www.dom4j.org
154  *
155  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
156  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
157  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
158  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
159  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
160  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
161  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
162  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
163  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
164  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
165  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
166  * OF THE POSSIBILITY OF SUCH DAMAGE.
167  *
168  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
169  *
170  * $Id: TestClone.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $
171  */