KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > AOPTester


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.bean;
23
24 import org.jboss.aop.Advised;
25 import org.jboss.aop.InstanceAdvisor;
26 import org.jboss.aop.metadata.ThreadMetaData;
27 import org.jboss.logging.Logger;
28 import org.jboss.system.ServiceMBeanSupport;
29
30 import javax.management.MBeanRegistration JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 /**
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 46372 $
38  */

39 public class AOPTester
40         extends ServiceMBeanSupport
41         implements AOPTesterMBean, MBeanRegistration JavaDoc
42 {
43    // Constants ----------------------------------------------------
44
// Attributes ---------------------------------------------------
45
static Logger log = Logger.getLogger(AOPTester.class);
46    MBeanServer JavaDoc m_mbeanServer;
47
48    // Static -------------------------------------------------------
49

50    // Constructors -------------------------------------------------
51
public AOPTester()
52    {
53    }
54
55    // Public -------------------------------------------------------
56

57    // MBeanRegistration implementation -----------------------------------
58
public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
59            throws Exception JavaDoc
60    {
61       m_mbeanServer = server;
62       return name;
63    }
64
65    public void postRegister(Boolean JavaDoc registrationDone)
66    {
67    }
68
69    public void preDeregister() throws Exception JavaDoc
70    {
71    }
72
73    public void postDeregister()
74    {
75    }
76
77    protected void startService()
78            throws Exception JavaDoc
79    {
80       // this is to load up the management console so that we can view everything
81
try
82       {
83          testBasic();
84          testInheritance();
85          testMetadata();
86          testDynamicInterceptors();
87          testFieldInterception();
88          testMethodInterception();
89          testConstructorInterception();
90          testExceptions();
91          testMixin();
92          testCallerPointcut();
93       }
94       catch (Exception JavaDoc ignored)
95       {
96       }
97
98    }
99
100    protected void stopService()
101    {
102    }
103
104    public void testBasic()
105    {
106       log.info("RUNNING TEST BASIC");
107       try
108       {
109          POJO pojo = new POJO();
110          if (!(pojo instanceof org.jboss.aop.Advised)) throw new RuntimeException JavaDoc("POJO is not instanceof Advised");
111          SimpleInterceptor.lastIntercepted = null;
112          SimpleInterceptor.lastTransAttributeAccessed = null;
113          pojo.someMethod();
114          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
115          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
116
117          InstanceOfInterceptor.intercepted = false;
118          Implements1 impl1 = new Implements1();
119          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) constructor interception");
120          InstanceOfInterceptor.intercepted = false;
121          impl1.foo = 1;
122          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) field interception");
123          InstanceOfInterceptor.intercepted = false;
124          impl1.someMethod();
125          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) method interception");
126
127          InstanceOfInterceptor.intercepted = false;
128          Implements2 impl2 = new Implements2();
129          if (InstanceOfInterceptor.intercepted == true) throw new RuntimeException JavaDoc("failed method only (instanceof) constructor interception");
130          InstanceOfInterceptor.intercepted = false;
131          impl2.someMethod();
132          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed method only(instanceof) method interception");
133          InstanceOfInterceptor.intercepted = false;
134
135          CFlowedPOJO cflow = new CFlowedPOJO();
136          InterceptorCounter.count = 0;
137          cflow.method3();
138          if (InterceptorCounter.count > 0) throw new RuntimeException JavaDoc("method3 count should be null");
139          InterceptorCounter.count = 0;
140          cflow.method1();
141          if (InterceptorCounter.count != 1) throw new RuntimeException JavaDoc("method1 count should be 1");
142          InterceptorCounter.count = 0;
143          cflow.recursive(1);
144          if (InterceptorCounter.count == 0) throw new RuntimeException JavaDoc("recursive never get intercepted");
145          if (InterceptorCounter.count > 1) throw new RuntimeException JavaDoc("recursive too many interceptions");
146       }
147       catch (Throwable JavaDoc ex)
148       {
149          log.error("failed", ex);
150          ex.printStackTrace();
151          throw new RuntimeException JavaDoc(ex.getMessage());
152       }
153    }
154
155    public void testInheritance()
156    {
157       log.info("RUNNING TEST INHERITANCE");
158       try
159       {
160          SimpleInterceptor.lastIntercepted = null;
161          SimpleInterceptor.lastTransAttributeAccessed = null;
162          POJOChild pojo = new POJOChild();
163          pojo.someMethod2();
164          if (!"someMethod2".equals(SimpleInterceptor.lastIntercepted))
165             throw new RuntimeException JavaDoc("Failed on interception test");
166          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
167             throw new RuntimeException JavaDoc("Failed on metadata test");
168
169          SimpleInterceptor.lastIntercepted = null;
170          SimpleInterceptor.lastTransAttributeAccessed = null;
171          pojo.someMethod();
172          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
173             throw new RuntimeException JavaDoc("Failed on interception test");
174          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
175             throw new RuntimeException JavaDoc("Failed on metadata test");
176
177       }
178       catch (Throwable JavaDoc ex)
179       {
180          log.error("failed", ex);
181          throw new RuntimeException JavaDoc(ex);
182       }
183    }
184
185    public void testMetadata()
186    {
187       log.info("RUNNING TEST METADATA");
188
189       try
190       {
191          POJOChild pojo = new POJOChild();
192          SimpleInterceptor.lastIntercepted = null;
193          SimpleInterceptor.lastTransAttributeAccessed = null;
194          pojo.someMethod();
195          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
196             throw new RuntimeException JavaDoc("Failed on interception test");
197          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
198             throw new RuntimeException JavaDoc("Failed on metadata test");
199
200          SimpleInterceptor.lastIntercepted = null;
201          SimpleInterceptor.lastTransAttributeAccessed = null;
202          pojo.anotherMethod();
203          if (!"anotherMethod".equals(SimpleInterceptor.lastIntercepted))
204             throw new RuntimeException JavaDoc("Failed on interception test");
205          if (!"Required".equals(SimpleInterceptor.lastTransAttributeAccessed))
206             throw new RuntimeException JavaDoc("Failed on metadata test");
207
208
209          SimpleInterceptor.lastIntercepted = null;
210          SimpleInterceptor.lastTransAttributeAccessed = null;
211          pojo.someMethod2();
212          if (!"someMethod2".equals(SimpleInterceptor.lastIntercepted))
213             throw new RuntimeException JavaDoc("Failed on interception test");
214          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
215             throw new RuntimeException JavaDoc("Failed on metadata test");
216
217
218          SimpleInterceptor.lastIntercepted = null;
219          SimpleInterceptor.lastTransAttributeAccessed = null;
220          pojo.someMethod3();
221          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
222             throw new RuntimeException JavaDoc("Failed on interception test");
223          if (!"Supports".equals(SimpleInterceptor.lastTransAttributeAccessed))
224             throw new RuntimeException JavaDoc("Failed on metadata test");
225
226          SimpleInterceptor.lastIntercepted = null;
227          SimpleInterceptor.lastTransAttributeAccessed = null;
228          org.jboss.aop.metadata.ThreadMetaData.instance().addMetaData("transaction", "trans-attribute", "Never");
229          pojo.someMethod3();
230          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
231             throw new RuntimeException JavaDoc("Failed on interception test");
232          if (!"Never".equals(SimpleInterceptor.lastTransAttributeAccessed))
233             throw new RuntimeException JavaDoc("Failed on metadata test");
234          org.jboss.aop.metadata.ThreadMetaData.instance().clear();
235
236          SimpleInterceptor.lastIntercepted = null;
237          SimpleInterceptor.lastTransAttributeAccessed = null;
238          InstanceAdvisor instanceAdvisor = ((Advised) pojo)._getInstanceAdvisor();
239          instanceAdvisor.getMetaData().addMetaData("transaction", "trans-attribute", "NotSupported");
240          pojo.someMethod3();
241          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
242             throw new RuntimeException JavaDoc("Failed on interception test");
243          if (!"NotSupported".equals(SimpleInterceptor.lastTransAttributeAccessed))
244             throw new RuntimeException JavaDoc("Failed on metadata test");
245          org.jboss.aop.metadata.ThreadMetaData.instance().clear();
246
247       }
248       catch (Throwable JavaDoc ex)
249       {
250          log.error("failed", ex);
251          throw new RuntimeException JavaDoc(ex);
252       }
253
254    }
255
256
257    public void testDynamicInterceptors()
258    {
259       log.info("RUNNING TEST DYNAMIC INTERCEPTORS");
260       try
261       {
262          POJOChild pojo = new POJOChild();
263          SimpleInterceptor.lastIntercepted = null;
264          SimpleInterceptor.lastTransAttributeAccessed = null;
265          BeforeInterceptor.lastIntercepted = null;
266          BeforeInterceptor.lastTransAttributeAccessed = null;
267          ((Advised) pojo)._getInstanceAdvisor().insertInterceptor(new BeforeInterceptor());
268          pojo.someMethod();
269          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
270             throw new RuntimeException JavaDoc("Failed on interception test");
271          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
272             throw new RuntimeException JavaDoc("Failed on metadata test");
273          if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
274             throw new RuntimeException JavaDoc("Failed on interception test");
275          if (!"RequiresNew".equals(BeforeInterceptor.lastTransAttributeAccessed))
276             throw new RuntimeException JavaDoc("Failed on metadata test");
277
278
279          SimpleInterceptor.lastIntercepted = null;
280          SimpleInterceptor.lastTransAttributeAccessed = null;
281          BeforeInterceptor.lastIntercepted = null;
282          BeforeInterceptor.lastTransAttributeAccessed = null;
283          AfterInterceptor.lastIntercepted = null;
284          AfterInterceptor.lastTransAttributeAccessed = null;
285          ((Advised) pojo)._getInstanceAdvisor().appendInterceptor(new AfterInterceptor());
286          pojo.someMethod();
287          if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
288             throw new RuntimeException JavaDoc("Failed on interception test");
289          if (!"RequiresNew".equals(BeforeInterceptor.lastTransAttributeAccessed))
290             throw new RuntimeException JavaDoc("Failed on metadata test");
291          if (!"someMethod".equals(AfterInterceptor.lastIntercepted))
292             throw new RuntimeException JavaDoc("Failed on interception test");
293          if (!"RequiresNew".equals(AfterInterceptor.lastTransAttributeAccessed))
294             throw new RuntimeException JavaDoc("Failed on metadata test");
295
296
297       }
298       catch (Throwable JavaDoc ex)
299       {
300          log.error("failed", ex);
301          throw new RuntimeException JavaDoc(ex);
302       }
303
304
305    }
306
307    public void testFieldInterception()
308    {
309       log.info("RUNNING TEST FIELD INTERCEPTION");
310       try
311       {
312
313
314          POJO pojo = new POJO();
315          SimpleInterceptor.lastFieldIntercepted = null;
316          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
317          pojo.accessField();
318
319          if (!"privateField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
320          if (!"NotSupported".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
321
322
323          POJOChild child = new POJOChild();
324          SimpleInterceptor.lastFieldIntercepted = null;
325          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
326          child.accessField();
327          if (!"privateField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
328          if (!"NotSupported".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
329
330          SimpleInterceptor.lastFieldIntercepted = null;
331          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
332          child.accessProtectedField();
333          if (!"protectedField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
334          if (!"Supports".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
335
336          POJORef ref = new POJORef();
337          SimpleInterceptor.lastFieldIntercepted = null;
338          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
339          ref.refPOJO();
340
341
342          if (!"protectedField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
343          if (!"Supports".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
344
345          pojo.accessStaticField();
346
347
348       }
349       catch (Throwable JavaDoc ex)
350       {
351          log.error("failed", ex);
352          throw new RuntimeException JavaDoc(ex);
353       }
354    }
355
356    public void testMethodInterception()
357    {
358       System.out.println("RUNNING METHOD INTERCEPTION");
359       try
360       {
361          POJO.staticMethod();
362          POJOConstructorTest vanilla;
363          vanilla = new POJOConstructorTest();
364
365          vanilla.data = "error";
366          vanilla.someMethod();
367          if (!vanilla.data.equals("someMethod")) throw new RuntimeException JavaDoc("someMethod() didn't get correct method metadata");
368
369          vanilla.data = "error";
370          vanilla.another();
371          if (!vanilla.data.equals("another()")) throw new RuntimeException JavaDoc("another() didn't get correct method metadata: " + vanilla.data);
372
373          vanilla.data = "nothing";
374          POJOMethodInterceptor.wasHit = false;
375          vanilla.another(1);
376          if (POJOMethodInterceptor.wasHit) throw new RuntimeException JavaDoc("interceptor should not have been called");
377          if (!vanilla.data.equals("nothing")) throw new RuntimeException JavaDoc("another(int) shouldn't get intercepted: " + vanilla.data);
378
379          vanilla.data = "nothing";
380          vanilla.another(1, 1);
381          if (!vanilla.data.equals("another(int, int)")) throw new RuntimeException JavaDoc("another(int, int) didn't get intercepted: " + vanilla.data);
382       }
383       catch (Throwable JavaDoc ex)
384       {
385          ex.printStackTrace();
386          throw new RuntimeException JavaDoc(ex.getMessage());
387       }
388    }
389
390    public void testAspect()
391    {
392       System.out.println("RUNNING ASPECT TEST");
393       try
394       {
395          POJO.staticMethod();
396          POJOAspectTester vanilla;
397          vanilla = new POJOAspectTester();
398          if (!vanilla.marker.equals("interceptConstructor")) throw new RuntimeException JavaDoc("vanilla constructor didn't get intercepted");
399
400          vanilla.marker = "error";
401          vanilla.someMethod();
402          if (!vanilla.marker.equals("interceptMethod")) throw new RuntimeException JavaDoc("vanilla.someMethod() didn't get intercepted");
403
404          vanilla.marker = "error";
405          vanilla.field = 5;
406          if (!vanilla.marker.equals("interceptField")) throw new RuntimeException JavaDoc("vanilla.field didn't get intercepted");
407
408       }
409       catch (Throwable JavaDoc ex)
410       {
411          ex.printStackTrace();
412          throw new RuntimeException JavaDoc(ex);
413       }
414    }
415
416    public void testConstructorInterception()
417    {
418       System.out.println("RUNNING CONSTRUCTOR INTERCEPTION");
419       try
420       {
421
422          POJO pojo = new POJO();
423          POJOChild child = new POJOChild();
424
425          POJORef ref = new POJORef();
426          ref.constructPOJO();
427
428          POJOWildCardConstructorTest wild;
429          wild = new POJOWildCardConstructorTest();
430          if (wild == null) throw new RuntimeException JavaDoc("wild was null!");
431          if (wild.data.equals("error")) throw new RuntimeException JavaDoc("wild() didn't intercept");
432          wild = new POJOWildCardConstructorTest(1);
433          if (wild.data.equals("error")) throw new RuntimeException JavaDoc("wild(int) didn't intercept");
434
435          POJOConstructorTest vanilla;
436          vanilla = new POJOConstructorTest();
437          if (vanilla == null) throw new RuntimeException JavaDoc("vanilla was null!");
438          if (vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla() didn't intercept");
439          if (!vanilla.data.equals("empty")) throw new RuntimeException JavaDoc("vanilla() didn't get correct constructor metadata");
440          vanilla = new POJOConstructorTest(1, 1);
441          if (vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla(int, int) didn't intercept");
442          if (!vanilla.data.equals("int, int")) throw new RuntimeException JavaDoc("vanilla(int, int) didn't get correct constructor metadata");
443          vanilla = new POJOConstructorTest(1);
444          if (!vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla(int) did intercept when it shouldn't have");
445
446       }
447       catch (Throwable JavaDoc ex)
448       {
449          ex.printStackTrace();
450          throw new RuntimeException JavaDoc(ex.getMessage());
451       }
452    }
453
454    public void testExceptions()
455    {
456       log.info("TEST AOP EXCEPTIONS");
457       try
458       {
459          NoInterceptorsPOJO pojo = new NoInterceptorsPOJO();
460
461          pojo.throwException();
462
463       }
464       catch (SomeException ignored)
465       {
466          log.info("caught SomeException successfully");
467       }
468       try
469       {
470          POJO pojo = new POJO();
471
472          pojo.throwException();
473       }
474       catch (SomeException ignored)
475       {
476          log.info("caught SomeException successfully");
477       }
478    }
479
480    public void testMixin()
481    {
482       try
483       {
484          log.info("TEST MIXIN");
485          POJO pojo = new POJO();
486          log.info("TEST Introduction");
487          Introduction intro = (Introduction) pojo;
488          log.info(intro.helloWorld("world"));
489          log.info("TEST Introduction2");
490          Introduction2 intro2 = (Introduction2) pojo;
491          log.info(intro2.goodbye("world"));
492          log.info("TEST InterfaceMixin");
493          InterfaceMixin mixin = (InterfaceMixin) pojo;
494          log.info(mixin.whazup());
495
496          POJOChild child = new POJOChild();
497          log.info("TEST child Introduction");
498          intro = (Introduction) child;
499          log.info(intro.helloWorld("world"));
500          log.info("TEST child Introduction2");
501          intro2 = (Introduction2) child;
502          log.info(intro2.goodbye("world"));
503          log.info("TEST child AnotherIntroduction");
504          SubclassIntroduction sub = (SubclassIntroduction) child;
505          log.info(sub.subclassHelloWorld("world"));
506          log.info("TEST metadata introduction pointcut");
507          NoInterceptorsPOJO nopojo = new NoInterceptorsPOJO();
508          intro = (Introduction) nopojo;
509
510       }
511       catch (Exception JavaDoc ex)
512       {
513          ex.printStackTrace();
514          throw new RuntimeException JavaDoc(ex);
515       }
516    }
517
518    public void testCallerPointcut()
519    {
520       log.info("TEST CALLER");
521       CallingPOJO callingPOJO = new CallingPOJO();
522       callingPOJO.callSomeMethod();
523       callingPOJO.nocallSomeMethod();
524       callingPOJO.callUnadvised();
525    }
526
527    public void testIntroducedAnnotation()
528    {
529       OverriddenAnnotationInterceptor.intercepted = false;
530       OverriddenAnnotationInterceptor.overriddenAnnotation = null;
531       
532       POJO pojo = new POJO();
533       pojo.overriddenAnnotatedMethod();
534       if (!OverriddenAnnotationInterceptor.intercepted) throw new RuntimeException JavaDoc("!IntroducedAnnotationInterceptor.intercepted");
535       if (OverriddenAnnotationInterceptor.overriddenAnnotation == null) throw new RuntimeException JavaDoc("IntroducedAnnotationInterceptor.overriddenAnnotation == null");
536    }
537    // Inner classes -------------------------------------------------
538
}
539
540
Popular Tags