1 package com.tonbeller.wcf.expr; 2 3 import java.beans.PropertyDescriptor ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 import junit.framework.TestCase; 8 9 12 public class ExprUtilsTest extends TestCase { 13 public class MyBean { 14 Object 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 getObject() { 23 return object; 24 } 25 public void setObject(Object object) { 26 this.object = object; 27 } 28 } 29 30 MyBean bean; 31 ExprContext ctx; 32 33 37 public ExprUtilsTest(String arg0) { 38 super(arg0); 39 } 40 41 44 protected void setUp() throws Exception { 45 bean = new MyBean(); 46 final Map map = new HashMap (); 47 map.put("bean", bean); 48 49 ctx = new ExprContext() { 50 public Object findBean(String name) { 51 return map.get(name); 52 } 53 public void setBean(String name, Object 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 e) { 65 } 66 67 try { 68 ExprUtils.checkExpr("${abc.def}"); 69 } 70 catch (IllegalArgumentException e) { 71 assertTrue("unexpected exception", false); 72 } 73 } 74 75 public void testGetModelReference() { 76 Object obj = ExprUtils.getModelReference(ctx, "${bean.value}"); 77 assertEquals(new Integer (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 (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 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 o = ExprUtils.getModelReference(ctx, "${bean}"); 106 assertEquals(b, o); 107 } 108 109 public void testSetBean3() { 110 Object obj1 = new Object (); 111 ExprUtils.setModelReference(ctx, "${bean.object}", obj1); 112 assertEquals(obj1, bean.getObject()); 113 Object obj2 = ExprUtils.getModelReference(ctx, "${bean.object}"); 114 assertEquals(obj1, obj2); 115 } 116 117 public void testPropertyDescriptor() { 118 PropertyDescriptor pd = ExprUtils.getPropertyDescriptor(ctx, "#{bean.value}"); 119 assertEquals(Integer.TYPE, pd.getPropertyType()); 120 } 121 } 122 | Popular Tags |