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: TestDetach.java,v 1.7 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  /*** A test harness to test the detach() method on root elements
17    *
18    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
19    * @version $Revision: 1.7 $
20    */
21  public class TestDetach 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( TestDetach.class );
29      }
30      
31      public TestDetach(String name) {
32          super(name);
33      }
34  
35      // Test case(s)
36      //-------------------------------------------------------------------------                    
37      public void testRoot() throws Exception {
38          document.setName( "doc1" );
39          
40          Element root = document.getRootElement();
41          assertTrue( "Has root element", root != null );
42          assertTrue( "Root has no parent", root.getParent() == null );
43          
44          root.detach();
45          
46          assertTrue( "Detached root now has no document", root.getDocument() == null );
47          assertTrue( "Original doc now has no root element", document.getRootElement() == null );
48          
49          Document doc2 = DocumentHelper.createDocument();
50          doc2.setName( "doc2" );
51          
52          assertTrue( "Doc2 has no root element", doc2.getRootElement() == null );
53          
54          doc2.setRootElement( root );
55          
56          assertTrue( "Doc2 has now has root element", doc2.getRootElement() == root );        
57          assertTrue( "Root element now has document", root.getDocument() == doc2 );
58          
59          
60          Document doc3 = DocumentHelper.createDocument();
61          doc3.setName( "doc3" );
62          doc3.addElement( "foo" );
63          
64          assertTrue( "Doc3 has root element", doc3.getRootElement() != null );
65          
66          root = doc2.getRootElement();
67          root.detach();
68          doc3.setRootElement( root );
69          
70          assertTrue( "Doc3 now has root element", doc3.getRootElement() == root );        
71          assertTrue( "Root element now has a document", root.getDocument() == doc3 );
72          assertTrue( "Doc2 has no root element", doc2.getRootElement() == null );        
73      }
74  }
75  
76  
77  
78  
79  /*
80   * Redistribution and use of this software and associated documentation
81   * ("Software"), with or without modification, are permitted provided
82   * that the following conditions are met:
83   *
84   * 1. Redistributions of source code must retain copyright
85   *    statements and notices.  Redistributions must also contain a
86   *    copy of this document.
87   *
88   * 2. Redistributions in binary form must reproduce the
89   *    above copyright notice, this list of conditions and the
90   *    following disclaimer in the documentation and/or other
91   *    materials provided with the distribution.
92   *
93   * 3. The name "DOM4J" must not be used to endorse or promote
94   *    products derived from this Software without prior written
95   *    permission of MetaStuff, Ltd.  For written permission,
96   *    please contact dom4j-info@metastuff.com.
97   *
98   * 4. Products derived from this Software may not be called "DOM4J"
99   *    nor may "DOM4J" appear in their names without prior written
100  *    permission of MetaStuff, Ltd. DOM4J is a registered
101  *    trademark of MetaStuff, Ltd.
102  *
103  * 5. Due credit should be given to the DOM4J Project - 
104  *    http://www.dom4j.org
105  *
106  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
107  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
108  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
109  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
110  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
111  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
112  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
113  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
114  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
115  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
116  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
117  * OF THE POSSIBILITY OF SUCH DAMAGE.
118  *
119  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
120  *
121  * $Id: TestDetach.java,v 1.7 2004/06/25 08:03:47 maartenc Exp $
122  */