1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 package org.jaxen;
65
66 import junit.framework.TestCase;
67
68 import org.jaxen.expr.DefaultXPathFactory;
69 import org.jaxen.expr.XPathExpr;
70 import org.jaxen.saxpath.XPathReader;
71 import org.jaxen.saxpath.XPathSyntaxException;
72 import org.jaxen.saxpath.helpers.XPathReaderFactory;
73
74 public class JaxenHandlerTest extends TestCase
75 {
76 String[] ignore_paths = {
77 "foo[.='bar']",
78 "foo[.!='bar']",
79 };
80
81 String[] paths = {
82 "/",
83 "*",
84 "//foo",
85 "/*",
86 "/.",
87 "/foo[/bar[/baz]]",
88 "/foo/bar/baz[(1 or 2) + 3 * 4 + 8 and 9]",
89 "/foo/bar/baz",
90 ".[1]",
91 "self::node()",
92 ".",
93 "count(/)",
94 "foo[1]",
95 "/baz[(1 or 2) + 3 * 4 + 8 and 9]",
96 "foo/bar[/baz[(1 or 2) - 3 mod 4 + 8 and 9 div 8]]",
97 "foo/bar/yeah:baz[a/b/c and toast]",
98 "/foo/bar[../x='123']",
99 "/foo[@bar='1234']",
100 "foo|bar",
101 "/foo|/bar[@id='1234']",
102 "count(//author/attribute::*)",
103 "$author",
104 "10 + $foo",
105 "10 + (count(descendant::author) * 5)",
106 "10 + count(descendant::author) * 5",
107 "2 + (2 * 5)",
108 "sum(count(//author), 5)",
109 "sum(count(//author),count(//author/attribute::*))",
110 "12 + sum(count(//author),count(//author/attribute::*)) div 2",
111 "text()[.='foo']",
112 "/*/*[@id='123']",
113 "/child::node()/child::node()[@id='_13563275']",
114 "$foo:bar",
115
116 "/foo/bar[@a='1' and @c!='2']",
117 };
118
119 String[] bogusPaths = { };
120
121 String[] ignore_bogusPaths = {
122
123 "/foo/bar/",
124
125
126
127 "12 + sum(count(//author),count(//author/attribute::*)) / 2",
128 "id()/2",
129 "+"
130 };
131
132 public JaxenHandlerTest(String name)
133 {
134 super( name );
135 }
136
137 public void setUp()
138 {
139 }
140
141 public void tearDown()
142 {
143 }
144
145 public void testValidPaths()
146 {
147
148 String path = null;
149
150 try
151 {
152 XPathReader reader = XPathReaderFactory.createReader();
153
154 JaxenHandler handler = new JaxenHandler();
155
156 handler.setXPathFactory( new DefaultXPathFactory() );
157
158 reader.setXPathHandler( handler );
159
160 for ( int i = 0; i < paths.length; i++ ) {
161 path = paths[i];
162
163
164
165
166
167 reader.parse(path);
168
169 XPathExpr xpath = handler.getXPathExpr(false);
170
171
172
173
174
175
176 xpath = handler.getXPathExpr();
177
178
179
180
181
182
183
184 }
185 }
186 catch (Exception e)
187 {
188 e.printStackTrace();
189 fail( path + " -> " + e.getMessage() );
190 }
191 }
192
193 public void testBogusPaths()
194 {
195 try
196 {
197 XPathReader reader = XPathReaderFactory.createReader();
198
199 JaxenHandler handler = new JaxenHandler();
200
201 handler.setXPathFactory( new DefaultXPathFactory() );
202
203 reader.setXPathHandler( handler );
204
205 for ( int i = 0; i < bogusPaths.length; i++ ) {
206 String path = bogusPaths[i];
207
208 System.out.println("-----------------");
209 System.out.println( "parsing bogus path : " + path );
210 System.out.println("-----------------");
211
212 try
213 {
214 reader.parse(path);
215
216 XPathExpr xpath = handler.getXPathExpr(false);
217
218 System.out.println( "Parsed as: " + xpath );
219
220 fail( "Parsed bogus path as: " + xpath );
221 }
222 catch (XPathSyntaxException e)
223 {
224
225 System.out.println("-----------------");
226
227 System.out.println( "Exception: ");
228 System.out.println( e.getMultilineMessage() );
229 System.out.println("-----------------");
230 }
231 }
232 }
233 catch (Exception e)
234 {
235 e.printStackTrace();
236 fail( e.getMessage() );
237 }
238 }
239 }