KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.ErrorLog;
25 import org.apache.hivemind.Location;
26 import org.apache.hivemind.service.BodyBuilder;
27 import org.apache.hivemind.service.MethodSignature;
28 import org.apache.hivemind.test.HiveMindTestCase;
29 import org.apache.tapestry.BaseComponent;
30 import org.apache.tapestry.IBinding;
31 import org.apache.tapestry.IComponent;
32 import org.apache.tapestry.binding.BindingSource;
33 import org.apache.tapestry.event.PageDetachListener;
34 import org.apache.tapestry.spec.IComponentSpecification;
35 import org.apache.tapestry.spec.IPropertySpecification;
36 import org.apache.tapestry.spec.PropertySpecification;
37 import org.easymock.MockControl;
38
39 /**
40  * Tests for {@link org.apache.tapestry.enhance.SpecifiedPropertyWorker}.
41  *
42  * @author Howard M. Lewis Ship
43  * @since 4.0
44  */

45 public class TestSpecifiedPropertyWorker extends HiveMindTestCase
46 {
47     private List JavaDoc buildPropertySpecs(String JavaDoc name, String JavaDoc type, boolean persistent)
48     {
49         return buildPropertySpecs(name, type, persistent, null, null);
50     }
51
52     private List JavaDoc buildPropertySpecs(String JavaDoc name, String JavaDoc type, boolean persistent,
53             Location location, String JavaDoc initialValue)
54     {
55         PropertySpecification ps = new PropertySpecification();
56         ps.setName(name);
57         ps.setType(type);
58         ps.setPersistence(persistent ? "session" : null);
59         ps.setInitialValue(initialValue);
60         ps.setLocation(location);
61
62         return Collections.singletonList(ps);
63     }
64
65     private IComponentSpecification buildComponentSpecification(List JavaDoc propertySpecs)
66     {
67         List JavaDoc names = new ArrayList JavaDoc();
68         Iterator JavaDoc i = propertySpecs.iterator();
69         while (i.hasNext())
70         {
71             IPropertySpecification ps = (IPropertySpecification) i.next();
72
73             names.add(ps.getName());
74         }
75
76         MockControl c = newControl(IComponentSpecification.class);
77         IComponentSpecification result = (IComponentSpecification) c.getMock();
78
79         result.getPropertySpecificationNames();
80         c.setReturnValue(names);
81
82         i = propertySpecs.iterator();
83         while (i.hasNext())
84         {
85             IPropertySpecification ps = (IPropertySpecification) i.next();
86
87             result.getPropertySpecification(ps.getName());
88             c.setReturnValue(ps);
89         }
90
91         return result;
92     }
93
94     private IComponentSpecification buildComponentSpecification(String JavaDoc name, String JavaDoc type,
95             boolean persistent)
96     {
97         return buildComponentSpecification(buildPropertySpecs(name, type, persistent));
98     }
99
100     public void testAddNormal() throws Exception JavaDoc
101     {
102         IComponentSpecification spec = buildComponentSpecification("fred", "boolean", false);
103
104         // Training
105

106         MockControl opc = newControl(EnhancementOperation.class);
107         EnhancementOperation op = (EnhancementOperation) opc.getMock();
108
109         op.convertTypeName("boolean");
110         opc.setReturnValue(boolean.class);
111
112         op.validateProperty("fred", boolean.class);
113         op.claimProperty("fred");
114         op.addField("_$fred", boolean.class);
115
116         op.getAccessorMethodName("fred");
117         opc.setReturnValue("isFred");
118
119         op.addMethod(
120                 Modifier.PUBLIC,
121                 new MethodSignature(boolean.class, "isFred", null, null),
122                 "return _$fred;");
123
124         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class JavaDoc[]
125         { boolean.class }, null), "{\n _$fred = $1;\n}\n");
126
127         op.addField("_$fred$default", boolean.class);
128
129         op.extendMethodImplementation(
130                 IComponent.class,
131                 EnhanceUtils.FINISH_LOAD_SIGNATURE,
132                 "_$fred$default = _$fred;");
133
134         op.extendMethodImplementation(
135                 PageDetachListener.class,
136                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
137                 "_$fred = _$fred$default;");
138
139         replayControls();
140
141         SpecifiedPropertyWorker w = new SpecifiedPropertyWorker();
142
143         w.performEnhancement(op, spec);
144
145         verifyControls();
146     }
147
148     public void testAddWithInitialValue() throws Exception JavaDoc
149     {
150         BindingSource bs = (BindingSource) newMock(BindingSource.class);
151         Location l = fabricateLocation(12);
152
153         IComponentSpecification spec = buildComponentSpecification(buildPropertySpecs(
154                 "fred",
155                 "java.util.List",
156                 false,
157                 l,
158                 "ognl:foo()"));
159
160         InitialValueBindingCreator expectedCreator = new InitialValueBindingCreator(bs,
161                 EnhanceMessages.initialValueForProperty("fred"), "ognl:foo()", l);
162
163         // Training
164

165         MockControl opc = newControl(EnhancementOperation.class);
166         EnhancementOperation op = (EnhancementOperation) opc.getMock();
167
168         op.convertTypeName("java.util.List");
169         opc.setReturnValue(List JavaDoc.class);
170
171         op.validateProperty("fred", List JavaDoc.class);
172         op.claimProperty("fred");
173         op.addField("_$fred", List JavaDoc.class);
174
175         op.getAccessorMethodName("fred");
176         opc.setReturnValue("getFred");
177
178         op.addMethod(
179                 Modifier.PUBLIC,
180                 new MethodSignature(List JavaDoc.class, "getFred", null, null),
181                 "return _$fred;");
182
183         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class JavaDoc[]
184         { List JavaDoc.class }, null), "{\n _$fred = $1;\n}\n");
185
186         op.addInjectedField(
187                 "_$fred$initialValueBindingCreator",
188                 InitialValueBindingCreator.class,
189                 expectedCreator);
190         opc.setReturnValue("_$fred$initialValueBindingCreator");
191         op.addField("_$fred$initialValueBinding", IBinding.class);
192         op
193                 .extendMethodImplementation(
194                         IComponent.class,
195                         EnhanceUtils.FINISH_LOAD_SIGNATURE,
196                         "_$fred$initialValueBinding = _$fred$initialValueBindingCreator.createBinding(this);\n");
197
198         op.getClassReference(List JavaDoc.class);
199         opc.setReturnValue("_$class$java$util$List");
200
201         String JavaDoc code = "_$fred = (java.util.List) _$fred$initialValueBinding.getObject(_$class$java$util$List);\n";
202
203         op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
204         op.extendMethodImplementation(
205                 PageDetachListener.class,
206                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
207                 code);
208
209         replayControls();
210
211         SpecifiedPropertyWorker w = new SpecifiedPropertyWorker();
212         w.setBindingSource(bs);
213
214         w.performEnhancement(op, spec);
215
216         verifyControls();
217     }
218
219     public void testAddPersistent() throws Exception JavaDoc
220     {
221         IComponentSpecification spec = buildComponentSpecification(
222                 "barney",
223                 "java.lang.String",
224                 true);
225
226         // Training
227

228         MockControl opc = newControl(EnhancementOperation.class);
229         EnhancementOperation op = (EnhancementOperation) opc.getMock();
230
231         op.convertTypeName("java.lang.String");
232         opc.setReturnValue(String JavaDoc.class);
233
234         op.validateProperty("barney", String JavaDoc.class);
235         op.claimProperty("barney");
236         op.addField("_$barney", String JavaDoc.class);
237
238         op.getAccessorMethodName("barney");
239         opc.setReturnValue("getBarney");
240
241         op.addMethod(
242                 Modifier.PUBLIC,
243                 new MethodSignature(String JavaDoc.class, "getBarney", null, null),
244                 "return _$barney;");
245
246         BodyBuilder b = new BodyBuilder();
247         b.begin();
248         b.addln("org.apache.tapestry.Tapestry#fireObservedChange(this, \"barney\", ($w) $1);");
249         b.addln("_$barney = $1;");
250         b.end();
251
252         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setBarney", new Class JavaDoc[]
253         { String JavaDoc.class }, null), b.toString());
254
255         op.addField("_$barney$default", String JavaDoc.class);
256
257         op.extendMethodImplementation(
258                 IComponent.class,
259                 EnhanceUtils.FINISH_LOAD_SIGNATURE,
260                 "_$barney$default = _$barney;");
261
262         op.extendMethodImplementation(
263                 PageDetachListener.class,
264                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
265                 "_$barney = _$barney$default;");
266
267         replayControls();
268
269         SpecifiedPropertyWorker w = new SpecifiedPropertyWorker();
270
271         w.performEnhancement(op, spec);
272
273         verifyControls();
274     }
275
276     public void testFailure() throws Exception JavaDoc
277     {
278         Location l = fabricateLocation(207);
279         // Should be "java.lang.Long"
280
List JavaDoc propertySpecs = buildPropertySpecs("wilma", "Long", false, l, null);
281         IComponentSpecification spec = buildComponentSpecification(propertySpecs);
282
283         MockControl opc = newControl(EnhancementOperation.class);
284         EnhancementOperation op = (EnhancementOperation) opc.getMock();
285
286         op.convertTypeName("Long");
287         Throwable JavaDoc ex = new ApplicationRuntimeException("Simulated error.");
288         opc.setThrowable(ex);
289
290         op.getBaseClass();
291         opc.setReturnValue(BaseComponent.class);
292
293         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
294
295         log
296                 .error(
297                         "Error adding property wilma to class org.apache.tapestry.BaseComponent: Simulated error.",
298                         l,
299                         ex);
300
301         replayControls();
302
303         SpecifiedPropertyWorker w = new SpecifiedPropertyWorker();
304         w.setErrorLog(log);
305
306         w.performEnhancement(op, spec);
307
308         verifyControls();
309     }
310
311 }
Popular Tags