KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > TestParameterPropertyWorker


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.Collections JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ErrorLog;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.service.BodyBuilder;
24 import org.apache.hivemind.service.MethodSignature;
25 import org.apache.hivemind.test.HiveMindTestCase;
26 import org.apache.tapestry.BaseComponent;
27 import org.apache.tapestry.IComponent;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.apache.tapestry.spec.IParameterSpecification;
30 import org.apache.tapestry.spec.ParameterSpecification;
31 import org.easymock.MockControl;
32
33 /**
34  * Tests for {@link org.apache.tapestry.enhance.ParameterPropertyWorker}.
35  *
36  * @author Howard M. Lewis Ship
37  */

38 public class TestParameterPropertyWorker extends HiveMindTestCase
39 {
40
41     private ParameterSpecification buildParameterSpec(String JavaDoc propertyName, String JavaDoc type,
42             Location location)
43     {
44         ParameterSpecification ps = new ParameterSpecification();
45
46         ps.setPropertyName(propertyName);
47         ps.setType(type);
48         ps.setLocation(location);
49
50         return ps;
51     }
52
53     private IComponentSpecification buildComponentSpecification(String JavaDoc parameterName,
54             IParameterSpecification ps)
55     {
56         MockControl c = newControl(IComponentSpecification.class);
57         IComponentSpecification result = (IComponentSpecification) c.getMock();
58
59         result.getParameterNames();
60
61         c.setReturnValue(Collections.singletonList(parameterName));
62
63         result.getParameter(parameterName);
64         c.setReturnValue(ps);
65
66         return result;
67     }
68
69     public void testFailure() throws Exception JavaDoc
70     {
71         Location l = fabricateLocation(207);
72
73         IComponentSpecification spec = buildComponentSpecification("fred", buildParameterSpec(
74                 "wilma",
75                 "String",
76                 l));
77
78         MockControl opc = newControl(EnhancementOperation.class);
79         EnhancementOperation op = (EnhancementOperation) opc.getMock();
80
81         op.convertTypeName("String");
82         Throwable JavaDoc ex = new ApplicationRuntimeException("Simulated error.");
83         opc.setThrowable(ex);
84
85         op.getBaseClass();
86         opc.setReturnValue(BaseComponent.class);
87
88         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
89
90         log
91                 .error(
92                         "Error adding property wilma to class org.apache.tapestry.BaseComponent: Simulated error.",
93                         l,
94                         ex);
95
96         replayControls();
97
98         ParameterPropertyWorker w = new ParameterPropertyWorker();
99         w.setErrorLog(log);
100
101         w.performEnhancement(op, spec);
102
103         verifyControls();
104     }
105
106     /**
107      * Tests for ordinary, object type of binding (as opposed to primitive types, which have a
108      * slightly different control flow.
109      */

110
111     public void testStandard()
112     {
113         IComponentSpecification spec = buildComponentSpecification("fred", buildParameterSpec(
114                 "fred",
115                 null,
116                 null));
117
118         MockControl opc = newControl(EnhancementOperation.class);
119         EnhancementOperation op = (EnhancementOperation) opc.getMock();
120
121         op.getPropertyType("fred");
122         opc.setReturnValue(String JavaDoc.class);
123
124         op.claimProperty("fred");
125
126         op.addField("_$fred", String JavaDoc.class);
127         op.addField("_$fred$Default", String JavaDoc.class);
128         op.addField("_$fred$Cached", boolean.class);
129
130         op.getClassReference(String JavaDoc.class);
131         opc.setReturnValue("_class$String");
132
133         BodyBuilder builder = new BodyBuilder();
134         builder.begin();
135         builder.addln("if (_$fred$Cached) return _$fred;");
136         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"fred\");");
137         builder.addln("if (binding == null) return _$fred$Default;");
138         builder.add("java.lang.String result = ");
139         builder.addln("(java.lang.String) binding.getObject(_class$String);");
140         builder.addln("if (isRendering() || binding.isInvariant())");
141         builder.begin();
142         builder.addln("_$fred = result;");
143         builder.addln("_$fred$Cached = true;");
144         builder.end();
145         builder.addln("return result;");
146         builder.end();
147
148         op.getAccessorMethodName("fred");
149         opc.setReturnValue("getFred");
150
151         op.addMethod(
152                 Modifier.PUBLIC,
153                 new MethodSignature(String JavaDoc.class, "getFred", null, null),
154                 builder.toString());
155
156         builder.clear();
157
158         builder.begin();
159         builder.addln("if (! isInActiveState())");
160         builder.begin();
161         builder.addln("_$fred$Default = $1;");
162         builder.addln("return;");
163         builder.end();
164
165         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"fred\");");
166
167         builder.addln("if (binding == null)");
168         builder
169                 .addln(" throw new org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'fred' is not bound and can not be updated.\");");
170
171         builder.addln("binding.setObject(($w) $1);");
172
173         builder.addln("if (isRendering())");
174         builder.begin();
175         builder.addln("_$fred = $1;");
176         builder.addln("_$fred$Cached = true;");
177         builder.end();
178         builder.end();
179
180         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class JavaDoc[]
181         { String JavaDoc.class }, null), builder.toString());
182
183         BodyBuilder expectedCleanup = new BodyBuilder();
184
185         expectedCleanup.addln("org.apache.tapestry.IBinding fredBinding = getBinding(\"fred\");");
186         expectedCleanup.addln("if (_$fred$Cached && ! fredBinding.isInvariant())");
187         expectedCleanup.begin();
188         expectedCleanup.addln("_$fred$Cached = false;");
189         expectedCleanup.addln("_$fred = _$fred$Default;");
190         expectedCleanup.end();
191
192         op.extendMethodImplementation(
193                 IComponent.class,
194                 EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
195                 expectedCleanup.toString());
196
197         replayControls();
198
199         ParameterPropertyWorker w = new ParameterPropertyWorker();
200
201         w.performEnhancement(op, spec);
202
203         verifyControls();
204     }
205
206     /**
207      * Test where the parameter name does not equal the property name. The parameter is "barney",
208      * but the binding is "fred".
209      */

