1 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 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 }; 84 return new BasicDynaClass("TestDynaClass", null, properties); 85 } 86 87 88 protected void assertExpression(String expressionText, Object expectedValue) throws Exception { 89 Expression expression = CompositeExpression.parse(expressionText, factory); 90 assertTrue( "Created a valid expression for: " + expressionText, expression != null ); 91 Object value = expression.evaluate(context); 92 assertEquals( "Wrong result for expression: " + expressionText, expectedValue, value ); 94 } 95 } 96 | Popular Tags |