1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl;
18
19 import junit.framework.TestCase;
20
21 /***
22 * Test cases for the if statement.
23 *
24 * @author Dion Gillard
25 * @since 1.1
26 */
27 public class IfTest extends TestCase {
28
29 public IfTest(String testName) {
30 super(testName);
31 }
32
33 /***
34 * Make sure if true executes the true statement
35 *
36 * @throws Exception on any error
37 */
38 public void testSimpleIfTrue() throws Exception {
39 Expression e = ExpressionFactory.createExpression("if (true) 1");
40 JexlContext jc = JexlHelper.createContext();
41
42 Object o = e.evaluate(jc);
43 assertEquals("Result is not 1", new Integer(1), o);
44 }
45
46 /***
47 * Make sure if false doesn't execute the true statement
48 *
49 * @throws Exception on any error
50 */
51 public void testSimpleIfFalse() throws Exception {
52 Expression e = ExpressionFactory.createExpression("if (false) 1");
53 JexlContext jc = JexlHelper.createContext();
54
55 Object o = e.evaluate(jc);
56 assertNull("Return value is not empty", o);
57 }
58
59 /***
60 * Make sure if false executes the false statement
61 *
62 * @throws Exception on any error
63 */
64 public void testSimpleElse() throws Exception {
65 Expression e = ExpressionFactory
66 .createExpression("if (false) 1; else 2;");
67 JexlContext jc = JexlHelper.createContext();
68
69 Object o = e.evaluate(jc);
70 assertEquals("Result is not 2", new Integer(2), o);
71 }
72
73 /***
74 * Test the if statement handles blocks correctly
75 *
76 * @throws Exception on any error
77 */
78 public void testBlockIfTrue() throws Exception {
79 Expression e = ExpressionFactory
80 .createExpression("if (true) { 'hello'; }");
81 JexlContext jc = JexlHelper.createContext();
82
83 Object o = e.evaluate(jc);
84 assertEquals("Result is wrong", "hello", o);
85 }
86
87 /***
88 * Test the if statement handles blocks in the else statement correctly
89 *
90 * @throws Exception on any error
91 */
92 public void testBlockElse() throws Exception {
93 Expression e = ExpressionFactory
94 .createExpression("if (false) {1;} else {2;}");
95 JexlContext jc = JexlHelper.createContext();
96
97 Object o = e.evaluate(jc);
98 assertEquals("Result is wrong", new Integer(2), o);
99 }
100
101 /***
102 * Test the if statement evaluates expressions correctly
103 *
104 * @throws Exception on any error
105 */
106 public void testIfWithSimpleExpression() throws Exception {
107 Expression e = ExpressionFactory
108 .createExpression("if (x == 1) true;");
109 JexlContext jc = JexlHelper.createContext();
110 jc.getVars().put("x", new Integer(1));
111
112 Object o = e.evaluate(jc);
113 assertEquals("Result is not true", Boolean.TRUE, o);
114 }
115
116 /***
117 * Test the if statement evaluates arithmetic expressions correctly
118 *
119 * @throws Exception on any error
120 */
121 public void testIfWithArithmeticExpression() throws Exception {
122 Expression e = ExpressionFactory
123 .createExpression("if ((x * 2) + 1 == 5) true;");
124 JexlContext jc = JexlHelper.createContext();
125 jc.getVars().put("x", new Integer(2));
126
127 Object o = e.evaluate(jc);
128 assertEquals("Result is not true", Boolean.TRUE, o);
129 }
130
131 /***
132 * Test the if statement evaluates decimal arithmetic expressions correctly
133 *
134 * @throws Exception on any error
135 */
136 public void testIfWithDecimalArithmeticExpression() throws Exception {
137 Expression e = ExpressionFactory
138 .createExpression("if ((x * 2) == 5) true;");
139 JexlContext jc = JexlHelper.createContext();
140 jc.getVars().put("x", new Float(2.5f));
141
142 Object o = e.evaluate(jc);
143 assertEquals("Result is not true", Boolean.TRUE, o);
144 }
145
146 /***
147 * Test the if statement works with assignment
148 *
149 * @throws Exception on any error
150 */
151 public void testIfWithAssignment() throws Exception {
152 Expression e = ExpressionFactory
153 .createExpression("if ((x * 2) == 5) {y = 1;} else {y = 2;}");
154 JexlContext jc = JexlHelper.createContext();
155 jc.getVars().put("x", new Float(2.5f));
156
157 e.evaluate(jc);
158 Object result = jc.getVars().get("y");
159 assertEquals("y has the wrong value", new Integer(1), result);
160 }
161 }