KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestDetach


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
18
19 /** A test harness to test the detach() method on root elements
20   *
21   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
22   * @version $Revision: 1.1 $
23   */

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

126
Popular Tags