001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.lang.reflect.Modifier;
018:
019: import org.apache.tapestry.MarkupWriter;
020: import org.apache.tapestry.annotations.SetupRender;
021: import org.apache.tapestry.model.MutableComponentModel;
022: import org.apache.tapestry.services.ClassTransformation;
023: import org.apache.tapestry.services.ComponentClassTransformWorker;
024: import org.apache.tapestry.services.MethodSignature;
025: import org.apache.tapestry.services.TransformConstants;
026: import org.apache.tapestry.test.TapestryTestCase;
027: import org.testng.annotations.Test;
028:
029: /**
030: * Of course, we're committing the cardinal sin of testing the code that's generated, rather than
031: * the *behavior* of the generated code. Fortunately, we back all this up with lots and lots of
032: * integration testing.
033: */
034: public class ComponentLifecycleMethodWorkerTest extends
035: TapestryTestCase {
036: @Test
037: public void no_methods_with_annotation() {
038: ClassTransformation tf = mockClassTransformation();
039: MutableComponentModel model = mockMutableComponentModel();
040:
041: MethodSignature sig = new MethodSignature("someRandomMethod");
042:
043: train_findMethods(tf, sig);
044:
045: train_getMethodAnnotation(tf, sig, SetupRender.class, null);
046:
047: replay();
048:
049: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
050: TransformConstants.SETUP_RENDER_SIGNATURE,
051: SetupRender.class, false);
052:
053: worker.transform(tf, model);
054:
055: verify();
056: }
057:
058: @Test
059: public void added_lifecycle_method_is_ignored() {
060: ClassTransformation tf = mockClassTransformation();
061: MutableComponentModel model = mockMutableComponentModel();
062:
063: train_findMethods(tf, TransformConstants.SETUP_RENDER_SIGNATURE);
064:
065: replay();
066:
067: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
068: TransformConstants.SETUP_RENDER_SIGNATURE,
069: SetupRender.class, false);
070:
071: worker.transform(tf, model);
072:
073: verify();
074: }
075:
076: @Test
077: public void void_method() {
078: ClassTransformation tf = mockClassTransformation();
079: MutableComponentModel model = mockMutableComponentModel();
080: SetupRender annotation = newSetupRender();
081:
082: MethodSignature sig = new MethodSignature("aMethod");
083:
084: train_findMethods(tf, sig);
085:
086: train_getMethodAnnotation(tf, sig, SetupRender.class,
087: annotation);
088:
089: train_isRootClass(model, false);
090:
091: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
092: "{ super.setupRender($$); if ($2.isAborted()) return; aMethod(); }");
093:
094: replay();
095:
096: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
097: TransformConstants.SETUP_RENDER_SIGNATURE,
098: SetupRender.class, false);
099:
100: worker.transform(tf, model);
101:
102: verify();
103: }
104:
105: @Test
106: public void match_on_method_name() {
107: ClassTransformation tf = mockClassTransformation();
108: MutableComponentModel model = mockMutableComponentModel();
109:
110: MethodSignature sig = new MethodSignature("setupRender");
111:
112: train_findMethods(tf, sig);
113:
114: train_isRootClass(model, false);
115:
116: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
117: "{ super.setupRender($$); if ($2.isAborted()) return; setupRender(); }");
118:
119: replay();
120:
121: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
122: TransformConstants.SETUP_RENDER_SIGNATURE,
123: SetupRender.class, false);
124:
125: worker.transform(tf, model);
126:
127: verify();
128: }
129:
130: protected final SetupRender newSetupRender() {
131: return newMock(SetupRender.class);
132: }
133:
134: @Test
135: public void multiple_methods_reverse_order() {
136: ClassTransformation tf = mockClassTransformation();
137: MutableComponentModel model = mockMutableComponentModel();
138: SetupRender annotation = newSetupRender();
139:
140: MethodSignature siga = new MethodSignature("aMethod");
141: MethodSignature sigb = new MethodSignature("bMethod");
142:
143: train_findMethods(tf, siga, sigb);
144:
145: train_getMethodAnnotation(tf, siga, SetupRender.class,
146: annotation);
147: train_getMethodAnnotation(tf, sigb, SetupRender.class,
148: annotation);
149:
150: train_isRootClass(model, false);
151:
152: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
153: "{ bMethod(); aMethod(); super.setupRender($$); }");
154:
155: replay();
156:
157: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
158: TransformConstants.SETUP_RENDER_SIGNATURE,
159: SetupRender.class, true);
160:
161: worker.transform(tf, model);
162:
163: verify();
164: }
165:
166: @Test
167: public void multiple_methods_parent_class_reverse_order() {
168: ClassTransformation tf = mockClassTransformation();
169: MutableComponentModel model = mockMutableComponentModel();
170: SetupRender annotation = newSetupRender();
171:
172: MethodSignature siga = new MethodSignature("aMethod");
173: MethodSignature sigb = new MethodSignature("bMethod");
174:
175: train_findMethods(tf, siga, sigb);
176:
177: train_getMethodAnnotation(tf, siga, SetupRender.class,
178: annotation);
179: train_getMethodAnnotation(tf, sigb, SetupRender.class,
180: annotation);
181:
182: train_isRootClass(model, true);
183:
184: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
185: "{ bMethod(); aMethod(); }");
186:
187: replay();
188:
189: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
190: TransformConstants.SETUP_RENDER_SIGNATURE,
191: SetupRender.class, true);
192:
193: worker.transform(tf, model);
194:
195: verify();
196:
197: }
198:
199: @Test
200: public void method_in_base_class() {
201: ClassTransformation tf = mockClassTransformation();
202: MutableComponentModel model = mockMutableComponentModel();
203: SetupRender annotation = newSetupRender();
204:
205: MethodSignature sig = new MethodSignature("aMethod");
206:
207: train_findMethods(tf, sig);
208:
209: train_getMethodAnnotation(tf, sig, SetupRender.class,
210: annotation);
211:
212: train_isRootClass(model, true);
213:
214: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
215: "{ aMethod(); }");
216:
217: replay();
218:
219: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
220: TransformConstants.SETUP_RENDER_SIGNATURE,
221: SetupRender.class, false);
222:
223: worker.transform(tf, model);
224:
225: verify();
226:
227: }
228:
229: @Test
230: public void method_with_markup_writer_parameter() {
231: ClassTransformation tf = mockClassTransformation();
232: MutableComponentModel model = mockMutableComponentModel();
233: SetupRender annotation = newSetupRender();
234:
235: MethodSignature sig = new MethodSignature(Modifier.PUBLIC,
236: "void", "aMethod", new String[] { MarkupWriter.class
237: .getName() }, null);
238:
239: train_findMethods(tf, sig);
240:
241: train_getMethodAnnotation(tf, sig, SetupRender.class,
242: annotation);
243:
244: train_isRootClass(model, false);
245:
246: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
247: "{ super.setupRender($$); if ($2.isAborted()) return; aMethod($1); }");
248:
249: replay();
250:
251: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
252: TransformConstants.SETUP_RENDER_SIGNATURE,
253: SetupRender.class, false);
254:
255: worker.transform(tf, model);
256:
257: verify();
258:
259: }
260:
261: @Test
262: public void nonvoid_method() {
263: ClassTransformation tf = mockClassTransformation();
264: MutableComponentModel model = mockMutableComponentModel();
265: SetupRender annotation = newSetupRender();
266:
267: MethodSignature sig = new MethodSignature(Modifier.PROTECTED,
268: "boolean", "aMethod", null, null);
269:
270: train_findMethods(tf, sig);
271:
272: train_getMethodAnnotation(tf, sig, SetupRender.class,
273: annotation);
274: train_getMethodIdentifier(tf, sig, "biff.Baz.aMethod()");
275:
276: train_isRootClass(model, false);
277:
278: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
279: "{", "super.setupRender($$);",
280: "if ($2.isAborted()) return; ",
281: "$2.setSource(this, \"biff.Baz.aMethod()\");",
282: "if ($2.storeResult(($w) aMethod())) return;", "}");
283:
284: replay();
285:
286: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
287: TransformConstants.SETUP_RENDER_SIGNATURE,
288: SetupRender.class, false);
289:
290: worker.transform(tf, model);
291:
292: verify();
293: }
294:
295: @Test
296: public void multiple_methods() {
297: ClassTransformation tf = mockClassTransformation();
298: MutableComponentModel model = mockMutableComponentModel();
299: SetupRender annotation = newSetupRender();
300:
301: MethodSignature siga = new MethodSignature(Modifier.PROTECTED,
302: "boolean", "aMethod", null, null);
303: MethodSignature sigb = new MethodSignature(Modifier.PUBLIC,
304: "void", "bMethod", new String[] { MarkupWriter.class
305: .getName() }, null);
306:
307: String ida = "aMethod()";
308:
309: train_findMethods(tf, siga, sigb);
310:
311: train_getMethodAnnotation(tf, siga, SetupRender.class,
312: annotation);
313: train_getMethodIdentifier(tf, siga, ida);
314:
315: train_getMethodAnnotation(tf, sigb, SetupRender.class,
316: annotation);
317:
318: train_isRootClass(model, false);
319:
320: train_addMethod(tf, TransformConstants.SETUP_RENDER_SIGNATURE,
321: "{ super.setupRender($$);",
322: "if ($2.isAborted()) return;",
323: "$2.setSource(this, \"aMethod()\");",
324: "if ($2.storeResult(($w) aMethod())) return;",
325: "bMethod($1); }");
326:
327: replay();
328:
329: ComponentClassTransformWorker worker = new ComponentLifecycleMethodWorker(
330: TransformConstants.SETUP_RENDER_SIGNATURE,
331: SetupRender.class, false);
332:
333: worker.transform(tf, model);
334:
335: verify();
336: }
337:
338: }
|