KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > jdk15 > annotated > AnnotatedTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 /**
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 45977 $
39  */

40 public class AnnotatedTestCase extends AOPTestWithSetup
41 {
42
43    public static void main(String JavaDoc[] 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 JavaDoc name)
56    {
57       super(name);
58    }
59
60    protected void setUp() throws Exception JavaDoc
61    {
62       super.setUp();
63       //This should go in the startService() method of the injboss testsuite
64
// testBinding();
65
// testCompostition();
66
// testMixin();
67
// testIntroduction();
68
// testInterceptorDef();
69
// testTypedef();
70
// testCFlow();
71
// testPrepare();
72
// testPrepareAtClassLevel();
73
// testDynamicCFlow();
74
// testAnnotationIntroduction();
75
// testAspectFactory();
76
}
77    
78    public void testBinding() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc mo = new java.rmi.MarshalledObject JavaDoc(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 JavaDoc mo2 = new java.rmi.MarshalledObject JavaDoc(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 JavaDoc
190    {
191       System.out.println("***** testIntroduction() ****");
192       NoInterfacesPOJO pojo = new NoInterfacesPOJO();
193       
194       try
195       {
196          EmptyInterface eif = (EmptyInterface)pojo;
197       }
198       catch(Exception JavaDoc e)
199       {
200          throw new RuntimeException JavaDoc("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 JavaDoc e)
211       {
212          throw new RuntimeException JavaDoc("pojo2 does not implement EmptyInterface");
213       }
214       
215    }
216
217    public void testInterceptorDef()throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
316    {
317       System.out.println("***** testPrecedence() ****");
318       VariaPOJO pojo = new VariaPOJO();
319       
320       pojo.precedenceMethod();
321       String JavaDoc[] expected = {"PrecedenceInterceptor1", "PrecedenceAspect1.advice1", "PrecedenceAspect1.advice2", "PrecedenceInterceptor2"};
322       java.util.ArrayList JavaDoc 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 JavaDoc)intercepted.get(i));
328       }
329    }
330    
331    /**
332     * Tests the annotation of an aspect factory as @Aspect.
333     */

334    public void testAspectFactory() throws Exception JavaDoc
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