View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3;
18  
19  import org.apache.commons.jexl3.junit.Asserter;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  /**
24   * Tests for the bitwise operators.
25   * @since 1.1
26   */
27  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
28  public class BitwiseOperatorTest extends JexlTestCase {
29      private Asserter asserter;
30  
31      @Override
32      @Before
33      public void setUp() {
34          asserter = new Asserter(JEXL);
35          asserter.setStrict(false, false);
36      }
37  
38      /**
39       * Create the named test.
40       */
41      public BitwiseOperatorTest() {
42          super("BitwiseOperatorTest");
43      }
44  
45      @Test
46      public void testAndWithTwoNulls() throws Exception {
47          asserter.assertExpression("null & null", new Long(0));
48      }
49  
50      @Test
51      public void testAndWithLeftNull() throws Exception {
52          asserter.assertExpression("null & 1", new Long(0));
53      }
54  
55      @Test
56      public void testAndWithRightNull() throws Exception {
57          asserter.assertExpression("1 & null", new Long(0));
58      }
59  
60      @Test
61      public void testAndSimple() throws Exception {
62          asserter.assertExpression("15 & 3", new Long(15 & 3));
63      }
64  
65      @Test
66      public void testAndVariableNumberCoercion() throws Exception {
67          asserter.setVariable("x", new Integer(15));
68          asserter.setVariable("y", new Short((short) 7));
69          asserter.assertExpression("x & y", new Long(15 & 7));
70      }
71  
72      @Test
73      public void testAndVariableStringCoercion() throws Exception {
74          asserter.setVariable("x", new Integer(15));
75          asserter.setVariable("y", "7");
76          asserter.assertExpression("x & y", new Long(15 & 7));
77      }
78  
79      @Test
80      public void testComplementWithNull() throws Exception {
81          asserter.assertExpression("~null", new Long(-1));
82      }
83  
84      @Test
85      public void testComplementSimple() throws Exception {
86          asserter.assertExpression("~128", new Long(-129));
87      }
88  
89      @Test
90      public void testComplementVariableNumberCoercion() throws Exception {
91          asserter.setVariable("x", new Integer(15));
92          asserter.assertExpression("~x", new Long(~15));
93      }
94  
95      @Test
96      public void testComplementVariableStringCoercion() throws Exception {
97          asserter.setVariable("x", "15");
98          asserter.assertExpression("~x", new Long(~15));
99      }
100 
101     @Test
102     public void testOrWithTwoNulls() throws Exception {
103         asserter.assertExpression("null | null", new Long(0));
104     }
105 
106     @Test
107     public void testOrWithLeftNull() throws Exception {
108         asserter.assertExpression("null | 1", new Long(1));
109     }
110 
111     @Test
112     public void testOrWithRightNull() throws Exception {
113         asserter.assertExpression("1 | null", new Long(1));
114     }
115 
116     @Test
117     public void testOrSimple() throws Exception {
118         asserter.assertExpression("12 | 3", new Long(15));
119     }
120 
121     @Test
122     public void testOrVariableNumberCoercion() throws Exception {
123         asserter.setVariable("x", new Integer(12));
124         asserter.setVariable("y", new Short((short) 3));
125         asserter.assertExpression("x | y", new Long(15));
126     }
127 
128     @Test
129     public void testOrVariableStringCoercion() throws Exception {
130         asserter.setVariable("x", new Integer(12));
131         asserter.setVariable("y", "3");
132         asserter.assertExpression("x | y", new Long(15));
133     }
134 
135     @Test
136     public void testXorWithTwoNulls() throws Exception {
137         asserter.assertExpression("null ^ null", new Long(0));
138     }
139 
140     @Test
141     public void testXorWithLeftNull() throws Exception {
142         asserter.assertExpression("null ^ 1", new Long(1));
143     }
144 
145     @Test
146     public void testXorWithRightNull() throws Exception {
147         asserter.assertExpression("1 ^ null", new Long(1));
148     }
149 
150     @Test
151     public void testXorSimple() throws Exception {
152         asserter.assertExpression("1 ^ 3", new Long(1 ^ 3));
153     }
154 
155     @Test
156     public void testXorVariableNumberCoercion() throws Exception {
157         asserter.setVariable("x", new Integer(1));
158         asserter.setVariable("y", new Short((short) 3));
159         asserter.assertExpression("x ^ y", new Long(1 ^ 3));
160     }
161 
162     @Test
163     public void testXorVariableStringCoercion() throws Exception {
164         asserter.setVariable("x", new Integer(1));
165         asserter.setVariable("y", "3");
166         asserter.assertExpression("x ^ y", new Long(1 ^ 3));
167     }
168 
169     @Test
170     public void testParenthesized() throws Exception {
171         asserter.assertExpression("(2 | 1) & 3", Long.valueOf(3L));
172         asserter.assertExpression("(2 & 1) | 3", Long.valueOf(3L));
173         asserter.assertExpression("~(120 | 42)", new Long(~(120 | 42)));
174     }
175 
176 }