1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/test/org/jaxen/saxpath/base/XPathLexerTest.java,v 1.8 2005/06/14 07:40:31 elharo Exp $
3    * $Revision: 1.8 $
4    * $Date: 2005/06/14 07:40:31 $
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.8 2005/06/14 07:40:31 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 tearDown()
80      {
81          this.lexer = null;
82          this.token = null;
83      }
84  
85      public void testNamespace()
86      {
87          setText( "a:b" );
88  
89          nextToken();
90          assertEquals( TokenTypes.IDENTIFIER,
91                        this.token.getTokenType() );
92          assertEquals( "a",
93                        this.token.getTokenText() );
94  
95          nextToken();
96          assertEquals( TokenTypes.COLON,
97                        this.token.getTokenType() );
98          
99          nextToken();
100         assertEquals( TokenTypes.IDENTIFIER,
101                       this.token.getTokenType() );
102         assertEquals( "b",
103                       this.token.getTokenText() );
104     }
105 
106     public void testIdentifier()
107     {
108         setText( "foo" );
109 
110         nextToken();
111         assertEquals( TokenTypes.IDENTIFIER,
112                       this.token.getTokenType() );
113         assertEquals( "foo",
114                       this.token.getTokenText() );
115 
116         setText( "foo.bar" );
117 
118         nextToken();
119         assertEquals( TokenTypes.IDENTIFIER,
120                       this.token.getTokenType() );
121         assertEquals( "foo.bar",
122                       this.token.getTokenText() );
123     }
124 
125     
126     /***
127      * This tests that characters added in XML 1.1 and Unicode 3.0
128      * are not recognized as legal name characters. 
129      */
130     public void testBurmeseIdentifierStart()
131     {
132         setText( "\u1000foo" );
133 
134         nextToken();
135         assertEquals( TokenTypes.ERROR,
136                       this.token.getTokenType() );
137 
138     }
139 
140     public void testBurmeseIdentifierPart()
141     {
142         setText( "foo\u1000foo" );
143 
144         nextToken();
145         assertEquals( "foo",
146                       this.token.getTokenText() );
147         nextToken();
148         assertEquals( TokenTypes.ERROR,
149                       this.token.getTokenType() );
150 
151     }
152 
153     public void testIdentifierAndOperator()
154     {
155         setText( "foo and bar" );
156 
157         nextToken();
158         assertEquals( TokenTypes.IDENTIFIER,
159                       this.token.getTokenType() );
160         assertEquals( "foo",
161                       this.token.getTokenText() );
162 
163         nextToken();
164         assertEquals( TokenTypes.AND,
165                       this.token.getTokenType() );
166         nextToken();
167         assertEquals( TokenTypes.IDENTIFIER,
168                       this.token.getTokenType() );
169         assertEquals( "bar",
170                       this.token.getTokenText() );
171     }
172 
173     public void testTrickyIdentifierAndOperator()
174     {
175         setText( "and and and" );
176 
177         nextToken();
178         assertEquals( TokenTypes.IDENTIFIER,
179                       this.token.getTokenType() );
180         assertEquals( "and",
181                       this.token.getTokenText() );
182 
183         nextToken();
184         assertEquals( TokenTypes.AND,
185                       this.token.getTokenType() );
186         
187         nextToken();
188         assertEquals( TokenTypes.IDENTIFIER,
189                       this.token.getTokenType() );
190         assertEquals( "and",
191                       this.token.getTokenText() );
192     }
193 
194     public void testInteger()
195     {
196         setText( "1234" );
197 
198         nextToken();
199         assertEquals( TokenTypes.INTEGER,
200                       this.token.getTokenType() );
201         assertEquals( "1234",
202                       this.token.getTokenText() );
203     }
204 
205     public void testDouble()
206     {
207         setText( "12.34" );
208 
209         nextToken();
210         assertEquals( TokenTypes.DOUBLE,
211                       this.token.getTokenType() );
212         assertEquals( "12.34",
213                       this.token.getTokenText() );
214     }
215 
216     public void testDoubleOnlyDecimal()
217     {
218         setText( ".34" );
219 
220         nextToken();
221         assertEquals( TokenTypes.DOUBLE,
222                       this.token.getTokenType() );
223         assertEquals( ".34",
224                       this.token.getTokenText() );
225     }
226 
227     public void testNumbersAndMode()
228     {
229         setText( "12.34 mod 3" );
230 
231         nextToken();
232         assertEquals( TokenTypes.DOUBLE,
233                       this.token.getTokenType() );
234         assertEquals( "12.34",
235                       this.token.getTokenText() );
236 
237         nextToken();
238         assertEquals( TokenTypes.MOD,
239                       this.token.getTokenType() );
240 
241         nextToken();
242         assertEquals( TokenTypes.INTEGER,
243                       this.token.getTokenType() );
244   
245     }
246 
247     public void testSlash()
248     {
249         setText( "/" );
250 
251         nextToken();
252         assertEquals( TokenTypes.SLASH,
253                       this.token.getTokenType() );
254     }
255 
256     public void testDoubleSlash()
257     {
258         setText( "//" );
259 
260         nextToken();
261         assertEquals( TokenTypes.DOUBLE_SLASH,
262                       this.token.getTokenType() );
263     }
264 
265     public void testIdentifierWithColon()
266     {
267         setText ( "foo:bar" );
268 
269         nextToken();
270         assertEquals( TokenTypes.IDENTIFIER,
271                       this.token.getTokenType() );
272         assertEquals( "foo",
273                       this.token.getTokenText() );
274 
275         nextToken();
276         assertEquals( TokenTypes.COLON,
277                       this.token.getTokenType() );
278         
279         nextToken();
280         assertEquals( TokenTypes.IDENTIFIER,
281                       this.token.getTokenType() );
282         assertEquals( "bar",
283                       this.token.getTokenText() );
284     }
285 
286     public void testEOF()
287     {
288         setText( "foo" );
289         
290         nextToken();
291         assertEquals( TokenTypes.IDENTIFIER,
292                       this.token.getTokenType() );
293         assertEquals( "foo",
294                       this.token.getTokenText() );
295 
296         nextToken();
297         assertEquals( TokenTypes.EOF,
298                       this.token.getTokenType() );
299     }
300  
301     public void testWhitespace()
302     {
303         setText ( " /   \tfoo:bar" );
304 
305         nextToken();
306 
307         assertEquals( TokenTypes.SLASH,
308                       this.token.getTokenType() );
309         nextToken();
310 
311         assertEquals( TokenTypes.IDENTIFIER,
312                       this.token.getTokenType() );
313 
314         assertEquals( "foo",
315                       this.token.getTokenText() );
316 
317         nextToken();
318 
319         assertEquals( TokenTypes.COLON,
320                       this.token.getTokenType() );
321 
322         nextToken();
323 
324         assertEquals( TokenTypes.IDENTIFIER,
325                       this.token.getTokenType() );
326 
327         assertEquals( "bar",
328                       this.token.getTokenText() );
329     }
330 
331     private void nextToken()
332     {
333         this.token = this.lexer.nextToken();
334     }
335 
336     private void setText(String text)
337     {
338         this.lexer = new XPathLexer( text );
339     }
340 }