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
65 package org.jaxen.saxpath.base;
66
67 import junit.framework.TestCase;
68
69 public class XPathLexerTest extends TestCase
70 {
71 private XPathLexer lexer;
72 private Token token;
73
74 public XPathLexerTest(String name)
75 {
76 super( name );
77 }
78
79 public void setUp()
80 {
81 }
82
83 public void tearDown()
84 {
85 setLexer( null );
86 setToken( null );
87 }
88
89 public void testNamespace()
90 {
91 setText( "a:b" );
92
93 nextToken();
94
95 assertEquals( TokenTypes.IDENTIFIER,
96 tokenType() );
97
98 assertEquals( "a",
99 tokenText() );
100
101 nextToken();
102
103 assertEquals( TokenTypes.COLON,
104 tokenType() );
105
106 nextToken();
107
108 assertEquals( TokenTypes.IDENTIFIER,
109 tokenType() );
110
111 assertEquals( "b",
112 tokenText() );
113 }
114
115 public void testIdentifier()
116 {
117 setText( "foo" );
118
119 nextToken();
120
121 assertEquals( TokenTypes.IDENTIFIER,
122 tokenType() );
123
124 assertEquals( "foo",
125 tokenText() );
126
127 setText( "foo.bar" );
128
129 nextToken();
130
131 assertEquals( TokenTypes.IDENTIFIER,
132 tokenType() );
133
134 assertEquals( "foo.bar",
135 tokenText() );
136 }
137
138
139 /***
140 * This tests that characters added in XML 1.1 and Unicode 3.0
141 * are not recognized as legal name characters.
142 */
143 public void testBurmeseIdentifierStart()
144 {
145 setText( "\u1000foo" );
146
147 nextToken();
148
149 assertEquals( TokenTypes.ERROR,
150 tokenType() );
151
152 }
153
154 public void testBurmeseIdentifierPart()
155 {
156 setText( "foo\u1000foo" );
157
158 nextToken();
159 assertEquals( "foo",
160 tokenText() );
161 nextToken();
162 assertEquals( TokenTypes.ERROR,
163 tokenType() );
164
165 }
166
167 public void testIdentifierAndOperator()
168 {
169 setText( "foo and bar" );
170
171 nextToken();
172
173 assertEquals( TokenTypes.IDENTIFIER,
174 tokenType() );
175
176 assertEquals( "foo",
177 tokenText() );
178
179 nextToken();
180
181 assertEquals( TokenTypes.AND,
182 tokenType() );
183 nextToken();
184
185 assertEquals( TokenTypes.IDENTIFIER,
186 tokenType() );
187
188 assertEquals( "bar",
189 tokenText() );
190 }
191
192 public void testTrickyIdentifierAndOperator()
193 {
194 setText( "and and and" );
195
196 nextToken();
197
198 assertEquals( TokenTypes.IDENTIFIER,
199 tokenType() );
200
201 assertEquals( "and",
202 tokenText() );
203
204 nextToken();
205
206 assertEquals( TokenTypes.AND,
207 tokenType() );
208 nextToken();
209
210 assertEquals( TokenTypes.IDENTIFIER,
211 tokenType() );
212
213 assertEquals( "and",
214 tokenText() );
215 }
216
217 public void testInteger()
218 {
219 setText( "1234" );
220
221 nextToken();
222
223 assertEquals( TokenTypes.INTEGER,
224 tokenType() );
225
226 assertEquals( "1234",
227 tokenText() );
228 }
229
230 public void testDouble()
231 {
232 setText( "12.34" );
233
234 nextToken();
235
236 assertEquals( TokenTypes.DOUBLE,
237 tokenType() );
238
239 assertEquals( "12.34",
240 tokenText() );
241 }
242
243 public void testDoubleOnlyDecimal()
244 {
245 setText( ".34" );
246
247 nextToken();
248
249 assertEquals( TokenTypes.DOUBLE,
250 tokenType() );
251
252 assertEquals( ".34",
253 tokenText() );
254 }
255
256 public void testNumbersAndMode()
257 {
258 setText( "12.34 mod 3" );
259
260 nextToken();
261
262 assertEquals( TokenTypes.DOUBLE,
263 tokenType() );
264
265 assertEquals( "12.34",
266 tokenText() );
267
268 nextToken();
269
270 assertEquals( TokenTypes.MOD,
271 tokenType() );
272
273 nextToken();
274
275 assertEquals( TokenTypes.INTEGER,
276 tokenType() );
277
278
279 }
280
281 public void testSlash()
282 {
283 setText( "/" );
284
285 nextToken();
286
287 assertEquals( TokenTypes.SLASH,
288 tokenType() );
289 }
290
291 public void testDoubleSlash()
292 {
293 setText( "//" );
294
295 nextToken();
296
297 assertEquals( TokenTypes.DOUBLE_SLASH,
298 tokenType() );
299 }
300
301 public void testIdentifierWithColon()
302 {
303 setText ( "foo:bar" );
304
305 nextToken();
306
307 assertEquals( TokenTypes.IDENTIFIER,
308 tokenType() );
309
310 assertEquals( "foo",
311 tokenText() );
312
313 nextToken();
314
315 assertEquals( TokenTypes.COLON,
316 tokenType() );
317
318 nextToken();
319
320 assertEquals( TokenTypes.IDENTIFIER,
321 tokenType() );
322
323 assertEquals( "bar",
324 tokenText() );
325 }
326
327 public void testEOF()
328 {
329 setText( "foo" );
330
331 nextToken();
332
333 assertEquals( TokenTypes.IDENTIFIER,
334 tokenType() );
335
336 assertEquals( "foo",
337 tokenText() );
338
339 nextToken();
340
341 assertEquals( TokenTypes.EOF,
342 tokenType() );
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 public void testWhitespace()
365 {
366 setText ( " / \tfoo:bar" );
367
368 nextToken();
369
370 assertEquals( TokenTypes.SLASH,
371 tokenType() );
372 nextToken();
373
374 assertEquals( TokenTypes.IDENTIFIER,
375 tokenType() );
376
377 assertEquals( "foo",
378 tokenText() );
379
380 nextToken();
381
382 assertEquals( TokenTypes.COLON,
383 tokenType() );
384
385 nextToken();
386
387 assertEquals( TokenTypes.IDENTIFIER,
388 tokenType() );
389
390 assertEquals( "bar",
391 tokenText() );
392 }
393
394 private void nextToken()
395 {
396 setToken( getLexer().nextToken() );
397 }
398
399 private int tokenType()
400 {
401 return getToken().getTokenType();
402 }
403
404 private String tokenText()
405 {
406 return getToken().getTokenText();
407 }
408
409 private Token getToken()
410 {
411 return this.token;
412 }
413
414 private void setToken(Token token)
415 {
416 this.token = token;
417 }
418
419 private void setText(String text)
420 {
421 this.lexer = new XPathLexer( text );
422 }
423
424 private void setLexer(XPathLexer lexer)
425 {
426 this.lexer = lexer;
427 }
428
429 private XPathLexer getLexer()
430 {
431 return this.lexer;
432 }
433 }