KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > 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.annotated;
23
24 import java.util.ArrayList JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import junit.textui.TestRunner;
29
30 import org.jboss.aop.Advised;
31 import org.jboss.aop.Advisor;
32 import org.jboss.aop.AspectManager;
33 import org.jboss.aop.InstanceAdvisor;
34 import org.jboss.aop.advice.AdviceBinding;
35 import org.jboss.aop.advice.AdviceFactory;
36 import org.jboss.aop.advice.AspectDefinition;
37 import org.jboss.test.aop.AOPTestWithSetup;
38
39 /**
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 45977 $
42  */

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

339    public void testAspectFactory() throws Exception JavaDoc
340    {
341       AdviceBinding binding = new AdviceBinding(
342             "execution(void *PreparedPOJO->someMethod(..))", null);
343       AspectDefinition aspectDefinition = AspectManager.instance()
344             .getAspectDefinition(AnnotatedAspectFactory.class.getName());
345       assertNotNull(aspectDefinition);
346       binding.addInterceptorFactory(new AdviceFactory(aspectDefinition,
347             "advice"));
348       AspectManager.instance().addBinding(binding);
349
350       PreparedPOJO pojo = new PreparedPOJO();
351       pojo.someMethod();
352       assertTrue(AnnotatedAspectFactory.isAspectCreated());
353       assertTrue(AnnotatedAspectFactory.getAspectCreated().isAdvised());
354       AspectManager.instance().removeBinding(binding.getName());
355    }
356 }
Popular Tags