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.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for the bitwise operators.
25   */
26  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
27  public class BitwiseOperatorTest extends JexlTestCase {
28      private Asserter asserter;
29  
30      /**
31       * Create the named test.
32       */
33      public BitwiseOperatorTest() {
34          super("BitwiseOperatorTest");
35      }
36  
37      @Override
38      @BeforeEach
39      public void setUp() {
40          asserter = new Asserter(JEXL);
41          asserter.setStrict(false, false);
42      }
43  
44      @Test
45      public void testAndSimple() throws Exception {
46          asserter.assertExpression("15 & 3", Long.valueOf(15 & 3));
47      }
48  
49      @Test
50      public void testAndVariableNumberCoercion() throws Exception {
51          asserter.setVariable("x", Integer.valueOf(15));
52          asserter.setVariable("y", Short.valueOf((short) 7));
53          asserter.assertExpression("x & y", Long.valueOf(15 & 7));
54      }
55  
56      @Test
57      public void testAndVariableStringCoercion() throws Exception {
58          asserter.setVariable("x", Integer.valueOf(15));
59          asserter.setVariable("y", "7");
60          asserter.assertExpression("x & y", Long.valueOf(15 & 7));
61      }
62  
63      @Test
64      public void testAndWithLeftNull() throws Exception {
65          asserter.assertExpression("null & 1", Long.valueOf(0));
66      }
67  
68      @Test
69      public void testAndWithRightNull() throws Exception {
70          asserter.assertExpression("1 & null", Long.valueOf(0));
71      }
72  
73      @Test
74      public void testAndWithTwoNulls() throws Exception {
75          asserter.assertExpression("null & null", Long.valueOf(0));
76      }
77  
78      @Test
79      public void testComplementSimple() throws Exception {
80          asserter.assertExpression("~128", Long.valueOf(-129));
81      }
82  
83      @Test
84      public void testComplementVariableNumberCoercion() throws Exception {
85          asserter.setVariable("x", Integer.valueOf(15));
86          asserter.assertExpression("~x", Long.valueOf(~15));
87      }
88  
89      @Test
90      public void testComplementVariableStringCoercion() throws Exception {
91          asserter.setVariable("x", "15");
92          asserter.assertExpression("~x", Long.valueOf(~15));
93      }
94  
95      @Test
96      public void testComplementWithNull() throws Exception {
97          asserter.assertExpression("~null", Long.valueOf(-1));
98      }
99  
100     @Test
101     public void testOrSimple() throws Exception {
102         asserter.assertExpression("12 | 3", Long.valueOf(15));
103     }
104 
105     @Test
106     public void testOrVariableNumberCoercion() throws Exception {
107         asserter.setVariable("x", Integer.valueOf(12));
108         asserter.setVariable("y", Short.valueOf((short) 3));
109         asserter.assertExpression("x | y", Long.valueOf(15));
110     }
111 
112     @Test
113     public void testOrVariableStringCoercion() throws Exception {
114         asserter.setVariable("x", Integer.valueOf(12));
115         asserter.setVariable("y", "3");
116         asserter.assertExpression("x | y", Long.valueOf(15));
117     }
118 
119     @Test
120     public void testOrWithLeftNull() throws Exception {
121         asserter.assertExpression("null | 1", Long.valueOf(1));
122     }
123 
124     @Test
125     public void testOrWithRightNull() throws Exception {
126         asserter.assertExpression("1 | null", Long.valueOf(1));
127     }
128 
129     @Test
130     public void testOrWithTwoNulls() throws Exception {
131         asserter.assertExpression("null | null", Long.valueOf(0));
132     }
133 
134     @Test
135     public void testParenthesized() throws Exception {
136         asserter.assertExpression("(2 | 1) & 3", Long.valueOf(3L));
137         asserter.assertExpression("(2 & 1) | 3", Long.valueOf(3L));
138         asserter.assertExpression("~(120 | 42)", Long.valueOf(~(120 | 42)));
139     }
140 
141     @Test
142     public void testXorSimple() throws Exception {
143         asserter.assertExpression("1 ^ 3", Long.valueOf(1 ^ 3));
144     }
145 
146     @Test
147     public void testXorVariableNumberCoercion() throws Exception {
148         asserter.setVariable("x", Integer.valueOf(1));
149         asserter.setVariable("y", Short.valueOf((short) 3));
150         asserter.assertExpression("x ^ y", Long.valueOf(1 ^ 3));
151     }
152 
153     @Test
154     public void testXorVariableStringCoercion() throws Exception {
155         asserter.setVariable("x", Integer.valueOf(1));
156         asserter.setVariable("y", "3");
157         asserter.assertExpression("x ^ y", Long.valueOf(1 ^ 3));
158     }
159 
160     @Test
161     public void testXorWithLeftNull() throws Exception {
162         asserter.assertExpression("null ^ 1", Long.valueOf(1));
163     }
164 
165     @Test
166     public void testXorWithRightNull() throws Exception {
167         asserter.assertExpression("1 ^ null", Long.valueOf(1));
168     }
169 
170     @Test
171     public void testXorWithTwoNulls() throws Exception {
172         asserter.assertExpression("null ^ null", Long.valueOf(0));
173     }
174 
175 }