01: /*
02: * Copyright 2002-2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.commons.jexl;
18:
19: import junit.framework.TestCase;
20:
21: /**
22: * Tests for while statement.
23: * @author Dion Gillard
24: * @since 1.1
25: */
26: public class WhileTest extends TestCase {
27:
28: public WhileTest(String testName) {
29: super (testName);
30: }
31:
32: public void testSimpleWhileFalse() throws Exception {
33: Expression e = ExpressionFactory
34: .createExpression("while (false) ;");
35: JexlContext jc = JexlHelper.createContext();
36:
37: Object o = e.evaluate(jc);
38: assertNull("Result is not null", o);
39: }
40:
41: public void testWhileExecutesExpressionWhenLooping()
42: throws Exception {
43: Expression e = ExpressionFactory
44: .createExpression("while (x < 10) x = x + 1;");
45: JexlContext jc = JexlHelper.createContext();
46: jc.getVars().put("x", new Integer(1));
47:
48: Object o = e.evaluate(jc);
49: assertEquals("Result is wrong", new Long(10), o);
50: }
51:
52: public void testWhileWithBlock() throws Exception {
53: Expression e = ExpressionFactory
54: .createExpression("while (x < 10) { x = x + 1; y = y * 2; }");
55: JexlContext jc = JexlHelper.createContext();
56: jc.getVars().put("x", new Integer(1));
57: jc.getVars().put("y", new Integer(1));
58:
59: Object o = e.evaluate(jc);
60: assertEquals("Result is wrong", new Long(512), o);
61: assertEquals("x is wrong", new Long(10), jc.getVars().get("x"));
62: assertEquals("y is wrong", new Long(512), jc.getVars().get("y"));
63: }
64: }
|