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: * Test cases for the if statement.
023: *
024: * @author Dion Gillard
025: * @since 1.1
026: */
027: public class IfTest extends TestCase {
028:
029: public IfTest(String testName) {
030: super (testName);
031: }
032:
033: /**
034: * Make sure if true executes the true statement
035: *
036: * @throws Exception on any error
037: */
038: public void testSimpleIfTrue() throws Exception {
039: Expression e = ExpressionFactory
040: .createExpression("if (true) 1");
041: JexlContext jc = JexlHelper.createContext();
042:
043: Object o = e.evaluate(jc);
044: assertEquals("Result is not 1", new Integer(1), o);
045: }
046:
047: /**
048: * Make sure if false doesn't execute the true statement
049: *
050: * @throws Exception on any error
051: */
052: public void testSimpleIfFalse() throws Exception {
053: Expression e = ExpressionFactory
054: .createExpression("if (false) 1");
055: JexlContext jc = JexlHelper.createContext();
056:
057: Object o = e.evaluate(jc);
058: assertNull("Return value is not empty", o);
059: }
060:
061: /**
062: * Make sure if false executes the false statement
063: *
064: * @throws Exception on any error
065: */
066: public void testSimpleElse() throws Exception {
067: Expression e = ExpressionFactory
068: .createExpression("if (false) 1; else 2;");
069: JexlContext jc = JexlHelper.createContext();
070:
071: Object o = e.evaluate(jc);
072: assertEquals("Result is not 2", new Integer(2), o);
073: }
074:
075: /**
076: * Test the if statement handles blocks correctly
077: *
078: * @throws Exception on any error
079: */
080: public void testBlockIfTrue() throws Exception {
081: Expression e = ExpressionFactory
082: .createExpression("if (true) { 'hello'; }");
083: JexlContext jc = JexlHelper.createContext();
084:
085: Object o = e.evaluate(jc);
086: assertEquals("Result is wrong", "hello", o);
087: }
088:
089: /**
090: * Test the if statement handles blocks in the else statement correctly
091: *
092: * @throws Exception on any error
093: */
094: public void testBlockElse() throws Exception {
095: Expression e = ExpressionFactory
096: .createExpression("if (false) {1;} else {2;}");
097: JexlContext jc = JexlHelper.createContext();
098:
099: Object o = e.evaluate(jc);
100: assertEquals("Result is wrong", new Integer(2), o);
101: }
102:
103: /**
104: * Test the if statement evaluates expressions correctly
105: *
106: * @throws Exception on any error
107: */
108: public void testIfWithSimpleExpression() throws Exception {
109: Expression e = ExpressionFactory
110: .createExpression("if (x == 1) true;");
111: JexlContext jc = JexlHelper.createContext();
112: jc.getVars().put("x", new Integer(1));
113:
114: Object o = e.evaluate(jc);
115: assertEquals("Result is not true", Boolean.TRUE, o);
116: }
117:
118: /**
119: * Test the if statement evaluates arithmetic expressions correctly
120: *
121: * @throws Exception on any error
122: */
123: public void testIfWithArithmeticExpression() throws Exception {
124: Expression e = ExpressionFactory
125: .createExpression("if ((x * 2) + 1 == 5) true;");
126: JexlContext jc = JexlHelper.createContext();
127: jc.getVars().put("x", new Integer(2));
128:
129: Object o = e.evaluate(jc);
130: assertEquals("Result is not true", Boolean.TRUE, o);
131: }
132:
133: /**
134: * Test the if statement evaluates decimal arithmetic expressions correctly
135: *
136: * @throws Exception on any error
137: */
138: public void testIfWithDecimalArithmeticExpression()
139: throws Exception {
140: Expression e = ExpressionFactory
141: .createExpression("if ((x * 2) == 5) true;");
142: JexlContext jc = JexlHelper.createContext();
143: jc.getVars().put("x", new Float(2.5f));
144:
145: Object o = e.evaluate(jc);
146: assertEquals("Result is not true", Boolean.TRUE, o);
147: }
148:
149: /**
150: * Test the if statement works with assignment
151: *
152: * @throws Exception on any error
153: */
154: public void testIfWithAssignment() throws Exception {
155: Expression e = ExpressionFactory
156: .createExpression("if ((x * 2) == 5) {y = 1;} else {y = 2;}");
157: JexlContext jc = JexlHelper.createContext();
158: jc.getVars().put("x", new Float(2.5f));
159:
160: e.evaluate(jc);
161: Object result = jc.getVars().get("y");
162: assertEquals("y has the wrong value", new Integer(1), result);
163: }
164: }
|