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: TestSetContent.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import junit.textui.TestRunner;
15  
16  /*** Tests the setContent method
17    *
18    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
19    * @version $Revision: 1.5 $
20    */
21  public class TestSetContent extends AbstractTestCase {
22  
23      public static void main( String[] args ) {
24          TestRunner.run( suite() );
25      }
26      
27      public static Test suite() {
28          return new TestSuite( TestSetContent.class );
29      }
30      
31      public TestSetContent(String name) {
32          super(name);
33      }
34  
35      // Test case(s)
36      //-------------------------------------------------------------------------                    
37      public void testDocument() throws Exception {
38          document.setName( "doc1" );
39                  
40          Element oldRoot = document.getRootElement();
41          
42          assertTrue( "Current root has document", oldRoot.getDocument() == document );
43          
44          Document doc2 = DocumentHelper.createDocument();
45          doc2.setName( "doc2" );
46          
47          assertTrue( "Doc2 has no root element", doc2.getRootElement() == null );
48          
49          doc2.setContent( document.content() );
50          
51          Element newRoot = doc2.getRootElement();
52          
53          assertTrue( "Current root has document", oldRoot.getDocument() == document );
54          
55          assertTrue( "Doc2 has now has root element", newRoot != null );        
56          assertTrue( "Doc2 has different root element", newRoot != oldRoot );        
57          assertTrue( "Root element now has document", newRoot.getDocument() == doc2 );
58          
59          testParent( doc2.getRootElement() );
60          testDocument( doc2, doc2 );
61          
62          doc2.clearContent();
63          
64          assertTrue( "New Doc has no root", doc2.getRootElement() == null );
65          assertTrue( "New root has no document", newRoot.getDocument() == null );
66      }
67          
68          
69      public void testRootElement() throws Exception {
70          Document doc3 = DocumentHelper.createDocument();
71          doc3.setName( "doc3" );
72          Element root = doc3.addElement( "root" );
73          Element oldElement = root.addElement( "old" );
74          
75          assertTrue( "Doc3 has root element", root != null );
76          
77          root.setContent( document.getRootElement().content() );
78          
79          assertTrue( "Doc3's root now has content", root.nodeCount() > 0 );
80          assertTrue( "Old element has no parent now", oldElement.getParent() == null );
81          assertTrue( "Old element has no document now", oldElement.getDocument() == null );
82          
83          testParent( root );
84          testDocument( root, doc3 );
85      }
86      
87      /*** Tests all the children of the branch have the correct parent */
88      protected void testParent( Branch parent ) {
89          for ( int i = 0, size = parent.nodeCount(); i < size; i++ ) {
90              Node node = parent.node(i);
91              assertTrue( "Child node of root has parent of root", node.getParent() == parent );
92          }
93      }
94      
95      /*** Tests all the children of the branch have the correct document */
96      protected void testDocument( Branch branch, Document doc ) {
97          for ( int i = 0, size = branch.nodeCount(); i < size; i++ ) {
98              Node node = branch.node(i);
99              assertTrue( "Node has correct document", node.getDocument() == doc );
100         }
101     }
102 }
103 
104 
105 
106 
107 /*
108  * Redistribution and use of this software and associated documentation
109  * ("Software"), with or without modification, are permitted provided
110  * that the following conditions are met:
111  *
112  * 1. Redistributions of source code must retain copyright
113  *    statements and notices.  Redistributions must also contain a
114  *    copy of this document.
115  *
116  * 2. Redistributions in binary form must reproduce the
117  *    above copyright notice, this list of conditions and the
118  *    following disclaimer in the documentation and/or other
119  *    materials provided with the distribution.
120  *
121  * 3. The name "DOM4J" must not be used to endorse or promote
122  *    products derived from this Software without prior written
123  *    permission of MetaStuff, Ltd.  For written permission,
124  *    please contact dom4j-info@metastuff.com.
125  *
126  * 4. Products derived from this Software may not be called "DOM4J"
127  *    nor may "DOM4J" appear in their names without prior written
128  *    permission of MetaStuff, Ltd. DOM4J is a registered
129  *    trademark of MetaStuff, Ltd.
130  *
131  * 5. Due credit should be given to the DOM4J Project - 
132  *    http://www.dom4j.org
133  *
134  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
135  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
136  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
137  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
138  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
139  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
140  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
141  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
142  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
143  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
144  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
145  * OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
148  *
149  * $Id: TestSetContent.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $
150  */