KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestParent


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: TestParent.java,v 1.2 2003/11/02 18:31:28 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.Element;
16 import org.dom4j.Node;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 /** A test harness to test the parent relationship and use of the
22   * {@link org.dom4j.Node#asXPathResult} method.
23   *
24   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25   * @version $Revision: 1.2 $
26   */

27 public class TestParent extends AbstractTestCase {
28
29     public static void main( String JavaDoc[] args ) {
30         TestRunner.run( suite() );
31     }
32
33     public static Test suite() {
34         return new TestSuite( TestParent.class );
35     }
36
37     public TestParent(String JavaDoc name) {
38         super(name);
39     }
40
41     // Test case(s)
42
//-------------------------------------------------------------------------
43
public void testDocument() throws Exception JavaDoc {
44         testParentRelationship( document.getRootElement() );
45     }
46
47     public void testFragment() throws Exception JavaDoc {
48
49         Element root = nodeFactory.createElement( "root" );
50         Element first = root.addElement( "child" );
51         Element second = root.addElement( "child" );
52
53         testXPathNode( root, first );
54         testXPathNode( root, second );
55     }
56
57     // Implementation methods
58
//-------------------------------------------------------------------------
59
protected void testParentRelationship( Element parent, List JavaDoc content ) {
60         for ( Iterator JavaDoc iter = content.iterator(); iter.hasNext(); ) {
61             Object JavaDoc object = iter.next();
62             if ( object instanceof Element ) {
63                 testParentRelationship( (Element) object );
64             }
65             testXPathNode( parent, (Node) object );
66         }
67     }
68
69     protected void testParentRelationship( Element element ) {
70         testParentRelationship( element, element.attributes() );
71         testParentRelationship( element, element.content() );
72     }
73
74
75     protected void testXPathNode( Element parent, Node node ) {
76         if ( node.supportsParent() ) {
77             log( "Node: " + node );
78             log( "Parent: " + parent );
79             log( "getParent(): " + node.getParent() );
80
81             assertTrue( "getParent() returns parent for: " + node, node.getParent() == parent );
82         }
83         else {
84             // lets create an XPath node
85
Node xpathNode = node.asXPathResult( parent );
86             assertTrue( "XPath Node supports parent for: " + xpathNode, xpathNode.supportsParent() );
87             assertTrue( "getParent() returns parent for: " + xpathNode, xpathNode.getParent() == parent );
88         }
89     }
90 }
91
92
93
94
95 /*
96  * Redistribution and use of this software and associated documentation
97  * ("Software"), with or without modification, are permitted provided
98  * that the following conditions are met:
99  *
100  * 1. Redistributions of source code must retain copyright
101  * statements and notices. Redistributions must also contain a
102  * copy of this document.
103  *
104  * 2. Redistributions in binary form must reproduce the
105  * above copyright notice, this list of conditions and the
106  * following disclaimer in the documentation and/or other
107  * materials provided with the distribution.
108  *
109  * 3. The name "DOM4J" must not be used to endorse or promote
110  * products derived from this Software without prior written
111  * permission of MetaStuff, Ltd. For written permission,
112  * please contact dom4j-info@metastuff.com.
113  *
114  * 4. Products derived from this Software may not be called "DOM4J"
115  * nor may "DOM4J" appear in their names without prior written
116  * permission of MetaStuff, Ltd. DOM4J is a registered
117  * trademark of MetaStuff, Ltd.
118  *
119  * 5. Due credit should be given to the DOM4J Project
120  * (http://dom4j.org/).
121  *
122  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
123  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
124  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
125  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
126  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
127  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
128  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
129  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
130  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
131  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
132  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
133  * OF THE POSSIBILITY OF SUCH DAMAGE.
134  *
135  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
136  *
137  * $Id: TestParent.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
138  */

139
Popular Tags