    1   /*
    2    * Copyright 2002-2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.jexl;
    18
    19  import junit.framework.TestCase;
    20
    21  /***
    22   * Tests for the bitwise operators.
    23   * @author Dion Gillard
    24   * @since 1.1
    25   */
    26  public class BitwiseOperatorTest extends TestCase {
    27
    28      /***
    29       * Create the named test.
    30       * @param name test name
    31       */
    32      public BitwiseOperatorTest(String name) {
    33          super(name);
    34      }
    35
    36      public void testAndWithTwoNulls() throws Exception {
    37          Expression e = ExpressionFactory.createExpression("null & null");
    38          JexlContext jc = JexlHelper.createContext();
    39          Object o = e.evaluate(jc);
    40          assertEquals("Result is wrong", new Long(0), o);
    41      }
    42
    43      public void testAndWithLeftNull() throws Exception {
    44          Expression e = ExpressionFactory.createExpression("null & 1");
    45          JexlContext jc = JexlHelper.createContext();
    46          Object o = e.evaluate(jc);
    47          assertEquals("Result is wrong", new Long(0), o);
    48      }
    49
    50      public void testAndWithRightNull() throws Exception {
    51          Expression e = ExpressionFactory.createExpression("1 & null");
    52          JexlContext jc = JexlHelper.createContext();
    53          Object o = e.evaluate(jc);
    54          assertEquals("Result is wrong", new Long(0), o);
    55      }
    56
    57      public void testAndSimple() throws Exception {
    58          Expression e = ExpressionFactory.createExpression("15 & 3");
    59          JexlContext jc = JexlHelper.createContext();
    60          Object o = e.evaluate(jc);
    61          assertEquals("Result is wrong", new Long(3), o);
    62      }
    63
    64      public void testAndVariableNumberCoercion() throws Exception {
    65          Expression e = ExpressionFactory.createExpression("x & y");
    66          JexlContext jc = JexlHelper.createContext();
    67          jc.getVars().put("x", new Integer(15));
    68          jc.getVars().put("y", new Short((short)7));
    69          Object o = e.evaluate(jc);
    70          assertEquals("Result is wrong", new Long(7), o);
    71      }
    72
    73      public void testAndVariableStringCoercion() throws Exception {
    74          Expression e = ExpressionFactory.createExpression("x & y");
    75          JexlContext jc = JexlHelper.createContext();
    76          jc.getVars().put("x", new Integer(15));
    77          jc.getVars().put("y", "7");
    78          Object o = e.evaluate(jc);
    79          assertEquals("Result is wrong", new Long(7), o);
    80      }
    81
    82      public void testComplementWithNull() throws Exception {
    83          Expression e = ExpressionFactory.createExpression("~null");
    84          JexlContext jc = JexlHelper.createContext();
    85          Object o = e.evaluate(jc);
    86          assertEquals("Result is wrong", new Long(-1), o);
    87      }
    88
    89      public void testComplementSimple() throws Exception {
    90          Expression e = ExpressionFactory.createExpression("~128");
    91          JexlContext jc = JexlHelper.createContext();
    92          Object o = e.evaluate(jc);
    93          assertEquals("Result is wrong", new Long(-129), o);
    94      }
    95
    96      public void testComplementVariableNumberCoercion() throws Exception {
    97          Expression e = ExpressionFactory.createExpression("~x");
    98          JexlContext jc = JexlHelper.createContext();
    99          jc.getVars().put("x", new Integer(15));
    100         Object o = e.evaluate(jc);
    101         assertEquals("Result is wrong", new Long(-16), o);
    102     }
    103
    104     public void testComplementVariableStringCoercion() throws Exception {
    105         Expression e = ExpressionFactory.createExpression("~x");
    106         JexlContext jc = JexlHelper.createContext();
    107         jc.getVars().put("x", "15");
    108         Object o = e.evaluate(jc);
    109         assertEquals("Result is wrong", new Long(-16), o);
    110     }
    111
    112     public void testOrWithTwoNulls() throws Exception {
    113         Expression e = ExpressionFactory.createExpression("null | null");
    114         JexlContext jc = JexlHelper.createContext();
    115         Object o = e.evaluate(jc);
    116         assertEquals("Result is wrong", new Long(0), o);
    117     }
    118
    119     public void testOrWithLeftNull() throws Exception {
    120         Expression e = ExpressionFactory.createExpression("null | 1");
    121         JexlContext jc = JexlHelper.createContext();
    122         Object o = e.evaluate(jc);
    123         assertEquals("Result is wrong", new Long(1), o);
    124     }
    125
    126     public void testOrWithRightNull() throws Exception {
    127         Expression e = ExpressionFactory.createExpression("1 | null");
    128         JexlContext jc = JexlHelper.createContext();
    129         Object o = e.evaluate(jc);
    130         assertEquals("Result is wrong", new Long(1), o);
    131     }
    132
    133     public void testOrSimple() throws Exception {
    134         Expression e = ExpressionFactory.createExpression("12 | 3");
    135         JexlContext jc = JexlHelper.createContext();
    136         Object o = e.evaluate(jc);
    137         assertEquals("Result is wrong", new Long(15), o);
    138     }
    139
    140     public void testOrVariableNumberCoercion() throws Exception {
    141         Expression e = ExpressionFactory.createExpression("x | y");
    142         JexlContext jc = JexlHelper.createContext();
    143         jc.getVars().put("x", new Integer(12));
    144         jc.getVars().put("y", new Short((short) 3));
    145         Object o = e.evaluate(jc);
    146         assertEquals("Result is wrong", new Long(15), o);
    147     }
    148
    149     public void testOrVariableStringCoercion() throws Exception {
    150         Expression e = ExpressionFactory.createExpression("x | y");
    151         JexlContext jc = JexlHelper.createContext();
    152         jc.getVars().put("x", new Integer(12));
    153         jc.getVars().put("y", "3");
    154         Object o = e.evaluate(jc);
    155         assertEquals("Result is wrong", new Long(15), o);
    156     }
    157
    158     public void testXorWithTwoNulls() throws Exception {
    159         Expression e = ExpressionFactory.createExpression("null ^ null");
    160         JexlContext jc = JexlHelper.createContext();
    161         Object o = e.evaluate(jc);
    162         assertEquals("Result is wrong", new Long(0), o);
    163     }
    164
    165     public void testXorWithLeftNull() throws Exception {
    166         Expression e = ExpressionFactory.createExpression("null ^ 1");
    167         JexlContext jc = JexlHelper.createContext();
    168         Object o = e.evaluate(jc);
    169         assertEquals("Result is wrong", new Long(1), o);
    170     }
    171
    172     public void testXorWithRightNull() throws Exception {
    173         Expression e = ExpressionFactory.createExpression("1 ^ null");
    174         JexlContext jc = JexlHelper.createContext();
    175         Object o = e.evaluate(jc);
    176         assertEquals("Result is wrong", new Long(1), o);
    177     }
    178
    179     public void testXorSimple() throws Exception {
    180         Expression e = ExpressionFactory.createExpression("1 ^ 3");
    181         JexlContext jc = JexlHelper.createContext();
    182         Object o = e.evaluate(jc);
    183         assertEquals("Result is wrong", new Long(2), o);
    184     }
    185
    186     public void testXorVariableNumberCoercion() throws Exception {
    187         Expression e = ExpressionFactory.createExpression("x ^ y");
    188         JexlContext jc = JexlHelper.createContext();
    189         jc.getVars().put("x", new Integer(1));
    190         jc.getVars().put("y", new Short((short) 3));
    191         Object o = e.evaluate(jc);
    192         assertEquals("Result is wrong", new Long(2), o);
    193     }
    194
    195     public void testXorVariableStringCoercion() throws Exception {
    196         Expression e = ExpressionFactory.createExpression("x ^ y");
    197         JexlContext jc = JexlHelper.createContext();
    198         jc.getVars().put("x", new Integer(1));
    199         jc.getVars().put("y", "3");
    200         Object o = e.evaluate(jc);
    201         assertEquals("Result is wrong", new Long(2), o);
    202     }
    203 }