    1   /*
    2    * Copyright 2002-2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    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   * 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  }