    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  package org.apache.commons.jexl;
    17
    18  import junit.framework.TestCase;
    19  /***
    20   * Tests for blocks
    21   * @since 1.1
    22   */
    23  public class BlockTest extends TestCase {
    24
    25      /***
    26       * Create the test
    27       *
    28       * @param testName name of the test
    29       */
    30      public BlockTest(String testName) {
    31          super(testName);
    32      }
    33
    34      public void testBlockSimple() throws Exception {
    35          Expression e = ExpressionFactory
    36                  .createExpression("if (true) { 'hello'; }");
    37          JexlContext jc = JexlHelper.createContext();
    38          Object o = e.evaluate(jc);
    39          assertEquals("Result is wrong", "hello", o);
    40      }
    41
    42      public void testBlockExecutesAll() throws Exception {
    43          Expression e = ExpressionFactory
    44                  .createExpression("if (true) { x = 'Hello'; y = 'World';}");
    45          JexlContext jc = JexlHelper.createContext();
    46          Object o = e.evaluate(jc);
    47          assertEquals("First result is wrong", "Hello", jc.getVars().get("x"));
    48          assertEquals("Second result is wrong", "World", jc.getVars().get("y"));
    49          assertEquals("Block result is wrong", "World", o);
    50      }
    51
    52      public void testEmptyBlock() throws Exception {
    53          Expression e = ExpressionFactory
    54                  .createExpression("if (true) { }");
    55          JexlContext jc = JexlHelper.createContext();
    56          Object o = e.evaluate(jc);
    57          assertNull("Result is wrong", o);
    58      }
    59
    60      public void testBlockLastExecuted01() throws Exception {
    61          Expression e = ExpressionFactory
    62                  .createExpression("if (true) { x = 1; } else { x = 2; }");
    63          JexlContext jc = JexlHelper.createContext();
    64          Object o = e.evaluate(jc);
    65          assertEquals("Block result is wrong", new Integer(1), o);
    66      }
    67
    68      public void testBlockLastExecuted02() throws Exception {
    69          Expression e = ExpressionFactory
    70                  .createExpression("if (false) { x = 1; } else { x = 2; }");
    71          JexlContext jc = JexlHelper.createContext();
    72          Object o = e.evaluate(jc);
    73          assertEquals("Block result is wrong", new Integer(2), o);
    74      }
    75
    76      public void testNestedBlock() throws Exception {
    77          Expression e = ExpressionFactory
    78                  .createExpression("if (true) { x = 'hello'; y = 'world';"
    79                      + " if (true) { x; } y; }");
    80          JexlContext jc = JexlHelper.createContext();
    81          Object o = e.evaluate(jc);
    82          assertEquals("Block result is wrong", "world", o);
    83      }
    84
    85  }