1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/test/org/jaxen/saxpath/base/XPathLexerTokenTest.java,v 1.3 2004/07/05 21:14:34 proyal Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/07/05 21:14:34 $
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: XPathLexerTokenTest.java,v 1.3 2004/07/05 21:14:34 proyal Exp $
60   */
61  
62  
63  
64  package org.jaxen.saxpath.base;
65  
66  import junit.framework.TestCase;
67  
68  public class XPathLexerTokenTest extends TestCase
69  {
70      private XPathLexer lexer;
71  
72      public XPathLexerTokenTest(String name)
73      {
74          super( name );
75      }
76  
77      public void setUp()
78      {
79          this.lexer = new XPathLexer();
80      }
81  
82      public void tearDown()
83      {
84          this.lexer = null;
85      }
86  
87      public void testIdentifier()
88      {
89          runTest( "identifier", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.EOF } );
90      }
91  
92      public void testNumberInteger()
93      {
94          runTest( "42", new int[]{ TokenTypes.INTEGER, TokenTypes.EOF } );
95      }
96  
97      public void testNumberDouble()
98      {
99          runTest( "42.42", new int[]{ TokenTypes.DOUBLE, TokenTypes.EOF } );
100     }
101 
102     public void testComma()
103     {
104         runTest( ",", new int[]{ TokenTypes.COMMA, TokenTypes.EOF } );
105     }
106 
107     public void testEquals()
108     {
109         runTest( "=", new int[]{ TokenTypes.EQUALS, TokenTypes.EOF } );
110     }
111 
112     public void testMinus()
113     {
114         runTest( "-", new int[]{ TokenTypes.MINUS, TokenTypes.EOF } );
115     }
116 
117     public void testPlus()
118     {
119         runTest( "+", new int[]{ TokenTypes.PLUS, TokenTypes.EOF } );
120     }
121 
122     public void testDollar()
123     {
124         runTest( "$", new int[]{ TokenTypes.DOLLAR, TokenTypes.EOF } );
125     }
126 
127     public void testPipe()
128     {
129         runTest( "|", new int[]{ TokenTypes.PIPE, TokenTypes.EOF } );
130     }
131 
132     public void testAt()
133     {
134         runTest( "@", new int[]{ TokenTypes.AT, TokenTypes.EOF } );
135     }
136 
137     public void testColon()
138     {
139         runTest( ":", new int[]{ TokenTypes.COLON, TokenTypes.EOF } );
140     }
141 
142     public void testDoubleColon()
143     {
144         runTest( "::", new int[]{ TokenTypes.DOUBLE_COLON, TokenTypes.EOF } );
145     }
146 
147     public void testNot()
148     {
149         runTest( "!", new int[]{ TokenTypes.NOT, TokenTypes.EOF } );
150     }
151 
152     public void testNotEquals()
153     {
154         runTest( "!=", new int[]{ TokenTypes.NOT_EQUALS, TokenTypes.EOF } );
155     }
156 
157     public void testStar()
158     {
159         runTest( "*", new int[]{ TokenTypes.STAR, TokenTypes.EOF } );
160     }
161 
162     public void testLiteralSingleQuote()
163     {
164         runTest( "'literal'", new int[]{ TokenTypes.LITERAL, TokenTypes.EOF } );
165     }
166 
167     public void testLiteralDoubleQuote()
168     {
169         runTest( "\"literal\"", new int[]{ TokenTypes.LITERAL, TokenTypes.EOF } );
170     }
171 
172     public void testSingleDot()
173     {
174         runTest( ".", new int[]{ TokenTypes.DOT, TokenTypes.EOF } );
175     }
176 
177     public void testDoubleDot()
178     {
179         runTest( "..", new int[]{ TokenTypes.DOT_DOT, TokenTypes.EOF });
180     }
181 
182     public void testLeftBracket()
183     {
184         runTest( "[", new int[]{ TokenTypes.LEFT_BRACKET, TokenTypes.EOF } );
185     }
186 
187     public void testRightBracket()
188     {
189         runTest( "]", new int[]{ TokenTypes.RIGHT_BRACKET, TokenTypes.EOF } );
190     }
191 
192     public void testLeftParen()
193     {
194         runTest( "(", new int[]{ TokenTypes.LEFT_PAREN, TokenTypes.EOF } );
195     }
196 
197     public void testSingleSlash()
198     {
199         runTest( "/", new int[]{ TokenTypes.SLASH, TokenTypes.EOF } );
200     }
201 
202     public void testDoubleSlash()
203     {
204         runTest( "//", new int[]{ TokenTypes.DOUBLE_SLASH, TokenTypes.EOF } );
205     }
206 
207     public void testLessThan()
208     {
209         runTest( "<", new int[]{ TokenTypes.LESS_THAN, TokenTypes.EOF } );
210     }
211 
212     public void testLessThanEquals()
213     {
214         runTest( "<=", new int[]{ TokenTypes.LESS_THAN_EQUALS, TokenTypes.EOF } );
215     }
216 
217     public void testGreaterThan()
218     {
219         runTest( ">", new int[]{ TokenTypes.GREATER_THAN, TokenTypes.EOF } );
220     }
221 
222     public void testGreaterThanEquals()
223     {
224         runTest( ">=", new int[]{ TokenTypes.GREATER_THAN_EQUALS, TokenTypes.EOF } );
225     }
226 
227     public void testOperatorAnd()
228     {
229         runTest( "identifier and", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.AND, TokenTypes.EOF } );
230     }
231 
232     public void testOperatorOr()
233     {
234         runTest( "identifier or", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.OR, TokenTypes.EOF } );
235     }
236 
237     public void testOperatorMod()
238     {
239         runTest( "identifier mod", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.MOD, TokenTypes.EOF } );
240     }
241 
242     public void testOperatorDiv()
243     {
244         runTest( "identifier div", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.DIV } );
245     }
246 
247     public void testWhitespace()
248     {
249         runTest( " \t \t \t", new int[]{ TokenTypes.EOF } );
250     }
251 
252     private void runTest(String text,
253                          int[] expectedTokens)
254     {
255         this.lexer.setXPath( text );
256 
257         int   tokenType = 0;
258         Token token     = null;
259 
260         for ( int i = 0 ; i < expectedTokens.length ; ++i )
261         {
262             tokenType = expectedTokens[i];
263 
264             token = this.lexer.nextToken();
265 
266             assertNotNull( token );
267 
268             assertEquals( tokenType,
269                           token.getTokenType() );
270         }
271     }
272 }