210
211     public void testDifferentPropertyName()
212     {
213         IComponentSpecification spec = buildComponentSpecification("barney", buildParameterSpec(
214                 "fred",
215                 null,
216                 null));
217
218         MockControl opc = newControl(EnhancementOperation.class);
219         EnhancementOperation op = (EnhancementOperation) opc.getMock();
220
221         op.getPropertyType("fred");
222         opc.setReturnValue(String JavaDoc.class);
223
224         op.claimProperty("fred");
225
226         op.addField("_$fred", String JavaDoc.class);
227         op.addField("_$fred$Default", String JavaDoc.class);
228         op.addField("_$fred$Cached", boolean.class);
229
230         op.getClassReference(String JavaDoc.class);
231         opc.setReturnValue("_class$String");
232
233         BodyBuilder builder = new BodyBuilder();
234         builder.begin();
235         builder.addln("if (_$fred$Cached) return _$fred;");
236         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"barney\");");
237         builder.addln("if (binding == null) return _$fred$Default;");
238         builder.add("java.lang.String result = ");
239         builder.addln("(java.lang.String) binding.getObject(_class$String);");
240         builder.addln("if (isRendering() || binding.isInvariant())");
241         builder.begin();
242         builder.addln("_$fred = result;");
243         builder.addln("_$fred$Cached = true;");
244         builder.end();
245         builder.addln("return result;");
246         builder.end();
247
248         op.getAccessorMethodName("fred");
249         opc.setReturnValue("getFred");
250
251         op.addMethod(
252                 Modifier.PUBLIC,
253                 new MethodSignature(String JavaDoc.class, "getFred", null, null),
254                 builder.toString());
255
256         builder.clear();
257
258         builder.begin();
259         builder.addln("if (! isInActiveState())");
260         builder.begin();
261         builder.addln("_$fred$Default = $1;");
262         builder.addln("return;");
263         builder.end();
264
265         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"barney\");");
266
267         builder.addln("if (binding == null)");
268         builder
269                 .addln(" throw new org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'barney' is not bound and can not be updated.\");");
270
271         builder.addln("binding.setObject(($w) $1);");
272
273         builder.addln("if (isRendering())");
274         builder.begin();
275         builder.addln("_$fred = $1;");
276         builder.addln("_$fred$Cached = true;");
277         builder.end();
278         builder.end();
279
280         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class JavaDoc[]
281         { String JavaDoc.class }, null), builder.toString());
282
283         BodyBuilder expectedCleanup = new BodyBuilder();
284
285         expectedCleanup.addln("org.apache.tapestry.IBinding fredBinding = getBinding(\"barney\");");
286         expectedCleanup.addln("if (_$fred$Cached && ! fredBinding.isInvariant())");
287         expectedCleanup.begin();
288         expectedCleanup.addln("_$fred$Cached = false;");
289         expectedCleanup.addln("_$fred = _$fred$Default;");
290         expectedCleanup.end();
291
292         op.extendMethodImplementation(
293                 IComponent.class,
294                 EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
295                 expectedCleanup.toString());
296
297         replayControls();
298
299         ParameterPropertyWorker w = new ParameterPropertyWorker();
300
301         w.performEnhancement(op, spec);
302
303         verifyControls();
304     }
305
306     public void testPrimitiveType()
307     {
308         MockControl opc = newControl(EnhancementOperation.class);
309         EnhancementOperation op = (EnhancementOperation) opc.getMock();
310
311         BodyBuilder builder = new BodyBuilder();
312         builder.begin();
313         builder.addln("if (_$fred$Cached) return _$fred;");
314         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"barney\");");
315         builder.addln("if (binding == null) return _$fred$Default;");
316         builder.add("boolean result = ");
317         builder.addln(EnhanceUtils.class.getName() + ".toBoolean(binding);");
318         builder.addln("if (isRendering() || binding.isInvariant())");
319         builder.begin();
320         builder.addln("_$fred = result;");
321         builder.addln("_$fred$Cached = true;");
322         builder.end();
323         builder.addln("return result;");
324         builder.end();
325
326         op.getAccessorMethodName("fred");
327         opc.setReturnValue("isFred");
328
329         op.addMethod(
330                 Modifier.PUBLIC,
331                 new MethodSignature(boolean.class, "isFred", null, null),
332                 builder.toString());
333
334         replayControls();
335
336         new ParameterPropertyWorker().buildAccessor(
337                 op,
338                 "barney",
339                 "fred",
340                 boolean.class,
341                 "_$fred",
342                 "_$fred$Default",
343                 "_$fred$Cached",
344                 true);
345
346         verifyControls();
347     }
348
349     public void testParameterCacheDisabled()
350     {
351         MockControl opc = newControl(EnhancementOperation.class);
352         EnhancementOperation op = (EnhancementOperation) opc.getMock();
353
354         BodyBuilder builder = new BodyBuilder();
355         builder.begin();
356         builder.addln("if (_$fred$Cached) return _$fred;");
357         builder.addln("org.apache.tapestry.IBinding binding = getBinding(\"barney\");");
358         builder.addln("if (binding == null) return _$fred$Default;");
359         builder.add("boolean result = ");
360         builder.addln(EnhanceUtils.class.getName() + ".toBoolean(binding);");
361         builder.addln("if (binding.isInvariant())");
362         builder.begin();
363         builder.addln("_$fred = result;");
364         builder.addln("_$fred$Cached = true;");
365         builder.end();
366         builder.addln("return result;");
367         builder.end();
368
369         op.getAccessorMethodName("fred");
370         opc.setReturnValue("isFred");
371
372         op.addMethod(
373                 Modifier.PUBLIC,
374                 new MethodSignature(boolean.class, "isFred", null, null),
375                 builder.toString());
376
377         replayControls();
378
379         new ParameterPropertyWorker().buildAccessor(
380                 op,
381                 "barney",
382                 "fred",
383                 boolean.class,
384                 "_$fred",
385                 "_$fred$Default",
386                 "_$fred$Cached",
387                 false);
388
389         verifyControls();
390     }
391 }
Popular Tags