1 package gls.ch06.s05;
2
3
4 import groovy.lang.Closure;
5 import junit.framework.TestCase;
6
7 import gls.ch06.s05.testClasses.Tt1cgi;
8 import gls.ch06.s05.testClasses.Tt1cgo;
9 import gls.ch06.s05.testClasses.Tt1gi;
10 import gls.ch06.s05.testClasses.Tt1go;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 /***
29 * @author John Wilson
30 *
31 */
32
33 public class JName1Test extends TestCase {
34 public void testObjectSupportNameHandling() {
35 final Tt1go obj = new Tt1go();
36 final String newX = "new x";
37 final String newX1 = "new x1";
38 final String newX2 = "new x2";
39 final String newX3 = "new x3";
40
41 assertTrue(obj.getProperty("x") == obj.getX());
42 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == obj.x);
43 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
44
45 obj.setProperty("x", newX);
46 obj.getMetaClass().setAttribute(obj, "x", newX1);
47
48 assertTrue(obj.getProperty("x") == newX);
49 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX1);
50
51 obj.setX(newX2);
52 obj.x = newX3;
53
54 assertTrue(obj.getProperty("x") == newX2);
55 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX3);
56 }
57
58 public void testObjectSupportNameHandling1() {
59 final Tt1go obj = new Tt1go() {};
60 final String newX = "new x";
61 final String newX1 = "new x1";
62 final String newX2 = "new x2";
63 final String newX3 = "new x3";
64
65 assertTrue(obj.getProperty("x") == obj.getX());
66 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == obj.x);
67 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
68
69 obj.setProperty("x", newX);
70 obj.getMetaClass().setAttribute(obj, "x", newX1);
71
72 assertTrue(obj.getProperty("x") == newX);
73 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX1);
74
75 obj.setX(newX2);
76 obj.x = newX3;
77
78 assertTrue(obj.getProperty("x") == newX2);
79 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX3);
80 }
81
82 public void testObjectSupportNameHandlingWitnClosureValues() {
83 final Tt1cgo obj = new Tt1cgo();
84 final Closure newX = new Closure(null) {
85 public Object doCall(final Object params) {
86 return "new x";
87 }
88 };
89 final Closure newX1 = new Closure(null) {
90 public Object doCall(final Object params) {
91 return "new x1";
92 }
93 };
94 final Closure newX2 = new Closure(null) {
95 public Object doCall(final Object params) {
96 return "new x2";
97 }
98 };
99 final Closure newX3 = new Closure(null) {
100 public Object doCall(final Object params) {
101 return "new x3";
102 }
103 };
104
105 assertTrue(((Closure)obj.getProperty("x")).call() == obj.getX().call());
106 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
107 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
108
109 obj.setProperty("x", newX);
110 obj.getMetaClass().setAttribute(obj, "x", newX1);
111
112 assertTrue(((Closure)obj.getProperty("x")).call() == newX.call());
113 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
114
115 obj.setX(newX2);
116 obj.x = newX3;
117
118 assertTrue(((Closure)obj.getProperty("x")).call() == newX2.call());
119 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
120 }
121
122 public void testObjectSupportNameHandlingWitnClosureValuesi() {
123 final Tt1cgo obj = new Tt1cgo() {};
124 final Closure newX = new Closure(null) {
125 public Object doCall(final Object params) {
126 return "new x";
127 }
128 };
129 final Closure newX1 = new Closure(null) {
130 public Object doCall(final Object params) {
131 return "new x1";
132 }
133 };
134 final Closure newX2 = new Closure(null) {
135 public Object doCall(final Object params) {
136 return "new x2";
137 }
138 };
139 final Closure newX3 = new Closure(null) {
140 public Object doCall(final Object params) {
141 return "new x3";
142 }
143 };
144
145 assertTrue(((Closure)obj.getProperty("x")).call() == obj.getX().call());
146 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
147 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
148
149 obj.setProperty("x", newX);
150 obj.getMetaClass().setAttribute(obj, "x", newX1);
151
152 assertTrue(((Closure)obj.getProperty("x")).call() == newX.call());
153 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
154
155 obj.setX(newX2);
156 obj.x = newX3;
157
158 assertTrue(((Closure)obj.getProperty("x")).call() == newX2.call());
159 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
160 }
161
162 public void testMetaClassNameHandling() {
163 final Tt1gi obj = new Tt1gi();
164 final String newX = "new x";
165 final String newX1 = "new x1";
166 final String newX2 = "new x2";
167 final String newX3 = "new x3";
168
169 assertTrue("dynamic property".equals(obj.getProperty("x")));
170 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == obj.x);
171 assertTrue("dynamic method".equals(obj.invokeMethod("x", new Object[]{})));
172
173 obj.setProperty("x", newX);
174 obj.getMetaClass().setAttribute(obj, "x", newX1);
175
176 assertTrue("dynamic property".equals(obj.getProperty("x")));
177 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX1);
178
179 obj.setX(newX2);
180 obj.x = newX3;
181
182 assertTrue("dynamic property".equals(obj.getProperty("x")));
183 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX3);
184 }
185
186 public void testMetaClassNameHandling1() {
187 final Tt1gi obj = new Tt1gi() {};
188 final String newX = "new x";
189 final String newX1 = "new x1";
190 final String newX2 = "new x2";
191 final String newX3 = "new x3";
192
193 assertTrue("dynamic property".equals(obj.getProperty("x")));
194 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == obj.x);
195 assertTrue("dynamic method".equals(obj.invokeMethod("x", new Object[]{})));
196
197 obj.setProperty("x", newX);
198 obj.getMetaClass().setAttribute(obj, "x", newX1);
199
200 assertTrue("dynamic property".equals(obj.getProperty("x")));
201 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX1);
202
203 obj.setX(newX2);
204 obj.x = newX3;
205
206 assertTrue("dynamic property".equals(obj.getProperty("x")));
207 assertTrue(obj.getMetaClass().getAttribute(obj, "x") == newX3);
208 }
209
210 public void testMetaClassNameHandlingWithClosures() {
211 final Tt1cgi obj = new Tt1cgi();
212 final Closure newX = new Closure(null) {
213 public Object doCall(final Object params) {
214 return "new x";
215 }
216 };
217 final Closure newX1 = new Closure(null) {
218 public Object doCall(final Object params) {
219 return "new x1";
220 }
221 };
222 final Closure newX2 = new Closure(null) {
223 public Object doCall(final Object params) {
224 return "new x2";
225 }
226 };
227 final Closure newX3 = new Closure(null) {
228 public Object doCall(final Object params) {
229 return "new x3";
230 }
231 };
232
233 assertTrue(((Closure)obj.getProperty("x")).call() == obj.getX().call());
234 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
235 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
236
237 obj.setProperty("x", newX);
238 obj.getMetaClass().setAttribute(obj, "x", newX1);
239
240 assertTrue(((Closure)obj.getProperty("x")).call() == newX.call());
241 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
242
243 obj.setX(newX2);
244 obj.x = newX3;
245
246 assertTrue(((Closure)obj.getProperty("x")).call() == newX2.call());
247 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
248 }
249
250 public void testMetaClassNameHandlingWithClosures1() {
251 final Tt1cgi obj = new Tt1cgi() {};
252 final Closure newX = new Closure(null) {
253 public Object doCall(final Object params) {
254 return "new x";
255 }
256 };
257 final Closure newX1 = new Closure(null) {
258 public Object doCall(final Object params) {
259 return "new x1";
260 }
261 };
262 final Closure newX2 = new Closure(null) {
263 public Object doCall(final Object params) {
264 return "new x2";
265 }
266 };
267 final Closure newX3 = new Closure(null) {
268 public Object doCall(final Object params) {
269 return "new x3";
270 }
271 };
272
273 assertTrue(((Closure)obj.getProperty("x")).call() == obj.getX().call());
274 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
275 assertTrue(obj.invokeMethod("x", new Object[]{}) == obj.x());
276
277 obj.setProperty("x", newX);
278 obj.getMetaClass().setAttribute(obj, "x", newX1);
279
280 assertTrue(((Closure)obj.getProperty("x")).call() == newX.call());
281 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
282
283 obj.setX(newX2);
284 obj.x = newX3;
285
286 assertTrue(((Closure)obj.getProperty("x")).call() == newX2.call());
287 assertTrue(((Closure)obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
288 }
289 }