View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  /**
23   * Tests for blocks
24   * @since 1.1
25   */
26  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
27  public class BlockTest extends JexlTestCase {
28  
29      /**
30       * Create the test
31       */
32      public BlockTest() {
33          super("BlockTest");
34      }
35  
36      @Test
37      public void testBlockSimple() throws Exception {
38          final JexlScript e = JEXL.createScript("if (true) { 'hello'; }");
39          final JexlContext jc = new MapContext();
40          final Object o = e.execute(jc);
41          Assert.assertEquals("Result is wrong", "hello", o);
42      }
43  
44      @Test
45      public void testBlockExecutesAll() throws Exception {
46          final JexlScript e = JEXL.createScript("if (true) { x = 'Hello'; y = 'World';}");
47          final JexlContext jc = new MapContext();
48          final Object o = e.execute(jc);
49          Assert.assertEquals("First result is wrong", "Hello", jc.get("x"));
50          Assert.assertEquals("Second result is wrong", "World", jc.get("y"));
51          Assert.assertEquals("Block result is wrong", "World", o);
52      }
53  
54      @Test
55      public void testEmptyBlock() throws Exception {
56          final JexlScript e = JEXL.createScript("if (true) { }");
57          final JexlContext jc = new MapContext();
58          final Object o = e.execute(jc);
59          Assert.assertNull("Result is wrong", o);
60      }
61  
62      @Test
63      public void testBlockLastExecuted01() throws Exception {
64          final JexlScript e = JEXL.createScript("if (true) { x = 1; } else { x = 2; }");
65          final JexlContext jc = new MapContext();
66          final Object o = e.execute(jc);
67          Assert.assertEquals("Block result is wrong", new Integer(1), o);
68      }
69  
70      @Test
71      public void testBlockLastExecuted02() throws Exception {
72          final JexlScript e = JEXL.createScript("if (false) { x = 1; } else { x = 2; }");
73          final JexlContext jc = new MapContext();
74          final Object o = e.execute(jc);
75          Assert.assertEquals("Block result is wrong", new Integer(2), o);
76      }
77  
78      @Test
79      public void testNestedBlock() throws Exception {
80          final JexlScript e = JEXL.createScript("if (true) { x = 'hello'; y = 'world';" + " if (true) { x; } y; }");
81          final JexlContext jc = new MapContext();
82          final Object o = e.execute(jc);
83          Assert.assertEquals("Block result is wrong", "world", o);
84      }
85  }