1
2
3
4
5
6
7
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
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232