1   /*
2    * Copyright 2001-2005 (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   
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      // Test case(s)
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  * Redistribution and use of this software and associated documentation
112  * ("Software"), with or without modification, are permitted provided that the
113  * following conditions are met:
114  * 
115  * 1. Redistributions of source code must retain copyright statements and
116  * notices. Redistributions must also contain a copy of this document.
117  * 
118  * 2. Redistributions in binary form must reproduce the above copyright notice,
119  * this list of conditions and the following disclaimer in the documentation
120  * and/or other materials provided with the distribution.
121  * 
122  * 3. The name "DOM4J" must not be used to endorse or promote products derived
123  * from this Software without prior written permission of MetaStuff, Ltd. For
124  * written permission, please contact dom4j-info@metastuff.com.
125  * 
126  * 4. Products derived from this Software may not be called "DOM4J" nor may
127  * "DOM4J" appear in their names without prior written permission of MetaStuff,
128  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
129  * 
130  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
131  * 
132  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
133  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
134  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
135  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
136  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
137  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
138  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
139  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
140  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
141  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
142  * POSSIBILITY OF SUCH DAMAGE.
143  * 
144  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
145  */