2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
10
import junit.textui.TestRunner;
12
import java.util.Iterator;
13
import java.util.List;
16
* A test harness to test the parent relationship and use of the {@link
17
* Node#asXPathResult} method.
19
* @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
20
* @version $Revision: 1.3 $
22
public class ParentTest extends AbstractTestCase {
23
public static void main(String[] args) {
24
TestRunner.run(ParentTest.class);
28
// -------------------------------------------------------------------------
29
public void testDocument() throws Exception {
30
testParentRelationship(document.getRootElement());
33
public void testFragment() throws Exception {
34
DocumentFactory factory = new DocumentFactory();
35
Element root = factory.createElement("root");
36
Element first = root.addElement("child");
37
Element second = root.addElement("child");
39
testXPathNode(root, first);
40
testXPathNode(root, second);
43
// Implementation methods
44
// -------------------------------------------------------------------------
45
protected void testParentRelationship(Element parent, List content) {
46
for (Iterator iter = content.iterator(); iter.hasNext();) {
47
Object object = iter.next();
49
if (object instanceof Element) {
50
testParentRelationship((Element) object);
53
testXPathNode(parent, (Node) object);
57
protected void testParentRelationship(Element element) {
58
testParentRelationship(element, element.attributes());
59
testParentRelationship(element, element.content());
62
protected void testXPathNode(Element parent, Node node) {
63
if (node.supportsParent()) {
65
log("Parent: " + parent);
66
log("getParent(): " + node.getParent());
68
assertTrue("getParent() returns parent for: " + node, node
69
.getParent() == parent);
71
// lets create an XPath node
72
Node xpathNode = node.asXPathResult(parent);
73
assertTrue("XPath Node supports parent for: " + xpathNode,
74
xpathNode.supportsParent());
75
assertTrue("getParent() returns parent for: " + xpathNode,
76
xpathNode.getParent() == parent);
82
* Redistribution and use of this software and associated documentation
83
* ("Software"), with or without modification, are permitted provided that the
84
* following conditions are met:
86
* 1. Redistributions of source code must retain copyright statements and
87
* notices. Redistributions must also contain a copy of this document.
89
* 2. Redistributions in binary form must reproduce the above copyright notice,
90
* this list of conditions and the following disclaimer in the documentation
91
* and/or other materials provided with the distribution.
93
* 3. The name "DOM4J" must not be used to endorse or promote products derived
94
* from this Software without prior written permission of MetaStuff, Ltd. For
95
* written permission, please contact dom4j-info@metastuff.com.
97
* 4. Products derived from this Software may not be called "DOM4J" nor may
98
* "DOM4J" appear in their names without prior written permission of MetaStuff,
99
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
101
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
103
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
104
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
105
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
106
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
107
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
108
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
109
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
110
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
111
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
112
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
113
* POSSIBILITY OF SUCH DAMAGE.
115
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.