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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.StringTokenizer;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   * Tests for the foreach statement
31   * @since 1.1
32   */
33  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
34  public class ForEachTest extends JexlTestCase {
35  
36      /** create a named test */
37      public ForEachTest() {
38          super("ForEachTest");
39      }
40  
41      @Test public void testForLoop0b0() {
42          String src = "(l)->{ for(let x = 0, y = 0; x < 4; ++x) l.add(x) }";
43          JexlEngine jexl = new JexlBuilder().safe(false).create();
44          JexlScript script = jexl.createScript(src);
45          List<Integer> l = new ArrayList<>();
46          Object result = script.execute(null, l);
47          Assert.assertNotNull(result);
48          Assert.assertEquals(Arrays.asList(0, 1, 2, 3), l);
49          String resrc = toString(script);
50          Assert.assertEquals(src, resrc);
51      }
52  
53      @Test public void testForLoop0a() {
54          String src = "(l)->{ for(let x = 0; x < 4; ++x) { l.add(x); } }";
55          JexlEngine jexl = new JexlBuilder().safe(false).create();
56          JexlScript script = jexl.createScript(src);
57          List<Integer> l = new ArrayList<>();
58          Object result = script.execute(null, l);
59          Assert.assertNotNull(result);
60          Assert.assertEquals(Arrays.asList(0, 1, 2, 3), l);
61          String resrc = toString(script);
62          Assert.assertEquals(src, resrc);
63      }
64  
65      @Test
66      public void testForEachWithEmptyStatement() throws Exception {
67          final JexlScript e = JEXL.createScript("for(item : list) ;");
68          final JexlContext jc = new MapContext();
69          jc.set("list", Collections.emptyList());
70  
71          final Object o = e.execute(jc);
72          Assert.assertNull("Result is not null", o);
73      }
74  
75      @Test
76      public void testForEachWithEmptyList() throws Exception {
77          final JexlScript e = JEXL.createScript("for(item : list) 1+1");
78          final JexlContext jc = new MapContext();
79          jc.set("list", Collections.emptyList());
80  
81          final Object o = e.execute(jc);
82          Assert.assertNull("Result is not null", o);
83      }
84  
85      @Test
86      public void testForEachWithArray() throws Exception {
87          final JexlScript e = JEXL.createScript("for(item : list) item");
88          final JexlContext jc = new MapContext();
89          jc.set("list", new Object[]{"Hello", "World"});
90          final Object o = e.execute(jc);
91          Assert.assertEquals("Result is not last evaluated expression", "World", o);
92      }
93  
94      @Test
95      public void testForEachWithCollection() throws Exception {
96          final JexlScript e = JEXL.createScript("for(var item : list) item");
97          final JexlContext jc = new MapContext();
98          jc.set("list", Arrays.asList("Hello", "World"));
99          final Object o = e.execute(jc);
100         Assert.assertEquals("Result is not last evaluated expression", "World", o);
101     }
102 
103     @Test
104     public void testForEachWithEnumeration() throws Exception {
105         final JexlScript e = JEXL.createScript("for(var item : list) item");
106         final JexlContext jc = new MapContext();
107         jc.set("list", new StringTokenizer("Hello,World", ","));
108         final Object o = e.execute(jc);
109         Assert.assertEquals("Result is not last evaluated expression", "World", o);
110     }
111 
112     @Test
113     public void testForEachWithIterator() throws Exception {
114         final JexlScript e = JEXL.createScript("for(var item : list) item");
115         final JexlContext jc = new MapContext();
116         jc.set("list", Arrays.asList("Hello", "World").iterator());
117         final Object o = e.execute(jc);
118         Assert.assertEquals("Result is not last evaluated expression", "World", o);
119     }
120 
121     @Test
122     public void testForEachWithMap() throws Exception {
123         final JexlScript e = JEXL.createScript("for(item : list) item");
124         final JexlContext jc = new MapContext();
125         final Map<?, ?> map = System.getProperties();
126         final String lastProperty = (String) new ArrayList<Object>(map.values()).get(System.getProperties().size() - 1);
127         jc.set("list", map);
128         final Object o = e.execute(jc);
129         Assert.assertEquals("Result is not last evaluated expression", lastProperty, o);
130     }
131 
132     @Test
133     public void testForEachWithBlock() throws Exception {
134         final JexlScript exs0 = JEXL.createScript("for(var in : list) { x = x + in; }");
135         final JexlContext jc = new MapContext();
136         jc.set("list", new Object[]{2, 3});
137             jc.set("x", new Integer(1));
138         final Object o = exs0.execute(jc);
139             Assert.assertEquals("Result is wrong", new Integer(6), o);
140             Assert.assertEquals("x is wrong", new Integer(6), jc.get("x"));
141         }
142 
143     @Test
144     public void testForEachWithListExpression() throws Exception {
145         final JexlScript e = JEXL.createScript("for(var item : list.keySet()) item");
146         final JexlContext jc = new MapContext();
147         final Map<?, ?> map = System.getProperties();
148         final String lastKey = (String) new ArrayList<Object>(map.keySet()).get(System.getProperties().size() - 1);
149         jc.set("list", map);
150         final Object o = e.execute(jc);
151         Assert.assertEquals("Result is not last evaluated expression", lastKey, o);
152     }
153 
154     @Test
155     public void testForEachWithProperty() throws Exception {
156         final JexlScript e = JEXL.createScript("for(var item : list.cheeseList) item");
157         final JexlContext jc = new MapContext();
158         jc.set("list", new Foo());
159         final Object o = e.execute(jc);
160         Assert.assertEquals("Result is not last evaluated expression", "brie", o);
161     }
162 
163     @Test
164     public void testForEachWithIteratorMethod() throws Exception {
165         final JexlScript e = JEXL.createScript("for(var item : list.cheezy) item");
166         final JexlContext jc = new MapContext();
167         jc.set("list", new Foo());
168         final Object o = e.execute(jc);
169         Assert.assertEquals("Result is not last evaluated expression", "brie", o);
170     }
171 
172     @Test
173     public void testForEachBreakMethod() throws Exception {
174         final JexlScript e = JEXL.createScript(
175                 "var rr = -1; for(var item : [1, 2, 3 ,4 ,5, 6]) { if (item == 3) { rr = item; break; }} rr"
176         );
177         final JexlContext jc = new MapContext();
178         jc.set("list", new Foo());
179         final Object o = e.execute(jc);
180         Assert.assertEquals("Result is not last evaluated expression", 3, o);
181     }
182 
183     @Test
184     public void testForEachContinueMethod() throws Exception {
185         final JexlScript e = JEXL.createScript(
186                 "var rr = 0; for(var item : [1, 2, 3 ,4 ,5, 6]) { if (item <= 3) continue; rr = rr + item;}"
187         );
188         final JexlContext jc = new MapContext();
189         jc.set("list", new Foo());
190         final Object o = e.execute(jc);
191         Assert.assertEquals("Result is not last evaluated expression", 15, o);
192     }
193 
194     @Test
195     public void testForEachContinueBroken() throws Exception {
196         try {
197             final JexlScript e = JEXL.createScript("var rr = 0; continue;");
198             Assert.fail("continue is out of loop!");
199         } catch (final JexlException.Parsing xparse) {
200             final String str = xparse.detailedMessage();
201             Assert.assertTrue(str.contains("continue"));
202         }
203     }
204 
205     @Test
206     public void testForEachBreakBroken() throws Exception {
207         try {
208             final JexlScript e = JEXL.createScript("if (true) { break; }");
209             Assert.fail("break is out of loop!");
210         } catch (final JexlException.Parsing xparse) {
211             final String str = xparse.detailedMessage();
212             Assert.assertTrue(str.contains("break"));
213         }
214     }
215 }