1 package org.weda.property;
2
3 import org.weda.test.WedaTestCase;
4 import org.weda.domain.Address;
5 import org.weda.domain.Company;
6 import org.weda.property.impl.PropertyListenerResultImpl;
7
8 /**
9 *
10 * @author tim
11 */
12 public class PropertyValueTest
13 extends WedaTestCase implements PropertyGetOperationListener
14 {
15 private boolean beforeGetFired, afterGetFired = false;
16 private boolean beforeGetChangeValue = false;
17 private boolean afterGetChangeValue = false;
18
19 public PropertyValueTest(String name) throws Exception {
20 super(name);
21 }
22
23 public void test_config() throws Exception {
24 PropertyValue expr = (PropertyValue)registry
25 .getService(PropertyValue.class);
26 assertNotNull(expr);
27 }
28
29 public void test_compileGetter() throws Exception {
30 PropertyValue expr = (PropertyValue)registry
31 .getService(PropertyValue.class);
32 Integer id1 = expr.compileGetter(Company.class, "id");
33 Integer id2 = expr.compileGetter(Company.class, "id");
34 assertNotNull(id1);
35 assertEquals(id1, id2);
36 assertNotNull(expr.compileGetter(Company.class, "address.street"));
37 }
38
39 public void test_getValue() throws Exception {
40 PropertyValue expr = (PropertyValue)registry
41 .getService(PropertyValue.class);
42 Integer id_ind = expr.compileGetter(Company.class, "id");
43 Integer address_ind = expr.compileGetter(Company.class, "address");
44 Integer street_ind =
45 expr.compileGetter(Company.class, "address.street");
46 Integer name_ind = expr.compileGetter(Company.class, "name");
47 Company comp = new Company();
48 comp.setId(1l);
49 assertEquals(1l, expr.getValue(comp, id_ind));
50 assertNull(expr.getValue(comp, name_ind));
51 assertNull(expr.getValue(comp, address_ind));
52 assertNull(expr.getValue(comp, street_ind));
53 Address addr = new Address();
54 comp.setAddress(addr);
55 assertNotNull(expr.getValue(comp, address_ind));
56 assertNull(expr.getValue(comp, street_ind));
57 addr.setStreet("street name");
58 assertEquals("street name", expr.getValue(comp, street_ind));
59 }
60
61 public void test_compileSetter() throws Exception {
62 PropertyValue expr = (PropertyValue)registry
63 .getService(PropertyValue.class);
64 Integer id1=expr.compileSetter(Company.class, "id");
65 Integer id2=expr.compileSetter(Company.class, "id");
66 assertNotNull(id1);
67 assertEquals(id1, id2);
68 assertNotNull(expr.compileSetter(Company.class, "address.street"));
69 }
70
71 public void test_setValue() throws Exception {
72 PropertyValue expr = (PropertyValue)registry
73 .getService(PropertyValue.class);
74
75 Integer id1=expr.compileSetter(Company.class, "id");
76 Integer addrId=expr.compileSetter(Company.class, "address");
77 Integer streetId=expr.compileSetter(Company.class, "address.street");
78 Company comp=new Company();
79 expr.setValue(comp, id1,1l);
80 assertEquals(1, comp.getId().intValue());
81 expr.setValue(comp, addrId, new Address());
82 assertNotNull(comp.getAddress());
83 expr.setValue(comp, streetId, "street");
84 assertEquals("street", comp.getAddress().getStreet());
85 }
86
87 public void test_getOperationListener() throws Exception {
88 PropertyValue expr = (PropertyValue)registry
89 .getService(PropertyValue.class);
90 Integer id = expr.compileGetter(Company.class, "id");
91 expr.addGetOperationListener(Company.class, "id", this);
92 Company comp = new Company();
93 comp.setId(10l);
94
95 clearListenerStates();
96 assertEquals(10l, expr.getValue(comp, id));
97 assertTrue(beforeGetFired);
98 assertTrue(afterGetFired);
99
100 clearListenerStates();
101 beforeGetChangeValue = true;
102 assertEquals(5, expr.getValue(comp, id));
103
104 clearListenerStates();
105 afterGetChangeValue = true;
106 assertEquals(6, expr.getValue(comp, id));
107
108 clearListenerStates();
109 expr.removeGetListener(this);
110 assertEquals(10l, expr.getValue(comp, id));
111 assertFalse(beforeGetFired);
112 assertFalse(afterGetFired);
113 }
114
115 private void clearListenerStates(){
116 beforeGetFired = false;
117 afterGetFired = false;
118 beforeGetChangeValue = false;
119 afterGetChangeValue = false;
120 }
121
122 public PropertyListenerResult beforeGet(Object propOwner, String propName)
123 throws PropertyListenerException
124 {
125 beforeGetFired = true;
126 if (beforeGetChangeValue)
127 return new PropertyListenerResultImpl(5);
128 else
129 return null;
130 }
131
132 public PropertyListenerResult afterGet(
133 Object propOwner, String propName, Object propValue)
134 throws PropertyListenerException
135 {
136 afterGetFired = true;
137 if (afterGetChangeValue)
138 return new PropertyListenerResultImpl(6);
139 return null;
140 }
141 }