KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestClone


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: TestClone.java,v 1.3 2003/09/29 15:43:58 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.Document;
16 import org.dom4j.Element;
17 import org.dom4j.io.OutputFormat;
18 import org.dom4j.io.XMLWriter;
19 import org.dom4j.util.NodeComparator;
20
21 import java.util.Comparator JavaDoc;
22
23 /** A test harness to test the clone() methods on Nodes
24   *
25   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
26   * @version $Revision: 1.3 $
27   */

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

190
Popular Tags