01: /*
02: * Copyright 2002,2004 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.jelly.expression;
17:
18: import java.util.List;
19: import java.util.Map;
20:
21: import junit.framework.Test;
22: import junit.framework.TestCase;
23: import junit.framework.TestSuite;
24: import junit.textui.TestRunner;
25:
26: import org.apache.commons.beanutils.BasicDynaClass;
27: import org.apache.commons.beanutils.DynaBean;
28: import org.apache.commons.beanutils.DynaClass;
29: import org.apache.commons.beanutils.DynaProperty;
30:
31: import org.apache.commons.jelly.JellyContext;
32: import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
33:
34: /**
35: * Tests the use of Expression parsing
36: *
37: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38: * @version $Revision: 155420 $
39: */
40: public class TestDynaBeans extends TestCase {
41:
42: protected JellyContext context = new JellyContext();
43: protected ExpressionFactory factory = new JexlExpressionFactory();
44:
45: public static void main(String[] args) {
46: TestRunner.run(suite());
47: }
48:
49: public static Test suite() {
50: return new TestSuite(TestDynaBeans.class);
51: }
52:
53: public TestDynaBeans(String testName) {
54: super (testName);
55: }
56:
57: public void testDynaBeans() throws Exception {
58: DynaClass dynaClass = createDynaClass();
59: DynaBean dynaBean = dynaClass.newInstance();
60: dynaBean.set("stringProperty", "foo");
61: dynaBean.set("intProperty", new Integer(24));
62:
63: context.setVariable("dbean", dynaBean);
64:
65: assertExpression("${dbean.stringProperty}", "foo");
66: assertExpression("${dbean.intProperty}", new Integer(24));
67: }
68:
69: protected DynaClass createDynaClass() {
70: DynaProperty[] properties = {
71: new DynaProperty("booleanProperty", Boolean.TYPE),
72: new DynaProperty("booleanSecond", Boolean.TYPE),
73: new DynaProperty("doubleProperty", Double.TYPE),
74: new DynaProperty("floatProperty", Float.TYPE),
75: new DynaProperty("intProperty", Integer.TYPE),
76: new DynaProperty("listIndexed", List.class),
77: new DynaProperty("longProperty", Long.TYPE),
78: new DynaProperty("mappedProperty", Map.class),
79: new DynaProperty("mappedIntProperty", Map.class),
80: new DynaProperty("nullProperty", String.class),
81: new DynaProperty("shortProperty", Short.TYPE),
82: new DynaProperty("stringProperty", String.class), };
83: return new BasicDynaClass("TestDynaClass", null, properties);
84: }
85:
86: protected void assertExpression(String expressionText,
87: Object expectedValue) throws Exception {
88: Expression expression = CompositeExpression.parse(
89: expressionText, factory);
90: assertTrue("Created a valid expression for: " + expressionText,
91: expression != null);
92: Object value = expression.evaluate(context);
93: //assertEquals( "Expression for: " + expressionText + " is: " + expression, expectedValue, value );
94: assertEquals("Wrong result for expression: " + expressionText,
95: expectedValue, value);
96: }
97: }
|