001: package org.openlaszlo.iv.flash.context;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006:
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.ListIterator;
010: import java.util.ArrayList;
011:
012: public class BeanContextTest extends TestCase {
013: public BeanContextTest(String testName) {
014: super (testName);
015: }
016:
017: public static void main(String[] args) {
018: junit.textui.TestRunner.run(suite());
019: }
020:
021: public static Test suite() {
022: TestSuite suite = new TestSuite(BeanContextTest.class);
023:
024: return suite;
025: }
026:
027: // Object as root is currently not handled by Jexl
028: //
029: // public void testGetValueWithObjectAsRoot()
030: // {
031: // BeanContext ctx = new BeanContext( null, new Foo() );
032: //
033: // assertEquals( "1", ctx.getValue( "intField" ) );
034: // assertEquals( "foo.stringField", ctx.getValue( "stringField" ) );
035: // assertEquals( "arrayItemZero", ctx.getValue( "arrayField[1]" ) );
036: // }
037:
038: public void testGetValueWithHashMapAsRoot() {
039: HashMap map = new HashMap();
040:
041: map.put("object", new Foo());
042:
043: BeanContext ctx = new BeanContext(null, map);
044:
045: assertEquals("1", ctx.getValue("object.intField"));
046: assertEquals("foo.stringField", ctx
047: .getValue("object.stringField"));
048: assertEquals("arrayItemZero", ctx
049: .getValue("object.arrayField.0"));
050: assertEquals("arrayItemOne", ctx
051: .getValue("object.arrayField[1]"));
052:
053: assertEquals("15", ctx.getValue("object.stringField.length()"));
054: }
055:
056: public void testGetValueList() {
057: List inList = new ArrayList();
058:
059: inList.add(new Foo());
060: inList.add(new Foo());
061: inList.add(new Foo());
062: inList.add(new Foo());
063:
064: BeanContext context = new BeanContext();
065:
066: context.put("list", inList);
067:
068: List l;
069:
070: l = context.getValueList("list");
071:
072: assertEquals(4, l.size());
073:
074: ListIterator iter = l.listIterator();
075: BeanContext childContext;
076:
077: while (iter.hasNext()) {
078: childContext = (BeanContext) iter.next();
079:
080: assertEquals("Instance of Foo", childContext
081: .getValue("root"));
082:
083: assertEquals("foo.stringField", childContext
084: .getValue("root.stringField"));
085: }
086: }
087:
088: public void testApply() {
089: HashMap map = new HashMap();
090:
091: map.put("object1", new Foo());
092: map.put("object2", new Foo());
093: map.put("object3", new Foo());
094: map.put("object4", new Foo());
095:
096: BeanContext context = new BeanContext(null, map);
097:
098: String initial = "'{object1.stringField}', '{object2.intField}', '{object3.stringField.length()}'";
099: String expected = "'foo.stringField', '1', '15'";
100:
101: String actual = context.apply(initial);
102:
103: assertEquals(expected, actual);
104: }
105:
106: // "Bean" for tests
107:
108: public static class Foo {
109: private int intField = 1;
110: private String stringField = "foo.stringField";
111: private String[] arrayField = { "arrayItemZero",
112: "arrayItemOne", "arrayItemTwo" };
113:
114: public int getIntField() {
115: return intField;
116: }
117:
118: public String getStringField() {
119: return stringField;
120: }
121:
122: public String[] getArrayField() {
123: return arrayField;
124: }
125:
126: public String toString() {
127: return "Instance of Foo";
128: }
129: }
130: }
|