KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > xpath > TestGetPath


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: TestGetPath.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
8  */

9
10 package test.dom4j.xpath;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.*;
16 import org.dom4j.io.SAXReader;
17 import test.dom4j.AbstractTestCase;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /** Test harness for the GetPath() method
23   *
24   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestGetPath 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( TestGetPath.class );
35     }
36
37     public TestGetPath(String JavaDoc name) {
38         super(name);
39     }
40
41     // Test case(s)
42
//-------------------------------------------------------------------------
43
public void testGetPath() throws Exception JavaDoc {
44         log( "Testing paths" );
45
46         //testBranchPath( document );
47

48         testPath( document, "/" );
49
50         Element root = document.getRootElement();
51
52         testPath( root, "/root" );
53
54         List JavaDoc elements = root.elements();
55
56         testPath( (Node) elements.get(0), "/root/author", "/root/author[1]" );
57
58         for ( int i = 0, size = elements.size(); i < size; i++ ) {
59             String JavaDoc path = "/root/author";
60             String JavaDoc uniquePath = "/root/author";
61             String JavaDoc pathRel = "author";
62             String JavaDoc uniquePathRel = "author";
63             if ( size > 1 ) {
64                 uniquePath = "/root/author[" + (i + 1) + "]";
65                 uniquePathRel = "author[" + (i + 1) + "]";
66             }
67             Element element = (Element) elements.get(i);
68             testPath( element, path, uniquePath );
69             testRelativePath( root, element, pathRel, uniquePathRel );
70
71             Attribute attribute = element.attribute( "name" );
72             testPath( attribute, path + "/@name", uniquePath + "/@name" );
73             testRelativePath( root, attribute, pathRel + "/@name", uniquePathRel + "/@name" );
74
75             Element child = element.element( "url" );
76             testPath( child, path + "/url", uniquePath + "/url" );
77             testRelativePath( root, child, pathRel + "/url", uniquePathRel + "/url" );
78         }
79     }
80
81     public void testDefaultNamespace() throws Exception JavaDoc {
82         SAXReader reader = new SAXReader();
83         Document doc = reader.read( "xml/test/defaultNamespace.xml" );
84         Element root = doc.getRootElement();
85         testPath( root, "/*[name()='a']" );
86
87         Element child = (Element) root.elements().get(0);
88         testPath( child, "/*[name()='a']/*[name()='b']" );
89         testRelativePath( root, child, "*[name()='b']" );
90     }
91
92
93     protected void testPath(Node node, String JavaDoc value) {
94         testPath( node, value, value );
95     }
96
97     protected void testPath(Node node, String JavaDoc path, String JavaDoc uniquePath) {
98         assertEquals( "getPath expression should be what is expected", path, node.getPath() );
99         assertEquals( "getUniquePath expression should be what is expected", uniquePath, node.getUniquePath() );
100     }
101
102     protected void testRelativePath( Element context, Node node, String JavaDoc pathRel ) {
103         testRelativePath( context, node, pathRel, pathRel );
104     }
105
106     protected void testRelativePath( Element context, Node node, String JavaDoc pathRel, String JavaDoc uniquePathRel ) {
107         assertEquals( "relative getPath expression should be what is expected", pathRel, node.getPath( context ) );
108         assertEquals( "relative getUniquePath expression should be what is expected", uniquePathRel, node.getUniquePath( context ) );
109     }
110
111
112     protected void testBranchPath(Branch branch) {
113         testNodePath( branch );
114
115         if ( branch instanceof Element ) {
116             Element element = (Element) branch;
117             for ( Iterator JavaDoc iter = element.attributeIterator(); iter.hasNext(); ) {
118                 Node node = (Node) iter.next();
119                 testNodePath( node );
120             }
121         }
122
123         for ( Iterator JavaDoc iter = branch.nodeIterator(); iter.hasNext(); ) {
124             Node node = (Node) iter.next();
125             if ( node instanceof Branch ) {
126                 testBranchPath( (Branch) node );
127             }
128             else {
129                 testNodePath( node );
130             }
131         }
132     }
133
134     protected void testNodePath(Node node) {
135
136         String JavaDoc path = node.getPath();
137
138         log( "Path: " + path + " node: " + node );
139     }
140 }
141
142
143
144
145 /*
146  * Redistribution and use of this software and associated documentation
147  * ("Software"), with or without modification, are permitted provided
148  * that the following conditions are met:
149  *
150  * 1. Redistributions of source code must retain copyright
151  * statements and notices. Redistributions must also contain a
152  * copy of this document.
153  *
154  * 2. Redistributions in binary form must reproduce the
155  * above copyright notice, this list of conditions and the
156  * following disclaimer in the documentation and/or other
157  * materials provided with the distribution.
158  *
159  * 3. The name "DOM4J" must not be used to endorse or promote
160  * products derived from this Software without prior written
161  * permission of MetaStuff, Ltd. For written permission,
162  * please contact dom4j-info@metastuff.com.
163  *
164  * 4. Products derived from this Software may not be called "DOM4J"
165  * nor may "DOM4J" appear in their names without prior written
166  * permission of MetaStuff, Ltd. DOM4J is a registered
167  * trademark of MetaStuff, Ltd.
168  *
169  * 5. Due credit should be given to the DOM4J Project
170  * (http://dom4j.org/).
171  *
172  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
173  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
174  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
175  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
176  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
177  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
178  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
179  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
180  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
181  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
182  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
183  * OF THE POSSIBILITY OF SUCH DAMAGE.
184  *
185  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
186  *
187  * $Id: TestGetPath.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
188  */

189
Popular Tags