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   * Tests for while statement.
22   * @author Dion Gillard
23   * @since 1.1
24   */
25  public class WhileTest extends TestCase {
26  
27      public WhileTest(String testName) {
28          super(testName);
29      }
30  
31      public void testSimpleWhileFalse() throws Exception {
32          Expression e = ExpressionFactory.createExpression("while (false) ;");
33          JexlContext jc = JexlHelper.createContext();
34  
35          Object o = e.evaluate(jc);
36          assertNull("Result is not null", o);
37      }
38      
39      public void testWhileExecutesExpressionWhenLooping() throws Exception {
40          Expression e = ExpressionFactory.createExpression("while (x < 10) x = x + 1;");
41          JexlContext jc = JexlHelper.createContext();
42          jc.getVars().put("x", new Integer(1));
43  
44          Object o = e.evaluate(jc);
45          assertEquals("Result is wrong", new Long(10), o);
46      }
47  
48      public void testWhileWithBlock() throws Exception {
49          Expression e = ExpressionFactory.createExpression("while (x < 10) { x = x + 1; y = y * 2; }");
50          JexlContext jc = JexlHelper.createContext();
51          jc.getVars().put("x", new Integer(1));
52          jc.getVars().put("y", new Integer(1));
53  
54          Object o = e.evaluate(jc);
55          assertEquals("Result is wrong", new Long(512), o);
56          assertEquals("x is wrong", new Long(10), jc.getVars().get("x"));
57          assertEquals("y is wrong", new Long(512), jc.getVars().get("y"));
58      }
59  }