1 22 package org.jboss.test.aop.jdk15.annotated; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.textui.TestRunner; 27 import org.jboss.aop.Advised; 28 import org.jboss.aop.Advisor; 29 import org.jboss.aop.AspectManager; 30 import org.jboss.aop.InstanceAdvisor; 31 import org.jboss.aop.advice.AdviceBinding; 32 import org.jboss.aop.advice.AdviceFactory; 33 import org.jboss.aop.advice.AspectDefinition; 34 import org.jboss.test.aop.AOPTestWithSetup; 35 36 40 public class AnnotatedTestCase extends AOPTestWithSetup 41 { 42 43 public static void main(String [] args) 44 { 45 TestRunner.run(suite()); 46 } 47 48 public static Test suite() 49 { 50 TestSuite suite = new TestSuite("AnnotatedTester"); 51 suite.addTestSuite(AnnotatedTestCase.class); 52 return suite; 53 } 54 55 public AnnotatedTestCase(String name) 56 { 57 super(name); 58 } 59 60 protected void setUp() throws Exception 61 { 62 super.setUp(); 63 } 77 78 public void testBinding() throws Exception 79 { 80 System.out.println("***** testBinding() ****"); 81 AspectPerVM vm = null; 82 AspectPerClass perClass = null; 83 AspectPerClass perClass2 = null; 84 85 try 86 { 87 System.out.println("---- POJO ---"); 88 POJO pojo = new POJO(); 89 pojo.field++; 90 pojo.someMethod(); 91 System.out.println("---- POJO2 ---"); 92 POJO2 pojo2 = new POJO2(); 93 pojo2.field++; 94 pojo2.someMethod(); 95 96 System.out.println("-- get stats --"); 97 vm = (AspectPerVM) AspectManager.instance().getPerVMAspect("org.jboss.test.aop.jdk15.annotated.AspectPerVM"); 98 System.out.println("perVM stats: " + vm.constructorCalled + " " + vm.methodCalled + " " + vm.fieldRead + " " + vm.fieldWrite); 99 assertEquals(2, vm.constructorCalled); 100 assertEquals(2, vm.methodCalled); 101 assertEquals(2, vm.fieldRead); 102 assertEquals(2, vm.fieldWrite); 103 104 Advisor advisor = ((Advised) pojo)._getAdvisor(); 105 perClass = (AspectPerClass) advisor.getPerClassAspect("org.jboss.test.aop.jdk15.annotated.AspectPerClass"); 106 System.out.println("POJO perClass stats: " + perClass.constructorCalled + " " + perClass.methodCalled + " " + perClass.fieldRead + " " + perClass.fieldWrite); 107 assertEquals(1, perClass.constructorCalled); 108 assertEquals(1, perClass.methodCalled); 109 assertEquals(1, perClass.fieldRead); 110 assertEquals(1, perClass.fieldWrite); 111 112 advisor = ((Advised) pojo2)._getAdvisor(); 113 perClass2 = (AspectPerClass) advisor.getPerClassAspect("org.jboss.test.aop.jdk15.annotated.AspectPerClass"); 114 System.out.println("POJO2 perClass stats: " + perClass.constructorCalled + " " + perClass.methodCalled + " " + perClass.fieldRead + " " + perClass.fieldWrite); 115 assertEquals(1, perClass2.constructorCalled); 116 assertEquals(1, perClass2.methodCalled); 117 assertEquals(1, perClass2.fieldRead); 118 assertEquals(1, perClass2.fieldWrite); 119 120 InstanceAdvisor ia = ((Advised) pojo)._getInstanceAdvisor(); 121 AspectPerInstance perInstance = (AspectPerInstance) ia.getPerInstanceAspect("org.jboss.test.aop.jdk15.annotated.AspectPerInstance"); 122 System.out.println("pojo perInstance stats: " + perInstance.methodCalled + " " + perInstance.fieldRead + " " + perInstance.fieldWrite); 123 assertEquals(1, perInstance.methodCalled); 124 assertEquals(1, perInstance.fieldRead); 125 assertEquals(1, perInstance.fieldWrite); 126 127 ia = ((Advised) pojo2)._getInstanceAdvisor(); 128 perInstance = (AspectPerInstance) ia.getPerInstanceAspect("org.jboss.test.aop.jdk15.annotated.AspectPerInstance"); 129 System.out.println("pojo2 perInstance stats: " + perInstance.methodCalled + " " + perInstance.fieldRead + " " + perInstance.fieldWrite); 130 assertEquals(1, perInstance.methodCalled); 131 assertEquals(1, perInstance.fieldRead); 132 assertEquals(1, perInstance.fieldWrite); 133 } 134 finally 135 { 136 if (vm != null) vm.reset(); 137 if (perClass != null) perClass.reset(); 138 if (perClass2 != null) perClass2.reset(); 139 } 140 } 141 142 public void testCompostition() throws Exception 143 { 144 AspectPerVM vm = null; 145 try 146 { 147 System.out.println("***** testCompostition() ****"); 148 System.out.println("---- AnotherPOJO ---"); 149 AnotherPOJO apojo = new AnotherPOJO(); 150 apojo.field++; 151 apojo.someMethod(); 152 153 vm = (AspectPerVM) AspectManager.instance().getPerVMAspect("org.jboss.test.aop.jdk15.annotated.AspectPerVM"); 154 assertEquals(4, vm.anotherPOJOAccess); 155 } 156 finally 157 { 158 if (vm != null) vm.reset(); 159 } 160 } 161 162 public void testMixin() throws Exception 163 { 164 System.out.println("***** testMixin() ****"); 165 ExternalizableMixin.write = false; 166 ExternalizableMixin.read = false; 167 NoInterfacesPOJO pojo = new NoInterfacesPOJO(); 168 169 pojo.stuff = "hello world"; 170 java.rmi.MarshalledObject mo = new java.rmi.MarshalledObject (pojo); 171 pojo = (NoInterfacesPOJO)mo.get(); 172 System.out.println("deserialized pojo2.stuff2: " + pojo.stuff); 173 assertTrue("writeExternal was not called", ExternalizableMixin.write); 174 assertTrue("readExternal was not called", ExternalizableMixin.read); 175 176 ExternalizableMixin.write = false; 177 ExternalizableMixin.read = false; 178 NoInterfacesPOJO2 pojo2 = new NoInterfacesPOJO2(); 179 180 pojo2.stuff = "whatever"; 181 java.rmi.MarshalledObject mo2 = new java.rmi.MarshalledObject (pojo2); 182 pojo2 = (NoInterfacesPOJO2)mo2.get(); 183 System.out.println("deserialized pojo2.stuff2: " + pojo2.stuff); 184 assertTrue("writeExternal was not called for pojo2", ExternalizableMixin.write); 185 assertTrue("readExternal was not called for pojo2", ExternalizableMixin.read); 186 187 } 188 189 public void testIntroduction() throws Exception 190 { 191 System.out.println("***** testIntroduction() ****"); 192 NoInterfacesPOJO pojo = new NoInterfacesPOJO(); 193 194 try 195 { 196 EmptyInterface eif = (EmptyInterface)pojo; 197 } 198 catch(Exception e) 199 { 200 throw new RuntimeException ("pojo does not implement EmptyInterface"); 201 } 202 203 NoInterfacesPOJO2 pojo2 = new NoInterfacesPOJO2(); 204 205 206 try 207 { 208 EmptyInterface eif = (EmptyInterface)pojo2; 209 } 210 catch(Exception e) 211 { 212 throw new RuntimeException ("pojo2 does not implement EmptyInterface"); 213 } 214 215 } 216 217 public void testInterceptorDef()throws Exception 218 { 219 System.out.println("***** testInterceptorDef() ****"); 220 221 CountingInterceptor.count = 0; 222 VariaPOJO pojo = new VariaPOJO(); 223 pojo.methodWithInterceptor(); 224 System.out.println("Count: " + CountingInterceptor.count); 225 assertEquals("execution of POJO.methodWithInterceptor() was not intercepted", 1, CountingInterceptor.count); 226 227 CountingInterceptor.count = 0; 228 pojo.methodWithInterceptorFactory(); 229 System.out.println("Count: " + CountingInterceptor.count); 230 assertEquals("execution of POJO.methodWithInterceptorFactory() was not intercepted", 1, CountingInterceptor.count); 231 } 232 233 public void testTypedef()throws Exception 234 { 235 System.out.println("***** testTypedef() ****"); 236 237 VariaPOJO pojo = new VariaPOJO(); 238 pojo.methodWithTypedef(); 239 System.out.println("Intercepted: " + TypedefAspect.intercepted); 240 assertTrue("execution of POJO.methodWithTypedef() was not intercepted", TypedefAspect.intercepted); 241 } 242 243 public void testCFlow()throws Exception 244 { 245 System.out.println("***** testCFlow() ****"); 246 247 CFlowAspect.cflowAccess = 0; 248 249 VariaPOJO pojo = new VariaPOJO(); 250 pojo.cflowMethod1(); 251 assertEquals("Wrong number of interceptions 1) for cflow Advice", 1, CFlowAspect.cflowAccess); 252 253 CFlowAspect.cflowAccess = 0; 254 pojo.cflowMethod2(); 255 System.out.println("ints: " + CFlowAspect.cflowAccess); 256 assertEquals("Wrong number of interceptions 2) for cflow Advice", 1, CFlowAspect.cflowAccess ); 257 258 } 259 260 public void testPrepare()throws Exception 261 { 262 System.out.println("***** testPrepare() ****"); 263 PreparePOJO pojo = new PreparePOJO(); 264 pojo.someMethod(); 265 266 Advised advised = (Advised)pojo; 267 Advisor advisor = advised._getAdvisor(); 268 } 269 270 public void testPrepareAtClassLevel() throws Exception 271 { 272 System.out.println("***** testPrepareAtClassLevel() ****"); 273 PreparedPOJO pojo = new PreparedPOJO(); 274 pojo.someMethod(); 275 276 Advised advised = (Advised)pojo; 277 Advisor advisor = advised._getAdvisor(); 278 } 279 280 public void testDynamicCFlow()throws Exception 281 { 282 System.out.println("***** testDynamicCFlow() ****"); 283 284 CFlowAspect.cflowAccess = 0; 285 286 VariaPOJO pojo = new VariaPOJO(); 287 pojo.dynamicCFlowMethod(); 288 assertEquals("Wrong number of interceptions for dynamic cflow Advice", 0, CFlowAspect.cflowAccess); 289 290 SimpleDynamicCFlow.execute = true; 291 pojo.dynamicCFlowMethod(); 292 assertEquals("Wrong number of interceptions for dynamic cflow Advice", 1, CFlowAspect.cflowAccess); 293 294 SimpleDynamicCFlow.execute = false; 295 pojo.dynamicCFlowMethod(); 296 assertEquals("Wrong number of interceptions for dynamic cflow Advice (2)", 1, CFlowAspect.cflowAccess); 297 } 298 299 public void testAnnotationIntroduction() throws Exception 300 { 301 System.out.println("***** testAnnotationIntroduction() ****"); 302 IntroducedAnnotationPOJO pojo = new IntroducedAnnotationPOJO(); 303 assertNull("IntroducedAnnotationPOJO should not have had a constructor annotation", IntroducedAnnotationInterceptor.lastMyAnnotation); 304 305 pojo.annotationIntroductionMethod(); 306 MyAnnotation annotation = IntroducedAnnotationInterceptor.lastMyAnnotation; 307 assertNotNull("IntroducedAnnotationPOJO.annotationIntroductionMethod() should have had a method annotation", annotation); 308 assertEquals("Wrong value for MyAnnotation.string()", "hello", annotation.string()); 309 assertEquals("Wrong value for MyAnnotation.integer()", 5, annotation.integer()); 310 assertEquals("Wrong value for MyAnnotation.bool()", true, annotation.bool()); 311 pojo.noAnnotationIntroductionMethod(); 312 assertNull("IntroducedAnnotationPOJO.noAnnotationIntroductionMethod() should not have had a method annotation", IntroducedAnnotationInterceptor.lastMyAnnotation); 313 } 314 315 public void testPrecedence() throws Exception 316 { 317 System.out.println("***** testPrecedence() ****"); 318 VariaPOJO pojo = new VariaPOJO(); 319 320 pojo.precedenceMethod(); 321 String [] expected = {"PrecedenceInterceptor1", "PrecedenceAspect1.advice1", "PrecedenceAspect1.advice2", "PrecedenceInterceptor2"}; 322 java.util.ArrayList intercepted = Interceptions.intercepted; 323 assertEquals("Wrong number of interceptions", expected.length ,intercepted.size()); 324 325 for (int i = 0 ; i < expected.length ; i++) 326 { 327 assertEquals("Wrong interception at index " + i, expected[i], (String )intercepted.get(i)); 328 } 329 } 330 331 334 public void testAspectFactory() throws Exception 335 { 336 AdviceBinding binding = new AdviceBinding( 337 "execution(void *PreparedPOJO->someMethod(..))", null); 338 AspectDefinition aspectDefinition = AspectManager.instance() 339 .getAspectDefinition(AnnotatedAspectFactory.class.getName()); 340 assertNotNull(aspectDefinition); 341 binding.addInterceptorFactory(new AdviceFactory(aspectDefinition, 342 "advice")); 343 AspectManager.instance().addBinding(binding); 344 345 PreparedPOJO pojo = new PreparedPOJO(); 346 pojo.someMethod(); 347 assertTrue(AnnotatedAspectFactory.isAspectCreated()); 348 assertTrue(AnnotatedAspectFactory.getAspectCreated().isAdvised()); 349 AspectManager.instance().removeBinding(binding.getName()); 350 } 351 } | Popular Tags |