KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > context > BeanContextTest


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 JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.ListIterator JavaDoc;
10 import java.util.ArrayList JavaDoc;
11
12 public class BeanContextTest extends TestCase
13 {
14     public BeanContextTest( String JavaDoc testName )
15     {
16         super( testName );
17     }
18
19     public static void main( String JavaDoc[] 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 // Object as root is currently not handled by Jexl
32
//
33
// public void testGetValueWithObjectAsRoot()
34
// {
35
// BeanContext ctx = new BeanContext( null, new Foo() );
36
//
37
// assertEquals( "1", ctx.getValue( "intField" ) );
38
// assertEquals( "foo.stringField", ctx.getValue( "stringField" ) );
39
// assertEquals( "arrayItemZero", ctx.getValue( "arrayField[1]" ) );
40
// }
41

42     public void testGetValueWithHashMapAsRoot()
43     {
44         HashMap JavaDoc map = new HashMap JavaDoc();
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 JavaDoc inList = new ArrayList JavaDoc();
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 JavaDoc l;
72
73         l = context.getValueList( "list" );
74
75         assertEquals( 4, l.size() );
76
77         ListIterator JavaDoc 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc initial = "'{object1.stringField}', '{object2.intField}', '{object3.stringField.length()}'";
104         String JavaDoc expected = "'foo.stringField', '1', '15'";
105
106         String JavaDoc actual = context.apply( initial );
107
108         assertEquals( expected, actual );
109     }
110
111     // "Bean" for tests
112

113     public static class Foo
114     {
115         private int intField = 1;
116         private String JavaDoc stringField = "foo.stringField";
117         private String JavaDoc[] arrayField =
118             {"arrayItemZero", "arrayItemOne", "arrayItemTwo"};
119
120         public int getIntField()
121         {
122             return intField;
123         }
124
125         public String JavaDoc getStringField()
126         {
127             return stringField;
128         }
129
130         public String JavaDoc[] getArrayField()
131         {
132             return arrayField;
133         }
134
135         public String JavaDoc toString()
136         {
137             return "Instance of Foo";
138         }
139     }
140 }
141
Popular Tags