KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > expression > TestDynaBeans


1 /*
2  * Copyright 2002,2004 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 package org.apache.commons.jelly.expression;
17
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
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 JavaDoc[] args) {
46         TestRunner.run(suite());
47     }
48
49     public static Test suite() {
50         return new TestSuite(TestDynaBeans.class);
51     }
52
53     public TestDynaBeans(String JavaDoc testName) {
54         super(testName);
55     }
56
57     public void testDynaBeans() throws Exception JavaDoc {
58         DynaClass dynaClass = createDynaClass();
59         DynaBean dynaBean = dynaClass.newInstance();
60         dynaBean.set( "stringProperty", "foo" );
61         dynaBean.set( "intProperty", new Integer JavaDoc(24) );
62
63         context.setVariable("dbean", dynaBean);
64
65         assertExpression("${dbean.stringProperty}", "foo");
66         assertExpression("${dbean.intProperty}", new Integer JavaDoc(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 JavaDoc.class),
77             new DynaProperty("longProperty", Long.TYPE),
78             new DynaProperty("mappedProperty", Map JavaDoc.class),
79             new DynaProperty("mappedIntProperty", Map JavaDoc.class),
80             new DynaProperty("nullProperty", String JavaDoc.class),
81             new DynaProperty("shortProperty", Short.TYPE),
82             new DynaProperty("stringProperty", String JavaDoc.class),
83         };
84         return new BasicDynaClass("TestDynaClass", null, properties);
85     }
86
87
88     protected void assertExpression(String JavaDoc expressionText, Object JavaDoc expectedValue) throws Exception JavaDoc {
89         Expression expression = CompositeExpression.parse(expressionText, factory);
90         assertTrue( "Created a valid expression for: " + expressionText, expression != null );
91         Object JavaDoc value = expression.evaluate(context);
92         //assertEquals( "Expression for: " + expressionText + " is: " + expression, expectedValue, value );
93
assertEquals( "Wrong result for expression: " + expressionText, expectedValue, value );
94     }
95 }
96
Popular Tags