KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestSetContent


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: TestSetContent.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.Branch;
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.Node;
19 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
20
21 /** Tests the setContent method
22   *
23   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
24   * @version $Revision: 1.1 $
25   */

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

156
Popular Tags