1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/test/org/jaxen/saxpath/base/XPathLexerTest.java,v 1.4 2005/02/05 16:50:23 elharo Exp $
3    * $Revision: 1.4 $
4    * $Date: 2005/02/05 16:50:23 $
5    *
6    * ====================================================================
7    *
8    * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions
13   * are met:
14   *
15   * 1. Redistributions of source code must retain the above copyright
16   *    notice, this list of conditions, and the following disclaimer.
17   *
18   * 2. Redistributions in binary form must reproduce the above copyright
19   *    notice, this list of conditions, and the disclaimer that follows
20   *    these conditions in the documentation and/or other materials
21   *    provided with the distribution.
22   *
23   * 3. The name "Jaxen" must not be used to endorse or promote products
24   *    derived from this software without prior written permission.  For
25   *    written permission, please contact license@jaxen.org.
26   *
27   * 4. Products derived from this software may not be called "Jaxen", nor
28   *    may "Jaxen" appear in their name, without prior written permission
29   *    from the Jaxen Project Management (pm@jaxen.org).
30   *
31   * In addition, we request (but do not require) that you include in the
32   * end-user documentation provided with the redistribution and/or in the
33   * software itself an acknowledgement equivalent to the following:
34   *     "This product includes software developed by the
35   *      Jaxen Project (http://www.jaxen.org/)."
36   * Alternatively, the acknowledgment may be graphical using the logos
37   * available at http://www.jaxen.org/
38   *
39   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50   * SUCH DAMAGE.
51   *
52   * ====================================================================
53   * This software consists of voluntary contributions made by many
54   * individuals on behalf of the Jaxen Project and was originally
55   * created by bob mcwhirter <bob@werken.com> and
56   * James Strachan <jstrachan@apache.org>.  For more information on the
57   * Jaxen Project, please see <http://www.jaxen.org/>.
58   *
59   * $Id: XPathLexerTest.java,v 1.4 2005/02/05 16:50:23 elharo Exp $
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     public void testAttributeWithUnderscore()
347     {
348         setText( "@_foo" );
349 
350         nextToken();
351 
352         assertEquals( TokenTypes.IDENTIFIER,
353                       tokenType() );
354 
355         assertEquals( "_foo",
356                       tokenText() );
357 
358         nextToken();
359 
360         assertEquals( TokenTypes.EOF,
361                       tokenType() );
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 }