01: /*
02: * Copyright 2002-2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.apache.commons.jexl;
17:
18: import junit.framework.TestCase;
19:
20: /**
21: * Tests for blocks
22: * @since 1.1
23: */
24: public class BlockTest extends TestCase {
25:
26: /**
27: * Create the test
28: *
29: * @param testName name of the test
30: */
31: public BlockTest(String testName) {
32: super (testName);
33: }
34:
35: public void testBlockSimple() throws Exception {
36: Expression e = ExpressionFactory
37: .createExpression("if (true) { 'hello'; }");
38: JexlContext jc = JexlHelper.createContext();
39: Object o = e.evaluate(jc);
40: assertEquals("Result is wrong", "hello", o);
41: }
42:
43: public void testBlockExecutesAll() throws Exception {
44: Expression e = ExpressionFactory
45: .createExpression("if (true) { x = 'Hello'; y = 'World';}");
46: JexlContext jc = JexlHelper.createContext();
47: Object o = e.evaluate(jc);
48: assertEquals("First result is wrong", "Hello", jc.getVars()
49: .get("x"));
50: assertEquals("Second result is wrong", "World", jc.getVars()
51: .get("y"));
52: assertEquals("Block result is wrong", "World", o);
53: }
54:
55: public void testEmptyBlock() throws Exception {
56: Expression e = ExpressionFactory
57: .createExpression("if (true) { }");
58: JexlContext jc = JexlHelper.createContext();
59: Object o = e.evaluate(jc);
60: assertNull("Result is wrong", o);
61: }
62:
63: public void testBlockLastExecuted01() throws Exception {
64: Expression e = ExpressionFactory
65: .createExpression("if (true) { x = 1; } else { x = 2; }");
66: JexlContext jc = JexlHelper.createContext();
67: Object o = e.evaluate(jc);
68: assertEquals("Block result is wrong", new Integer(1), o);
69: }
70:
71: public void testBlockLastExecuted02() throws Exception {
72: Expression e = ExpressionFactory
73: .createExpression("if (false) { x = 1; } else { x = 2; }");
74: JexlContext jc = JexlHelper.createContext();
75: Object o = e.evaluate(jc);
76: assertEquals("Block result is wrong", new Integer(2), o);
77: }
78:
79: public void testNestedBlock() throws Exception {
80: Expression e = ExpressionFactory
81: .createExpression("if (true) { x = 'hello'; y = 'world';"
82: + " if (true) { x; } y; }");
83: JexlContext jc = JexlHelper.createContext();
84: Object o = e.evaluate(jc);
85: assertEquals("Block result is wrong", "world", o);
86: }
87:
88: }
|