KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > expr > ExprUtilsTest


1 package com.tonbeller.wcf.expr;
2
3 import java.beans.PropertyDescriptor JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import junit.framework.TestCase;
8
9 /**
10  * @author av
11  */

12 public class ExprUtilsTest extends TestCase {
13   public class MyBean {
14     Object JavaDoc object = null;
15     int value = 4;
16     public int getValue() {
17       return value;
18     }
19     public void setValue(int newValue) {
20       this.value = newValue;
21     }
22     public Object JavaDoc getObject() {
23       return object;
24     }
25     public void setObject(Object JavaDoc object) {
26       this.object = object;
27     }
28   }
29   
30   MyBean bean;
31   ExprContext ctx;
32   
33   /**
34    * Constructor for ExprUtilsTest.
35    * @param arg0
36    */

37   public ExprUtilsTest(String JavaDoc arg0) {
38     super(arg0);
39   }
40
41   /**
42    * @see TestCase#setUp()
43    */

44   protected void setUp() throws Exception JavaDoc {
45     bean = new MyBean();
46     final Map JavaDoc map = new HashMap JavaDoc();
47     map.put("bean", bean);
48     
49     ctx = new ExprContext() {
50       public Object JavaDoc findBean(String JavaDoc name) {
51         return map.get(name);
52       }
53       public void setBean(String JavaDoc name, Object JavaDoc value) {
54         map.put(name, value);
55       }
56     };
57   }
58
59   public void testCheckExpr() {
60     try {
61       ExprUtils.checkExpr("${abc.def");
62       assertTrue("Exception expected", false);
63     }
64     catch (IllegalArgumentException JavaDoc e) {
65     }
66
67     try {
68       ExprUtils.checkExpr("${abc.def}");
69     }
70     catch (IllegalArgumentException JavaDoc e) {
71       assertTrue("unexpected exception", false);
72     }
73   }
74
75   public void testGetModelReference() {
76     Object JavaDoc obj = ExprUtils.getModelReference(ctx, "${bean.value}");
77     assertEquals(new Integer JavaDoc(bean.getValue()), obj);
78     obj = ExprUtils.getModelReference(ctx, "bean");
79     assertEquals(bean, obj);
80   }
81   
82   public void testBeanName() {
83     assertEquals("bean", ExprUtils.getBeanName("${bean.property}"));
84   }
85
86   public void testSetModelReference() {
87     ExprUtils.setModelReference(ctx, "${bean.value}", new Integer JavaDoc(27));
88     assertEquals(27, bean.getValue());
89   }
90
91   public void testSetBean1() {
92     MyBean b = new MyBean();
93     ExprUtils.setModelReference(ctx, "bean", b);
94     Object JavaDoc o = ExprUtils.getModelReference(ctx, "bean");
95     assertEquals(b, o);
96     o = ExprUtils.getModelReference(ctx, "#{bean}");
97     assertEquals(b, o);
98     o = ExprUtils.getModelReference(ctx, "${bean}");
99     assertEquals(b, o);
100   }
101
102   public void testSetBean2() {
103     MyBean b = new MyBean();
104     ExprUtils.setModelReference(ctx, "${bean}", b);
105     Object JavaDoc o = ExprUtils.getModelReference(ctx, "${bean}");
106     assertEquals(b, o);
107   }
108   
109   public void testSetBean3() {
110     Object JavaDoc obj1 = new Object JavaDoc();
111     ExprUtils.setModelReference(ctx, "${bean.object}", obj1);
112     assertEquals(obj1, bean.getObject());
113     Object JavaDoc obj2 = ExprUtils.getModelReference(ctx, "${bean.object}");
114     assertEquals(obj1, obj2);
115   }
116   
117   public void testPropertyDescriptor() {
118     PropertyDescriptor JavaDoc pd = ExprUtils.getPropertyDescriptor(ctx, "#{bean.value}");
119     assertEquals(Integer.TYPE, pd.getPropertyType());
120   }
121 }
122
Popular Tags