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