1 package org.openlaszlo.iv.flash.context; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.ListIterator ; 10 import java.util.ArrayList ; 11 12 public class BeanContextTest extends TestCase 13 { 14 public BeanContextTest( String testName ) 15 { 16 super( testName ); 17 } 18 19 public static void main( String [] args ) 20 { 21 junit.textui.TestRunner.run( suite() ); 22 } 23 24 public static Test suite() 25 { 26 TestSuite suite = new TestSuite( BeanContextTest.class ); 27 28 return suite; 29 } 30 31 42 public void testGetValueWithHashMapAsRoot() 43 { 44 HashMap map = new HashMap (); 45 46 map.put( "object", new Foo() ); 47 48 BeanContext ctx = new BeanContext( null, map ); 49 50 assertEquals( "1", ctx.getValue( "object.intField" ) ); 51 assertEquals( "foo.stringField", ctx.getValue( "object.stringField" ) ); 52 assertEquals( "arrayItemZero", ctx.getValue( "object.arrayField.0" ) ); 53 assertEquals( "arrayItemOne", ctx.getValue( "object.arrayField[1]" ) ); 54 55 assertEquals( "15", ctx.getValue( "object.stringField.length()" ) ); 56 } 57 58 public void testGetValueList() 59 { 60 List inList = new ArrayList (); 61 62 inList.add( new Foo() ); 63 inList.add( new Foo() ); 64 inList.add( new Foo() ); 65 inList.add( new Foo() ); 66 67 BeanContext context = new BeanContext(); 68 69 context.put( "list", inList ); 70 71 List l; 72 73 l = context.getValueList( "list" ); 74 75 assertEquals( 4, l.size() ); 76 77 ListIterator iter = l.listIterator(); 78 BeanContext childContext; 79 80 while ( iter.hasNext() ) 81 { 82 childContext = ( BeanContext ) iter.next(); 83 84 assertEquals( "Instance of Foo", 85 childContext.getValue( "root" ) ); 86 87 assertEquals( "foo.stringField", 88 childContext.getValue( "root.stringField" ) ); 89 } 90 } 91 92 public void testApply() 93 { 94 HashMap map = new HashMap (); 95 96 map.put( "object1", new Foo() ); 97 map.put( "object2", new Foo() ); 98 map.put( "object3", new Foo() ); 99 map.put( "object4", new Foo() ); 100 101 BeanContext context = new BeanContext( null, map ); 102 103 String initial = "'{object1.stringField}', '{object2.intField}', '{object3.stringField.length()}'"; 104 String expected = "'foo.stringField', '1', '15'"; 105 106 String actual = context.apply( initial ); 107 108 assertEquals( expected, actual ); 109 } 110 111 113 public static class Foo 114 { 115 private int intField = 1; 116 private String stringField = "foo.stringField"; 117 private String [] arrayField = 118 {"arrayItemZero", "arrayItemOne", "arrayItemTwo"}; 119 120 public int getIntField() 121 { 122 return intField; 123 } 124 125 public String getStringField() 126 { 127 return stringField; 128 } 129 130 public String [] getArrayField() 131 { 132 return arrayField; 133 } 134 135 public String toString() 136 { 137 return "Instance of Foo"; 138 } 139 } 140 } 141 | Popular Tags |