1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/12/19 21:41:58
6    */
7   package test.org.asyrinx.joey.gen.task;
8   
9   import java.util.HashMap;
10  import java.util.Map;
11  
12  import junit.framework.TestCase;
13  import ognl.Ognl;
14  import ognl.OgnlException;
15  
16  /***
17   * @author takeshi
18   */
19  public class OgnlTest extends TestCase {
20  
21      public static void main(String[] args) {
22          junit.swingui.TestRunner.run(OgnlTest.class);
23      }
24  
25      public static class A {
26  
27          private String propString = null;
28  
29          private A ref = null;
30  
31          public String getPropString() {
32              return propString;
33          }
34  
35          public void setPropString(String propString) {
36              this.propString = propString;
37          }
38  
39          public A getRef() {
40              return ref;
41          }
42  
43          public void setRef(A ref) {
44              this.ref = ref;
45          }
46      }
47  
48      public void testUsge() {
49          final A a1 = new A();
50          a1.setPropString("propOfA1");
51          final A a2 = new A();
52          a2.setPropString("propOfA2");
53          a2.setRef(a1);
54          final A a3 = new A();
55          a3.setPropString("propOfA3");
56          a3.setRef(a1);
57          //
58          final Map beans = new HashMap();
59          beans.put("a1", a1);
60          beans.put("a2", a2);
61          //final Map context = Ognl.createDefaultContext(beans);
62          final Map context = beans;
63          //
64          //OgnlRuntime.setPropertyAccessor(A.class, new
65          // ObjectPropertyAccessor());
66          //
67          assertEquals(null, Ognl.getRoot(context));
68          try {
69              assertEquals(a1, Ognl.getValue("a1", context));
70              assertEquals(a2, Ognl.getValue("a2", context));
71              assertEquals(a1, Ognl.getValue("a2.ref", context));
72              assertEquals(a1, Ognl.getValue("a2.getRef()", context));
73              assertEquals("propOfA2", Ognl.getValue("a2.propString", context));
74              assertEquals("propOfA1", Ognl.getValue("a2.ref.propString", context));
75              Ognl.setValue("a2.propString", context, "propStringAAA");
76              assertEquals("propStringAAA", Ognl.getValue("a2.propString", context));
77              Ognl.setValue("a2.ref", context, a3);
78              assertEquals(a3, Ognl.getValue("a2.ref", context));
79              final Object map1 = Ognl.getValue("#{\"1\":\"aaa\",\"2\":\"bbb\"}", null);
80              assertEquals(true, map1 instanceof Map);
81              assertEquals(2, ((Map) map1).size());
82              assertEquals("aaa", ((Map) map1).get("1"));
83              assertEquals("bbb", ((Map) map1).get("2"));
84              final Object map2 = Ognl.getValue("#{1:\"aaa\",2:\"bbb\"}", null);
85              assertEquals(true, map2 instanceof Map);
86              assertEquals(2, ((Map) map2).size());
87              assertEquals("aaa", ((Map) map2).get(new Integer(1)));
88              assertEquals("bbb", ((Map) map2).get(new Integer(2)));
89              //
90              assertEquals(a3, Ognl.getValue("ref", a2));
91              assertEquals("propOfA3", Ognl.getValue("ref.propString", a2));
92              //
93              assertEquals(a3, Ognl.getValue("ref", a2));
94              assertEquals("propOfA3", Ognl.getValue("ref.propString", a2));
95  
96          } catch (OgnlException e) {
97              e.printStackTrace();
98              fail();
99          }
100     }
101 
102 }