1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 /***
13 * Tests the setContent method
14 *
15 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
16 * @version $Revision: 1.3 $
17 */
18 public class SetContentTest extends AbstractTestCase {
19 public static void main(String[] args) {
20 TestRunner.run(SetContentTest.class);
21 }
22
23
24
25 public void testDocument() throws Exception {
26 document.setName("doc1");
27
28 Element oldRoot = document.getRootElement();
29
30 assertTrue("Current root has document",
31 oldRoot.getDocument() == document);
32
33 Document doc2 = DocumentHelper.createDocument();
34 doc2.setName("doc2");
35
36 assertTrue("Doc2 has no root element", doc2.getRootElement() == null);
37
38 doc2.setContent(document.content());
39
40 Element newRoot = doc2.getRootElement();
41
42 assertTrue("Current root has document",
43 oldRoot.getDocument() == document);
44
45 assertTrue("Doc2 has now has root element", newRoot != null);
46 assertTrue("Doc2 has different root element", newRoot != oldRoot);
47 assertTrue("Root element now has document",
48 newRoot.getDocument() == doc2);
49
50 testParent(doc2.getRootElement());
51 testDocument(doc2, doc2);
52
53 doc2.clearContent();
54
55 assertTrue("New Doc has no root", doc2.getRootElement() == null);
56 assertTrue("New root has no document", newRoot.getDocument() == null);
57 }
58
59 public void testRootElement() throws Exception {
60 Document doc3 = DocumentHelper.createDocument();
61 doc3.setName("doc3");
62
63 Element root = doc3.addElement("root");
64 Element oldElement = root.addElement("old");
65
66 assertTrue("Doc3 has root element", root != null);
67
68 root.setContent(document.getRootElement().content());
69
70 assertTrue("Doc3's root now has content", root.nodeCount() > 0);
71 assertTrue("Old element has no parent now",
72 oldElement.getParent() == null);
73 assertTrue("Old element has no document now",
74 oldElement.getDocument() == null);
75
76 testParent(root);
77 testDocument(root, doc3);
78 }
79
80 /***
81 * Tests all the children of the branch have the correct parent
82 *
83 * @param parent
84 * DOCUMENT ME!
85 */
86 protected void testParent(Branch parent) {
87 for (int i = 0, size = parent.nodeCount(); i < size; i++) {
88 Node node = parent.node(i);
89 assertTrue("Child node of root has parent of root", node
90 .getParent() == parent);
91 }
92 }
93
94 /***
95 * Tests all the children of the branch have the correct document
96 *
97 * @param branch
98 * DOCUMENT ME!
99 * @param doc
100 * DOCUMENT ME!
101 */
102 protected void testDocument(Branch branch, Document doc) {
103 for (int i = 0, size = branch.nodeCount(); i < size; i++) {
104 Node node = branch.node(i);
105 assertTrue("Node has correct document", node.getDocument() == doc);
106 }
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120
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