1 15 package org.apache.tapestry.annotations; 16 17 import java.lang.annotation.Annotation ; 18 import java.lang.reflect.Method ; 19 import java.util.Collections ; 20 import java.util.Map ; 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 36 37 public class TestAnnotationEnhancementWorker extends BaseAnnotationTestCase 38 { 39 protected EnhancementOperation newOp(Class 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 newMap(Class annotationClass, Object worker) 51 { 52 return Collections.singletonMap(annotationClass, worker); 53 } 54 55 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 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 baseClass, 104 Method m, Class 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 baseClass, 113 Class annotationClass) 114 { 115 Resource classResource = newResource(resolver, baseClass); 116 Annotation 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 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 t = new RuntimeException ("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 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 .class); 216 217 classWorker.performEnhancement(op, spec, DeprecatedBean.class, location); 218 219 replayControls(); 220 221 AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker(); 222 worker.setClassWorkers(newMap(Deprecated .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 t = new RuntimeException ("Simulated failure."); 243 244 AnnotationLocation location = newClassLocation( 245 resolver, 246 DeprecatedBean.class, 247 Deprecated .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 .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 |