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    *      https://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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Collections;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests for blocks
29   */
30  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
31  class BlockTest extends JexlTestCase {
32  
33      /**
34       * Create the test
35       */
36      public BlockTest() {
37          super("BlockTest");
38      }
39  
40      @Test
41      void testBlockExecutesAll() {
42          final JexlScript e = JEXL.createScript("if (true) { x = 'Hello'; y = 'World';}");
43          final JexlContext jc = new MapContext();
44          final Object o = e.execute(jc);
45          assertEquals("Hello", jc.get("x"), "First result is wrong");
46          assertEquals("World", jc.get("y"), "Second result is wrong");
47          assertEquals("World", o, "Block result is wrong");
48      }
49  
50      @Test
51      void testBlockLastExecuted01() {
52          final JexlScript e = JEXL.createScript("if (true) { x = 1; } else { x = 2; }");
53          final JexlContext jc = new MapContext();
54          final Object o = e.execute(jc);
55          assertEquals(Integer.valueOf(1), o, "Block result is wrong");
56      }
57  
58      @Test
59      void testBlockLastExecuted02() {
60          final JexlScript e = JEXL.createScript("if (false) { x = 1; } else { x = 2; }");
61          final JexlContext jc = new MapContext();
62          final Object o = e.execute(jc);
63          assertEquals(Integer.valueOf(2), o, "Block result is wrong");
64      }
65  
66      @Test
67      void testBlockSimple() {
68          final JexlScript e = JEXL.createScript("if (true) { 'hello'; }");
69          final JexlContext jc = new MapContext();
70          final Object o = e.execute(jc);
71          assertEquals("hello", o, "Result is wrong");
72      }
73  
74      @Test
75      void testEmptyBlock() {
76          final JexlScript e = JEXL.createScript("if (true) { }");
77          final JexlContext jc = new MapContext();
78          final Object o = e.execute(jc);
79          assertNull(o, "Result is wrong");
80      }
81  
82      @Test
83      void testNestedBlock() {
84          final JexlScript e = JEXL.createScript("if (true) { x = 'hello'; y = 'world';" + " if (true) { x; } y; }");
85          final JexlContext jc = new MapContext();
86          final Object o = e.execute(jc);
87          assertEquals("world", o, "Block result is wrong");
88      }
89  
90      @Test
91      void testSetVSBlock() {
92          final AnnotationTest.AnnotationContext jc = new AnnotationTest.AnnotationContext();
93          JexlScript e;
94          Object r;
95          // synchronized block
96          e = JEXL.createScript("let n = 41 @synchronized { n += 1; }");
97          r = e.execute(jc);
98          assertEquals(42, r);
99          assertEquals(1, jc.getCount());
100         assertTrue(jc.getNames().contains("synchronized"));
101         // synchronized set
102         e = JEXL.createScript("let n = 41 @synchronized { n += 1 }");
103         r = e.execute(jc);
104         assertEquals(Collections.singleton(42), r);
105         assertEquals(2, jc.getCount());
106         assertTrue(jc.getNames().contains("synchronized"));
107 
108         e = JEXL.createScript("let n = 41 { n += 1 }");
109         r = e.execute(jc);
110         assertEquals(Collections.singleton(42), r);
111 
112         e = JEXL.createScript("let n = 41 { n += 1; }");
113         r = e.execute(jc);
114         assertEquals(42, r);
115 
116         e = JEXL.createScript("{'A' : 1, 'B' : 42}['B']");
117         r = e.execute(jc);
118         assertEquals(42, r);
119 
120         e = JEXL.createScript("{ n = 42; }");
121         r = e.execute(jc);
122         assertEquals(42, r);
123         e = JEXL.createScript("@synchronized(y) { n = 42; }", "y");
124         r = e.execute(jc);
125         assertEquals(42, r);
126 
127         e = JEXL.createScript("{ n = 42 }");
128         r = e.execute(jc);
129         assertEquals(Collections.singleton(42), r);
130         e = JEXL.createScript("@synchronized(z) { n = 42 }", "z");
131         r = e.execute(jc);
132         assertEquals(Collections.singleton(42), r);
133 
134         e = JEXL.createScript("{ n = 41; m = 42 }");
135         r = e.execute(jc);
136         assertEquals(42, r);
137 
138         e = JEXL.createScript("{ 20 + 22; }");
139         r = e.execute(jc);
140         assertEquals(42, r);
141         e = JEXL.createScript("@synchronized { 20 + 22; }");
142         r = e.execute(jc);
143         assertEquals(42, r);
144 
145         e = JEXL.createScript("{ 6 * 7 }");
146         r = e.execute(jc);
147         assertEquals(Collections.singleton(42), r);
148     }
149 }