001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl;
018:
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Map;
022: import java.util.StringTokenizer;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for the foreach statement
028: * @author Dion Gillard
029: * @since 1.1
030: */
031: public class ForEachTest extends TestCase {
032:
033: /** create a named test */
034: public ForEachTest(String name) {
035: super (name);
036: }
037:
038: public void testForEachWithEmptyStatement() throws Exception {
039: Expression e = ExpressionFactory
040: .createExpression("foreach (item in list) ;");
041: JexlContext jc = JexlHelper.createContext();
042:
043: Object o = e.evaluate(jc);
044: assertNull("Result is not null", o);
045: }
046:
047: public void testForEachWithEmptyList() throws Exception {
048: Expression e = ExpressionFactory
049: .createExpression("foreach (item in list) 1+1");
050: JexlContext jc = JexlHelper.createContext();
051:
052: Object o = e.evaluate(jc);
053: assertNull("Result is not null", o);
054: }
055:
056: public void testForEachWithArray() throws Exception {
057: Expression e = ExpressionFactory
058: .createExpression("foreach (item in list) item");
059: JexlContext jc = JexlHelper.createContext();
060: jc.getVars().put("list", new Object[] { "Hello", "World" });
061: Object o = e.evaluate(jc);
062: assertEquals("Result is not last evaluated expression",
063: "World", o);
064: }
065:
066: public void testForEachWithCollection() throws Exception {
067: Expression e = ExpressionFactory
068: .createExpression("foreach (item in list) item");
069: JexlContext jc = JexlHelper.createContext();
070: jc.getVars().put("list",
071: Arrays.asList(new Object[] { "Hello", "World" }));
072: Object o = e.evaluate(jc);
073: assertEquals("Result is not last evaluated expression",
074: "World", o);
075: }
076:
077: public void testForEachWithEnumeration() throws Exception {
078: Expression e = ExpressionFactory
079: .createExpression("foreach (item in list) item");
080: JexlContext jc = JexlHelper.createContext();
081: jc.getVars().put("list",
082: new StringTokenizer("Hello,World", ","));
083: Object o = e.evaluate(jc);
084: assertEquals("Result is not last evaluated expression",
085: "World", o);
086: }
087:
088: public void testForEachWithIterator() throws Exception {
089: Expression e = ExpressionFactory
090: .createExpression("foreach (item in list) item");
091: JexlContext jc = JexlHelper.createContext();
092: jc.getVars().put(
093: "list",
094: Arrays.asList(new Object[] { "Hello", "World" })
095: .iterator());
096: Object o = e.evaluate(jc);
097: assertEquals("Result is not last evaluated expression",
098: "World", o);
099: }
100:
101: public void testForEachWithMap() throws Exception {
102: Expression e = ExpressionFactory
103: .createExpression("foreach (item in list) item");
104: JexlContext jc = JexlHelper.createContext();
105: Map map = System.getProperties();
106: String lastProperty = (String) new ArrayList(map.values())
107: .get(System.getProperties().size() - 1);
108: jc.getVars().put("list", map);
109: Object o = e.evaluate(jc);
110: assertEquals("Result is not last evaluated expression",
111: lastProperty, o);
112: }
113:
114: public void testForEachWithBlock() throws Exception {
115: Expression e = ExpressionFactory
116: .createExpression("foreach (item in list) { x = x + item; }");
117: JexlContext jc = JexlHelper.createContext();
118: jc.getVars().put("list", new Object[] { "1", "1" });
119: jc.getVars().put("x", new Integer(0));
120: Object o = e.evaluate(jc);
121: assertEquals("Result is wrong", new Long(2), o);
122: assertEquals("x is wrong", new Long(2), jc.getVars().get("x"));
123: }
124:
125: public void testForEachWithListExpression() throws Exception {
126: Expression e = ExpressionFactory
127: .createExpression("foreach (item in list.keySet()) item");
128: JexlContext jc = JexlHelper.createContext();
129: Map map = System.getProperties();
130: String lastKey = (String) new ArrayList(map.keySet())
131: .get(System.getProperties().size() - 1);
132: jc.getVars().put("list", map);
133: Object o = e.evaluate(jc);
134: assertEquals("Result is not last evaluated expression",
135: lastKey, o);
136: }
137:
138: public void testForEachWithProperty() throws Exception {
139: Expression e = ExpressionFactory
140: .createExpression("foreach (item in list.cheeseList) item");
141: JexlContext jc = JexlHelper.createContext();
142: jc.getVars().put("list", new Foo());
143: Object o = e.evaluate(jc);
144: assertEquals("Result is not last evaluated expression", "brie",
145: o);
146: }
147: }
|