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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Map;
22  import java.util.StringTokenizer;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * Tests for the foreach statement
28   * @author Dion Gillard
29   * @since 1.1
30   */
31  public class ForEachTest extends TestCase {
32  
33      /*** create a named test */
34      public ForEachTest(String name) {
35          super(name);
36      }
37  
38      public void testForEachWithEmptyStatement() throws Exception {
39          Expression e = ExpressionFactory.createExpression("foreach (item in list) ;");
40          JexlContext jc = JexlHelper.createContext();
41  
42          Object o = e.evaluate(jc);
43          assertNull("Result is not null", o);
44      }
45  
46      public void testForEachWithEmptyList() throws Exception {
47          Expression e = ExpressionFactory.createExpression("foreach (item in list) 1+1");
48          JexlContext jc = JexlHelper.createContext();
49  
50          Object o = e.evaluate(jc);
51          assertNull("Result is not null", o);
52      }
53  
54      public void testForEachWithArray() throws Exception {
55          Expression e = ExpressionFactory.createExpression("foreach (item in list) item");
56          JexlContext jc = JexlHelper.createContext();
57          jc.getVars().put("list", new Object[] {"Hello", "World"});
58          Object o = e.evaluate(jc);
59          assertEquals("Result is not last evaluated expression", "World", o);
60      }
61  
62      public void testForEachWithCollection() throws Exception {
63          Expression e = ExpressionFactory.createExpression("foreach (item in list) item");
64          JexlContext jc = JexlHelper.createContext();
65          jc.getVars().put("list", Arrays.asList(new Object[] {"Hello", "World"}));
66          Object o = e.evaluate(jc);
67          assertEquals("Result is not last evaluated expression", "World", o);
68      }
69  
70      public void testForEachWithEnumeration() throws Exception {
71          Expression e = ExpressionFactory.createExpression("foreach (item in list) item");
72          JexlContext jc = JexlHelper.createContext();
73          jc.getVars().put("list", new StringTokenizer("Hello,World", ","));
74          Object o = e.evaluate(jc);
75          assertEquals("Result is not last evaluated expression", "World", o);
76      }
77  
78      public void testForEachWithIterator() throws Exception {
79          Expression e = ExpressionFactory.createExpression("foreach (item in list) item");
80          JexlContext jc = JexlHelper.createContext();
81          jc.getVars().put("list", Arrays.asList(new Object[] {"Hello", "World"}).iterator());
82          Object o = e.evaluate(jc);
83          assertEquals("Result is not last evaluated expression", "World", o);
84      }
85  
86      public void testForEachWithMap() throws Exception {
87          Expression e = ExpressionFactory.createExpression("foreach (item in list) item");
88          JexlContext jc = JexlHelper.createContext();
89          Map map = System.getProperties();
90          String lastProperty = (String) new ArrayList(map.values()).get(System.getProperties().size() - 1);
91          jc.getVars().put("list", map);
92          Object o = e.evaluate(jc);
93          assertEquals("Result is not last evaluated expression", lastProperty, o);
94      }
95  
96      public void testForEachWithBlock() throws Exception {
97          Expression e = ExpressionFactory.createExpression("foreach (item in list) { x = x + item; }");
98          JexlContext jc = JexlHelper.createContext();
99          jc.getVars().put("list", new Object[] {"1", "1"});
100         jc.getVars().put("x", new Integer(0));
101         Object o = e.evaluate(jc);
102         assertEquals("Result is wrong", new Long(2), o);
103         assertEquals("x is wrong", new Long(2), jc.getVars().get("x"));
104     }
105 
106     public void testForEachWithListExpression() throws Exception {
107         Expression e = ExpressionFactory.createExpression("foreach (item in list.keySet()) item");
108         JexlContext jc = JexlHelper.createContext();
109         Map map = System.getProperties();
110         String lastKey = (String) new ArrayList(map.keySet()).get(System.getProperties().size() - 1);
111         jc.getVars().put("list", map);
112         Object o = e.evaluate(jc);
113         assertEquals("Result is not last evaluated expression", lastKey, o);
114     }
115     
116     public void testForEachWithProperty() throws Exception
117     {
118         Expression e = ExpressionFactory.createExpression("foreach (item in list.cheeseList) item");
119         JexlContext jc = JexlHelper.createContext();
120         jc.getVars().put("list", new Foo());
121         Object o = e.evaluate(jc);
122         assertEquals("Result is not last evaluated expression", "brie", o);
123     }
124 }