1
2
3
4
5
6
7
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171