KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > annotations > TestAnnotationEnhancementWorker


1 // Copyright 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.annotations;
16
17 import java.lang.annotation.Annotation JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.ClassResolver;
23 import org.apache.hivemind.ErrorLog;
24 import org.apache.hivemind.Resource;
25 import org.apache.hivemind.impl.DefaultClassResolver;
26 import org.apache.tapestry.enhance.EnhancementOperation;
27 import org.apache.tapestry.spec.IComponentSpecification;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.annotations.AnnotationEnhancementWorker}.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36
37 public class TestAnnotationEnhancementWorker extends BaseAnnotationTestCase
38 {
39     protected EnhancementOperation newOp(Class JavaDoc baseClass)
40     {
41         MockControl control = newControl(EnhancementOperation.class);
42         EnhancementOperation op = (EnhancementOperation) control.getMock();
43
44         op.getBaseClass();
45         control.setReturnValue(baseClass);
46
47         return op;
48     }
49
50     protected Map JavaDoc newMap(Class JavaDoc annotationClass, Object JavaDoc worker)
51     {
52         return Collections.singletonMap(annotationClass, worker);
53     }
54
55     /**
56      * No method annotations registered.
57      */

58     public void testNoAnnotations()
59     {
60         EnhancementOperation op = newOp(AnnotatedPage.class);
61         IComponentSpecification spec = newSpec();
62
63         replayControls();
64
65         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
66         worker.setMethodWorkers(Collections.EMPTY_MAP);
67
68         worker.performEnhancement(op, spec);
69
70         verifyControls();
71     }
72
73     public void testAnnotationMatch()
74     {
75         ClassResolver resolver = new DefaultClassResolver();
76
77         EnhancementOperation op = newOp(AnnotatedPage.class);
78         IComponentSpecification spec = newSpec();
79
80         MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) newMock(MethodAnnotationEnhancementWorker.class);
81
82         Method JavaDoc m = findMethod(AnnotatedPage.class, "getInjectedObject");
83
84         AnnotationLocation location = newMethodLocation(
85                 resolver,
86                 AnnotatedPage.class,
87                 m,
88                 InjectObject.class);
89
90         methodWorker.performEnhancement(op, spec, m, location);
91
92         replayControls();
93
94         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
95         worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
96         worker.setClassResolver(resolver);
97
98         worker.performEnhancement(op, spec);
99
100         verifyControls();
101     }
102
103     protected AnnotationLocation newMethodLocation(ClassResolver resolver, Class JavaDoc baseClass,
104             Method JavaDoc m, Class JavaDoc annotationClass)
105     {
106         Resource classResource = newResource(resolver, baseClass);
107
108         return new AnnotationLocation(classResource, AnnotationMessages.methodAnnotation(m
109                 .getAnnotation(annotationClass), m));
110     }
111
112     private AnnotationLocation newClassLocation(ClassResolver resolver, Class JavaDoc baseClass,
113             Class JavaDoc annotationClass)
114     {
115         Resource classResource = newResource(resolver, baseClass);
116         Annotation JavaDoc annotation = baseClass.getAnnotation(annotationClass);
117
118         return new AnnotationLocation(classResource, AnnotationMessages.classAnnotation(
119                 annotation,
120                 baseClass));
121     }
122
123     public void testAnnotationWithSubclass()
124     {
125         ClassResolver resolver = new DefaultClassResolver();
126
127         EnhancementOperation op = newOp(AnnotatedPageSubclass.class);
128         IComponentSpecification spec = newSpec();
129
130         MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) newMock(MethodAnnotationEnhancementWorker.class);
131
132         Method JavaDoc m = findMethod(AnnotatedPageSubclass.class, "getInjectedObject");
133
134         AnnotationLocation location = newMethodLocation(
135                 resolver,
136                 AnnotatedPageSubclass.class,
137                 m,
138                 InjectObject.class);
139
140         methodWorker.performEnhancement(op, spec, m, location);
141
142         replayControls();
143
144         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
145         worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
146         worker.setClassResolver(resolver);
147
148         worker.performEnhancement(op, spec);
149
150         verifyControls();
151     }
152
153     public void testAnnotationFailure()
154     {
155         ClassResolver resolver = new DefaultClassResolver();
156
157         ErrorLog log = newLog();
158         Throwable JavaDoc t = new RuntimeException JavaDoc("Woops!");
159
160         EnhancementOperation op = newOp(AnnotatedPage.class);
161         IComponentSpecification spec = newSpec();
162
163         MockControl methodWorkerc = newControl(MethodAnnotationEnhancementWorker.class);
164         MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) methodWorkerc
165                 .getMock();
166
167         Method JavaDoc m = findMethod(AnnotatedPage.class, "getInjectedObject");
168
169         AnnotationLocation location = newMethodLocation(
170                 resolver,
171                 AnnotatedPage.class,
172                 m,
173                 InjectObject.class);
174
175         methodWorker.performEnhancement(op, spec, m, location);
176         methodWorkerc.setThrowable(t);
177
178         log
179                 .error(
180                         "An error occured processing annotation "
181                                 + "@org.apache.tapestry.annotations.InjectObject(value=barney) of "
182                                 + "public abstract java.lang.Object org.apache.tapestry.annotations.AnnotatedPage.getInjectedObject(): Woops!",
183                         null,
184                         t);
185
186         replayControls();
187
188         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
189         worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
190         worker.setErrorLog(log);
191         worker.setClassResolver(resolver);
192
193         worker.performEnhancement(op, spec);
194
195         verifyControls();
196     }
197
198     private ErrorLog newLog()
199     {
200         return (ErrorLog) newMock(ErrorLog.class);
201     }
202
203     public void testClassAnnotation()
204     {
205         ClassResolver resolver = new DefaultClassResolver();
206
207         EnhancementOperation op = newOp(DeprecatedBean.class);
208         IComponentSpecification spec = newSpec();
209
210         ClassAnnotationEnhancementWorker classWorker = (ClassAnnotationEnhancementWorker) newMock(ClassAnnotationEnhancementWorker.class);
211
212         AnnotationLocation location = newClassLocation(
213                 resolver,
214                 DeprecatedBean.class,
215                 Deprecated JavaDoc.class);
216
217         classWorker.performEnhancement(op, spec, DeprecatedBean.class, location);
218
219         replayControls();
220
221         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
222         worker.setClassWorkers(newMap(Deprecated JavaDoc.class, classWorker));
223         worker.setClassResolver(resolver);
224
225         worker.performEnhancement(op, spec);
226
227         verifyControls();
228     }
229
230     public void testClassAnnotationFailure()
231     {
232         ClassResolver resolver = new DefaultClassResolver();
233
234         ErrorLog log = newLog();
235         EnhancementOperation op = newOp(DeprecatedBean.class);
236         IComponentSpecification spec = newSpec();
237
238         MockControl classWorkerc = newControl(ClassAnnotationEnhancementWorker.class);
239         ClassAnnotationEnhancementWorker classWorker = (ClassAnnotationEnhancementWorker) classWorkerc
240                 .getMock();
241
242         Throwable JavaDoc t = new RuntimeException JavaDoc("Simulated failure.");
243
244         AnnotationLocation location = newClassLocation(
245                 resolver,
246                 DeprecatedBean.class,
247                 Deprecated JavaDoc.class);
248
249         classWorker.performEnhancement(op, spec, DeprecatedBean.class, location);
250         classWorkerc.setThrowable(t);
251
252         log
253                 .error(
254                         "An error occured processing annotation @java.lang.Deprecated() of "
255                                 + "class org.apache.tapestry.annotations.DeprecatedBean: Simulated failure.",
256                         null,
257                         t);
258
259         replayControls();
260
261         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
262         worker.setClassWorkers(newMap(Deprecated JavaDoc.class, classWorker));
263         worker.setErrorLog(log);
264         worker.setClassResolver(resolver);
265
266         worker.performEnhancement(op, spec);
267
268         verifyControls();
269     }
270
271     public void testClassAnnotationNoMatch()
272     {
273         EnhancementOperation op = newOp(DeprecatedBean.class);
274         IComponentSpecification spec = newSpec();
275
276         replayControls();
277
278         AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
279         worker.setClassWorkers(Collections.EMPTY_MAP);
280
281         worker.performEnhancement(op, spec);
282
283         verifyControls();
284     }
285 }
286
Popular Tags