001: //--------------------------------------------------------------------------
002: // Copyright (c) 2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package org.ognl.test;
032:
033: import java.math.BigDecimal;
034: import java.math.BigInteger;
035: import junit.framework.TestSuite;
036:
037: public class ArithmeticAndLogicalOperatorsTest extends OgnlTestCase {
038: private static Object[][] TESTS = {
039: // Double-valued arithmetic expressions
040: { "-1d", new Double(-1) },
041: { "+1d", new Double(1) },
042: { "--1f", new Float(1) },
043: { "2*2.0", new Double(4) },
044: { "5/2.", new Double(2.5) },
045: { "5+2D", new Double(7) },
046: { "5f-2F", new Float(3) },
047: { "5.+2*3", new Double(11) },
048: { "(5.+2)*3", new Double(21) },
049:
050: // BigDecimal-valued arithmetic expressions
051: { "-1b", new BigDecimal(-1) },
052: { "+1b", new BigDecimal(1) },
053: { "--1b", new BigDecimal(1) },
054: { "2*2.0b", new BigDecimal("4.0") },
055: { "5/2.B", new BigDecimal(2) },
056: { "5.0B/2", new BigDecimal(2.5) },
057: { "5+2b", new BigDecimal(7) },
058: { "5-2B", new BigDecimal(3) },
059: { "5.+2b*3", new BigDecimal(11) },
060: { "(5.+2b)*3", new BigDecimal(21) },
061:
062: // Integer-valued arithmetic expressions
063: { "-1", new Integer(-1) },
064: { "+1", new Integer(1) },
065: { "--1", new Integer(1) },
066: { "2*2", new Integer(4) },
067: { "5/2", new Integer(2) },
068: { "5+2", new Integer(7) },
069: { "5-2", new Integer(3) },
070: { "5+2*3", new Integer(11) },
071: { "(5+2)*3", new Integer(21) },
072: { "~1", new Integer(~1) },
073: { "5%2", new Integer(1) },
074: { "5<<2", new Integer(20) },
075: { "5>>2", new Integer(1) },
076: { "5>>1+1", new Integer(1) },
077: { "-5>>>2", new Integer(-5 >>> 2) },
078: { "-5L>>>2", new Long(-5L >>> 2) },
079: { "5. & 3", new Double(1) },
080: { "5 ^3", new Integer(6) },
081: { "5l&3|5^3", new Long(7) },
082: { "5&(3|5^3)", new Integer(5) },
083:
084: // BigInteger-valued arithmetic expressions
085: { "-1h", BigInteger.valueOf(-1) },
086: { "+1H", BigInteger.valueOf(1) },
087: { "--1h", BigInteger.valueOf(1) },
088: { "2h*2", BigInteger.valueOf(4) },
089: { "5/2h", BigInteger.valueOf(2) },
090: { "5h+2", BigInteger.valueOf(7) },
091: { "5-2h", BigInteger.valueOf(3) },
092: { "5+2H*3", BigInteger.valueOf(11) },
093: { "(5+2H)*3", BigInteger.valueOf(21) },
094: { "~1h", BigInteger.valueOf(~1) },
095: { "5h%2", BigInteger.valueOf(1) },
096: { "5h<<2", BigInteger.valueOf(20) },
097: { "5h>>2", BigInteger.valueOf(1) },
098: { "5h>>1+1", BigInteger.valueOf(1) },
099: { "-5h>>>2", BigInteger.valueOf(-2) },
100: { "5.b & 3", BigInteger.valueOf(1) },
101: { "5h ^3", BigInteger.valueOf(6) },
102: { "5h&3|5^3", BigInteger.valueOf(7) },
103: { "5H&(3|5^3)", BigInteger.valueOf(5) },
104:
105: // Logical expressions
106: { "!1", Boolean.FALSE },
107: { "!null", Boolean.TRUE },
108: { "5<2", Boolean.FALSE },
109: { "5>2", Boolean.TRUE },
110: { "5<=5", Boolean.TRUE },
111: { "5>=3", Boolean.TRUE },
112: { "5<-5>>>2", Boolean.TRUE },
113: { "5==5.0", Boolean.TRUE },
114: { "5!=5.0", Boolean.FALSE },
115: { "null in {true,false,null}", Boolean.TRUE },
116: { "null not in {true,false,null}", Boolean.FALSE },
117: { "null in {true,false,null}.toArray()", Boolean.TRUE },
118: { "5 in {true,false,null}", Boolean.FALSE },
119: { "5 not in {true,false,null}", Boolean.TRUE },
120: { "5 instanceof java.lang.Integer", Boolean.TRUE },
121: { "5. instanceof java.lang.Integer", Boolean.FALSE },
122:
123: // Logical expressions (string versions)
124: { "2 or 0", new Integer(2) },
125: { "1 and 0", new Integer(0) },
126: { "1 bor 0", new Integer(1) },
127: { "1 xor 0", new Integer(1) },
128: { "1 band 0", new Integer(0) }, { "1 eq 1", Boolean.TRUE },
129: { "1 neq 1", Boolean.FALSE }, { "1 lt 5", Boolean.TRUE },
130: { "1 lte 5", Boolean.TRUE }, { "1 gt 5", Boolean.FALSE },
131: { "1 gte 5", Boolean.FALSE }, { "1 lt 5", Boolean.TRUE },
132: { "1 shl 2", new Integer(4) },
133: { "4 shr 2", new Integer(1) },
134: { "4 ushr 2", new Integer(1) },
135: { "not null", Boolean.TRUE }, { "not 1", Boolean.FALSE },
136:
137: { "#x > 0", Boolean.TRUE }, { "#x < 0", Boolean.FALSE },
138: { "#x == 0", Boolean.FALSE }, { "#x == 1", Boolean.TRUE },
139: { "0 > #x", Boolean.FALSE }, { "0 < #x", Boolean.TRUE },
140: { "0 == #x", Boolean.FALSE }, { "1 == #x", Boolean.TRUE },
141: { "\"1\" > 0", Boolean.TRUE },
142: { "\"1\" < 0", Boolean.FALSE },
143: { "\"1\" == 0", Boolean.FALSE },
144: { "\"1\" == 1", Boolean.TRUE },
145: { "0 > \"1\"", Boolean.FALSE },
146: { "0 < \"1\"", Boolean.TRUE },
147: { "0 == \"1\"", Boolean.FALSE },
148: { "1 == \"1\"", Boolean.TRUE }, { "#x + 1", "11" },
149: { "1 + #x", "11" }, { "#y == 1", Boolean.TRUE },
150: { "#y == \"1\"", Boolean.TRUE }, { "#y + \"1\"", "11" },
151: { "\"1\" + #y", "11" } };
152:
153: /*===================================================================
154: Public static methods
155: ===================================================================*/
156: public static TestSuite suite() {
157: TestSuite result = new TestSuite();
158:
159: for (int i = 0; i < TESTS.length; i++) {
160: result.addTest(new ArithmeticAndLogicalOperatorsTest(
161: (String) TESTS[i][0] + " (" + TESTS[i][1] + ")",
162: null, (String) TESTS[i][0], TESTS[i][1]));
163: }
164: return result;
165: }
166:
167: /*===================================================================
168: Constructors
169: ===================================================================*/
170: public ArithmeticAndLogicalOperatorsTest() {
171: super ();
172: }
173:
174: public ArithmeticAndLogicalOperatorsTest(String name) {
175: super (name);
176: }
177:
178: public ArithmeticAndLogicalOperatorsTest(String name, Object root,
179: String expressionString, Object expectedResult,
180: Object setValue, Object expectedAfterSetResult) {
181: super (name, root, expressionString, expectedResult, setValue,
182: expectedAfterSetResult);
183: }
184:
185: public ArithmeticAndLogicalOperatorsTest(String name, Object root,
186: String expressionString, Object expectedResult,
187: Object setValue) {
188: super (name, root, expressionString, expectedResult, setValue);
189: }
190:
191: public ArithmeticAndLogicalOperatorsTest(String name, Object root,
192: String expressionString, Object expectedResult) {
193: super (name, root, expressionString, expectedResult);
194: }
195:
196: /*===================================================================
197: Overridden methods
198: ===================================================================*/
199: protected void setUp() {
200: super .setUp();
201: context.put("x", "1");
202: context.put("y", new BigDecimal(1));
203: }
204: }
|