001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl;
018:
019: import junit.framework.TestCase;
020:
021: /**
022: * Tests for the bitwise operators.
023: * @author Dion Gillard
024: * @since 1.1
025: */
026: public class BitwiseOperatorTest extends TestCase {
027:
028: /**
029: * Create the named test.
030: * @param name test name
031: */
032: public BitwiseOperatorTest(String name) {
033: super (name);
034: }
035:
036: public void testAndWithTwoNulls() throws Exception {
037: Expression e = ExpressionFactory
038: .createExpression("null & null");
039: JexlContext jc = JexlHelper.createContext();
040: Object o = e.evaluate(jc);
041: assertEquals("Result is wrong", new Long(0), o);
042: }
043:
044: public void testAndWithLeftNull() throws Exception {
045: Expression e = ExpressionFactory.createExpression("null & 1");
046: JexlContext jc = JexlHelper.createContext();
047: Object o = e.evaluate(jc);
048: assertEquals("Result is wrong", new Long(0), o);
049: }
050:
051: public void testAndWithRightNull() throws Exception {
052: Expression e = ExpressionFactory.createExpression("1 & null");
053: JexlContext jc = JexlHelper.createContext();
054: Object o = e.evaluate(jc);
055: assertEquals("Result is wrong", new Long(0), o);
056: }
057:
058: public void testAndSimple() throws Exception {
059: Expression e = ExpressionFactory.createExpression("15 & 3");
060: JexlContext jc = JexlHelper.createContext();
061: Object o = e.evaluate(jc);
062: assertEquals("Result is wrong", new Long(3), o);
063: }
064:
065: public void testAndVariableNumberCoercion() throws Exception {
066: Expression e = ExpressionFactory.createExpression("x & y");
067: JexlContext jc = JexlHelper.createContext();
068: jc.getVars().put("x", new Integer(15));
069: jc.getVars().put("y", new Short((short) 7));
070: Object o = e.evaluate(jc);
071: assertEquals("Result is wrong", new Long(7), o);
072: }
073:
074: public void testAndVariableStringCoercion() throws Exception {
075: Expression e = ExpressionFactory.createExpression("x & y");
076: JexlContext jc = JexlHelper.createContext();
077: jc.getVars().put("x", new Integer(15));
078: jc.getVars().put("y", "7");
079: Object o = e.evaluate(jc);
080: assertEquals("Result is wrong", new Long(7), o);
081: }
082:
083: public void testComplementWithNull() throws Exception {
084: Expression e = ExpressionFactory.createExpression("~null");
085: JexlContext jc = JexlHelper.createContext();
086: Object o = e.evaluate(jc);
087: assertEquals("Result is wrong", new Long(-1), o);
088: }
089:
090: public void testComplementSimple() throws Exception {
091: Expression e = ExpressionFactory.createExpression("~128");
092: JexlContext jc = JexlHelper.createContext();
093: Object o = e.evaluate(jc);
094: assertEquals("Result is wrong", new Long(-129), o);
095: }
096:
097: public void testComplementVariableNumberCoercion() throws Exception {
098: Expression e = ExpressionFactory.createExpression("~x");
099: JexlContext jc = JexlHelper.createContext();
100: jc.getVars().put("x", new Integer(15));
101: Object o = e.evaluate(jc);
102: assertEquals("Result is wrong", new Long(-16), o);
103: }
104:
105: public void testComplementVariableStringCoercion() throws Exception {
106: Expression e = ExpressionFactory.createExpression("~x");
107: JexlContext jc = JexlHelper.createContext();
108: jc.getVars().put("x", "15");
109: Object o = e.evaluate(jc);
110: assertEquals("Result is wrong", new Long(-16), o);
111: }
112:
113: public void testOrWithTwoNulls() throws Exception {
114: Expression e = ExpressionFactory
115: .createExpression("null | null");
116: JexlContext jc = JexlHelper.createContext();
117: Object o = e.evaluate(jc);
118: assertEquals("Result is wrong", new Long(0), o);
119: }
120:
121: public void testOrWithLeftNull() throws Exception {
122: Expression e = ExpressionFactory.createExpression("null | 1");
123: JexlContext jc = JexlHelper.createContext();
124: Object o = e.evaluate(jc);
125: assertEquals("Result is wrong", new Long(1), o);
126: }
127:
128: public void testOrWithRightNull() throws Exception {
129: Expression e = ExpressionFactory.createExpression("1 | null");
130: JexlContext jc = JexlHelper.createContext();
131: Object o = e.evaluate(jc);
132: assertEquals("Result is wrong", new Long(1), o);
133: }
134:
135: public void testOrSimple() throws Exception {
136: Expression e = ExpressionFactory.createExpression("12 | 3");
137: JexlContext jc = JexlHelper.createContext();
138: Object o = e.evaluate(jc);
139: assertEquals("Result is wrong", new Long(15), o);
140: }
141:
142: public void testOrVariableNumberCoercion() throws Exception {
143: Expression e = ExpressionFactory.createExpression("x | y");
144: JexlContext jc = JexlHelper.createContext();
145: jc.getVars().put("x", new Integer(12));
146: jc.getVars().put("y", new Short((short) 3));
147: Object o = e.evaluate(jc);
148: assertEquals("Result is wrong", new Long(15), o);
149: }
150:
151: public void testOrVariableStringCoercion() throws Exception {
152: Expression e = ExpressionFactory.createExpression("x | y");
153: JexlContext jc = JexlHelper.createContext();
154: jc.getVars().put("x", new Integer(12));
155: jc.getVars().put("y", "3");
156: Object o = e.evaluate(jc);
157: assertEquals("Result is wrong", new Long(15), o);
158: }
159:
160: public void testXorWithTwoNulls() throws Exception {
161: Expression e = ExpressionFactory
162: .createExpression("null ^ null");
163: JexlContext jc = JexlHelper.createContext();
164: Object o = e.evaluate(jc);
165: assertEquals("Result is wrong", new Long(0), o);
166: }
167:
168: public void testXorWithLeftNull() throws Exception {
169: Expression e = ExpressionFactory.createExpression("null ^ 1");
170: JexlContext jc = JexlHelper.createContext();
171: Object o = e.evaluate(jc);
172: assertEquals("Result is wrong", new Long(1), o);
173: }
174:
175: public void testXorWithRightNull() throws Exception {
176: Expression e = ExpressionFactory.createExpression("1 ^ null");
177: JexlContext jc = JexlHelper.createContext();
178: Object o = e.evaluate(jc);
179: assertEquals("Result is wrong", new Long(1), o);
180: }
181:
182: public void testXorSimple() throws Exception {
183: Expression e = ExpressionFactory.createExpression("1 ^ 3");
184: JexlContext jc = JexlHelper.createContext();
185: Object o = e.evaluate(jc);
186: assertEquals("Result is wrong", new Long(2), o);
187: }
188:
189: public void testXorVariableNumberCoercion() throws Exception {
190: Expression e = ExpressionFactory.createExpression("x ^ y");
191: JexlContext jc = JexlHelper.createContext();
192: jc.getVars().put("x", new Integer(1));
193: jc.getVars().put("y", new Short((short) 3));
194: Object o = e.evaluate(jc);
195: assertEquals("Result is wrong", new Long(2), o);
196: }
197:
198: public void testXorVariableStringCoercion() throws Exception {
199: Expression e = ExpressionFactory.createExpression("x ^ y");
200: JexlContext jc = JexlHelper.createContext();
201: jc.getVars().put("x", new Integer(1));
202: jc.getVars().put("y", "3");
203: Object o = e.evaluate(jc);
204: assertEquals("Result is wrong", new Long(2), o);
205: }
206: }
|