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