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: TestXPath.java,v 1.22 2004/06/25 08:03:47 maartenc Exp $
8 */
9
10 package org.dom4j;
11
12 import java.util.List;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16 import junit.textui.TestRunner;
17
18 import org.dom4j.tree.DefaultElement;
19 import org.dom4j.xpath.DefaultXPath;
20
21 /*** A test harness to test XPath expression evaluation in DOM4J
22 *
23 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
24 * @version $Revision: 1.22 $
25 */
26
27 public class TestXPath extends AbstractTestCase {
28
29 protected static boolean VERBOSE = true;
30
31 protected static String[] paths =
32 {
33 ".",
34 "*",
35 "/",
36 "/.",
37 "/*",
38 "/node()",
39 "/child::node()",
40 "/self::node()",
41 "root",
42 "/root",
43 "/root/author",
44 "text()",
45 "//author",
46 "//author/text()",
47 "//@location",
48 "//attribute::*",
49 "//namespace::*",
50 "normalize-space(/root)",
51 "//author[@location]",
52 "//author[@location='UK']",
53 "root|author",
54 "//*[.='James Strachan']",
55 "//root/author[1]",
56 "normalize-space(/root/author)",
57 "normalize-space(' a b c d ')",
58 "//root|//author[1]|//author[2]",
59 "//root/author[2]",
60 "//root/author[3]" };
61
62 public static void main(String[] args) {
63
64 TestRunner.run(suite());
65
66 }
67
68 public static Test suite() {
69
70 return new TestSuite(TestXPath.class);
71
72 }
73
74 public TestXPath(String name) {
75
76 super(name);
77
78 }
79
80 // Test case(s)
81
82 //-------------------------------------------------------------------------
83
84 public void testXPaths() throws Exception {
85
86 int size = paths.length;
87
88 for (int i = 0; i < size; i++) {
89
90 testXPath(paths[i]);
91
92 }
93
94 }
95
96 public void testCreateXPathBug() throws Exception {
97
98 Element element = new DefaultElement( "foo" );
99 XPath xpath = element.createXPath( "//bar" );
100
101 assertTrue("created a valid XPath: " + xpath != null );
102 }
103
104 public void testBug857704() throws Exception {
105 Document doc = DocumentHelper.parseText("<foo xmlns:bar='http://blort'/>");
106 doc.selectNodes("//*[preceding-sibling::*]"); // shouldn't throw NPE
107 }
108
109 public void testBooleanValueOf() throws Exception {
110 Document doc = DocumentHelper.parseText("<root><foo>blah</foo></root>");
111
112 XPath path = new DefaultXPath("//root");
113 assertTrue(path.booleanValueOf(doc));
114
115 path = new DefaultXPath("//root2");
116 assertFalse(path.booleanValueOf(doc));
117 }
118
119 // Implementation methods
120
121 //-------------------------------------------------------------------------
122
123 protected void testXPath(String xpathExpression) {
124
125 log("Searched path: " + xpathExpression);
126
127 XPath xpath = DocumentHelper.createXPath(xpathExpression);
128
129 List list = xpath.selectNodes(document);
130
131 log("Found : " + list.size() + " result(s)");
132
133 if (VERBOSE) {
134
135 log("...........................................");
136
137 log("XPath: :" + xpath);
138
139 log("...........................................");
140
141 }
142
143 log("Results");
144
145 if (list == null) {
146
147 log("null");
148
149 }
150
151 else {
152
153 log("[");
154
155 for (int i = 0, size = list.size(); i < size; i++) {
156
157 Object object = list.get(i);
158
159 String text = "null";
160
161 if (object instanceof Node) {
162
163 Node node = (Node) object;
164
165 text = node.asXML();
166
167 }
168
169 else if (object != null) {
170
171 text = object.toString();
172
173 }
174
175 log(" " + text);
176
177 }
178
179 log("]");
180
181 }
182
183 log("...........................................");
184
185 }
186
187 }
188
189 /*
190 * Redistribution and use of this software and associated documentation
191 * ("Software"), with or without modification, are permitted provided
192 * that the following conditions are met:
193 *
194 * 1. Redistributions of source code must retain copyright
195 * statements and notices. Redistributions must also contain a
196 * copy of this document.
197 *
198 * 2. Redistributions in binary form must reproduce the
199 * above copyright notice, this list of conditions and the
200 * following disclaimer in the documentation and/or other
201 * materials provided with the distribution.
202 *
203 * 3. The name "DOM4J" must not be used to endorse or promote
204 * products derived from this Software without prior written
205 * permission of MetaStuff, Ltd. For written permission,
206 * please contact dom4j-info@metastuff.com.
207 *
208 * 4. Products derived from this Software may not be called "DOM4J"
209 * nor may "DOM4J" appear in their names without prior written
210 * permission of MetaStuff, Ltd. DOM4J is a registered
211 * trademark of MetaStuff, Ltd.
212 *
213 * 5. Due credit should be given to the DOM4J Project -
214 * http://www.dom4j.org
215 *
216 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
217 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
218 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
219 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
220 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
221 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
222 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
223 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
225 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
226 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
227 * OF THE POSSIBILITY OF SUCH DAMAGE.
228 *
229 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
230 *
231 * $Id: TestXPath.java,v 1.22 2004/06/25 08:03:47 maartenc Exp $
232 */