KickJava   Java API By Example, From Geeks To Geeks.

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

40 public class AOPTester extends AOPTestWithSetup
41 {
42    public static void main(String JavaDoc[] args)
43    {
44       TestRunner.run(suite());
45    }
46
47    public static Test suite()
48    {
49       TestSuite suite = new TestSuite("AOPTester");
50       suite.addTestSuite(AOPTester.class);
51       return suite;
52    }
53    // Constants ----------------------------------------------------
54
// Attributes ---------------------------------------------------
55

56    // Static -------------------------------------------------------
57

58    // Constructors -------------------------------------------------
59
public AOPTester(String JavaDoc name)
60    {
61       super(name);
62    }
63
64    // Public -------------------------------------------------------
65

66    public void testBasic()
67    {
68       System.out.println("RUNNING TEST BASIC");
69       try
70       {
71          POJO pojo = new POJO();
72          SimpleInterceptor.lastIntercepted = null;
73          SimpleInterceptor.lastTransAttributeAccessed = null;
74          pojo.someMethod();
75          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
76          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
77
78          InstanceOfInterceptor.intercepted = false;
79          Implements1 impl1 = new Implements1();
80          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) constructor interception");
81          InstanceOfInterceptor.intercepted = false;
82          impl1.foo = 1;
83          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) field interception");
84          InstanceOfInterceptor.intercepted = false;
85          impl1.someMethod();
86          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed all(instanceof) method interception");
87
88          InstanceOfInterceptor.intercepted = false;
89          Implements2 impl2 = new Implements2();
90          if (InstanceOfInterceptor.intercepted == true) throw new RuntimeException JavaDoc("failed method only (instanceof) constructor interception");
91          InstanceOfInterceptor.intercepted = false;
92          impl2.someMethod();
93          if (InstanceOfInterceptor.intercepted == false) throw new RuntimeException JavaDoc("failed method only(instanceof) method interception");
94          InstanceOfInterceptor.intercepted = false;
95
96          CFlowedPOJO cflow = new CFlowedPOJO();
97          InterceptorCounter.count = 0;
98          cflow.method3();
99          if (InterceptorCounter.count > 0) throw new RuntimeException JavaDoc("method3 count should be null");
100          InterceptorCounter.count = 0;
101          cflow.method1();
102          if (InterceptorCounter.count != 1) throw new RuntimeException JavaDoc("method1 count should be 1");
103          InterceptorCounter.count = 0;
104          cflow.recursive(1);
105          if (InterceptorCounter.count == 0) throw new RuntimeException JavaDoc("recursive never get intercepted");
106          if (InterceptorCounter.count > 1) throw new RuntimeException JavaDoc("recursive too many interceptions");
107       }
108       catch (Throwable JavaDoc ex)
109       {
110          ex.printStackTrace();
111          throw new RuntimeException JavaDoc(ex);
112       }
113    }
114
115    public void testInheritance()
116    {
117       System.out.println("RUNNING TEST INHERITANCE");
118       try
119       {
120          SimpleInterceptor.lastIntercepted = null;
121          SimpleInterceptor.lastTransAttributeAccessed = null;
122          POJOChild pojo = new POJOChild();
123          pojo.someMethod2();
124          if (!"someMethod2".equals(SimpleInterceptor.lastIntercepted))
125             throw new RuntimeException JavaDoc("Failed on interception test");
126          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
127             throw new RuntimeException JavaDoc("Failed on metadata test");
128
129          SimpleInterceptor.lastIntercepted = null;
130          SimpleInterceptor.lastTransAttributeAccessed = null;
131          pojo.someMethod();
132          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
133             throw new RuntimeException JavaDoc("Failed on interception test");
134          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
135             throw new RuntimeException JavaDoc("Failed on metadata test");
136
137       }
138       catch (Throwable JavaDoc ex)
139       {
140          throw new RuntimeException JavaDoc(ex);
141       }
142    }
143
144    public void testMetadata()
145    {
146       System.out.println("RUNNING TEST METADATA");
147
148       try
149       {
150          POJOChild pojo = new POJOChild();
151          SimpleInterceptor.lastIntercepted = null;
152          SimpleInterceptor.lastTransAttributeAccessed = null;
153          pojo.someMethod();
154          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
155             throw new RuntimeException JavaDoc("Failed on interception test");
156          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
157             throw new RuntimeException JavaDoc("Failed on metadata test");
158
159          SimpleInterceptor.lastIntercepted = null;
160          SimpleInterceptor.lastTransAttributeAccessed = null;
161          pojo.anotherMethod();
162          if (!"anotherMethod".equals(SimpleInterceptor.lastIntercepted))
163             throw new RuntimeException JavaDoc("Failed on interception test");
164          if (!"Required".equals(SimpleInterceptor.lastTransAttributeAccessed))
165             throw new RuntimeException JavaDoc("Failed on metadata test");
166
167
168          SimpleInterceptor.lastIntercepted = null;
169          SimpleInterceptor.lastTransAttributeAccessed = null;
170          pojo.someMethod2();
171          if (!"someMethod2".equals(SimpleInterceptor.lastIntercepted))
172             throw new RuntimeException JavaDoc("Failed on interception test");
173          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
174             throw new RuntimeException JavaDoc("Failed on metadata test");
175
176
177          SimpleInterceptor.lastIntercepted = null;
178          SimpleInterceptor.lastTransAttributeAccessed = null;
179          pojo.someMethod3();
180          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
181             throw new RuntimeException JavaDoc("Failed on interception test");
182          if (!"Supports".equals(SimpleInterceptor.lastTransAttributeAccessed))
183             throw new RuntimeException JavaDoc("Failed on metadata test");
184
185          SimpleInterceptor.lastIntercepted = null;
186          SimpleInterceptor.lastTransAttributeAccessed = null;
187          org.jboss.aop.metadata.ThreadMetaData.instance().addMetaData("transaction", "trans-attribute", "Never");
188          pojo.someMethod3();
189          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
190             throw new RuntimeException JavaDoc("Failed on interception test");
191          if (!"Never".equals(SimpleInterceptor.lastTransAttributeAccessed))
192             throw new RuntimeException JavaDoc("Failed on metadata test");
193          org.jboss.aop.metadata.ThreadMetaData.instance().clear();
194
195          SimpleInterceptor.lastIntercepted = null;
196          SimpleInterceptor.lastTransAttributeAccessed = null;
197          InstanceAdvisor instanceAdvisor = ((Advised) pojo)._getInstanceAdvisor();
198          instanceAdvisor.getMetaData().addMetaData("transaction", "trans-attribute", "NotSupported");
199          pojo.someMethod3();
200          if (!"someMethod3".equals(SimpleInterceptor.lastIntercepted))
201             throw new RuntimeException JavaDoc("Failed on interception test");
202          if (!"NotSupported".equals(SimpleInterceptor.lastTransAttributeAccessed))
203             throw new RuntimeException JavaDoc("Failed on metadata test");
204          org.jboss.aop.metadata.ThreadMetaData.instance().clear();
205
206       }
207       catch (Throwable JavaDoc ex)
208       {
209          throw new RuntimeException JavaDoc(ex);
210       }
211
212    }
213
214
215    public void testDynamicInterceptors()
216    {
217       System.out.println("RUNNING TEST DYNAMIC INTERCEPTORS");
218       try
219       {
220          POJOChild pojo = new POJOChild();
221          SimpleInterceptor.lastIntercepted = null;
222          SimpleInterceptor.lastTransAttributeAccessed = null;
223          BeforeInterceptor.lastIntercepted = null;
224          BeforeInterceptor.lastTransAttributeAccessed = null;
225          ((Advised) pojo)._getInstanceAdvisor().insertInterceptor(new BeforeInterceptor());
226          pojo.someMethod();
227          if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
228             throw new RuntimeException JavaDoc("Failed on interception test");
229          if (!"RequiresNew".equals(SimpleInterceptor.lastTransAttributeAccessed))
230             throw new RuntimeException JavaDoc("Failed on metadata test");
231          if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
232             throw new RuntimeException JavaDoc("Failed on interception test");
233          if (!"RequiresNew".equals(BeforeInterceptor.lastTransAttributeAccessed))
234             throw new RuntimeException JavaDoc("Failed on metadata test");
235
236
237          SimpleInterceptor.lastIntercepted = null;
238          SimpleInterceptor.lastTransAttributeAccessed = null;
239          BeforeInterceptor.lastIntercepted = null;
240          BeforeInterceptor.lastTransAttributeAccessed = null;
241          AfterInterceptor.lastIntercepted = null;
242          AfterInterceptor.lastTransAttributeAccessed = null;
243          ((Advised) pojo)._getInstanceAdvisor().appendInterceptor(new AfterInterceptor());
244          pojo.someMethod();
245          if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
246             throw new RuntimeException JavaDoc("Failed on interception test");
247          if (!"RequiresNew".equals(BeforeInterceptor.lastTransAttributeAccessed))
248             throw new RuntimeException JavaDoc("Failed on metadata test");
249          if (!"someMethod".equals(AfterInterceptor.lastIntercepted))
250             throw new RuntimeException JavaDoc("Failed on interception test");
251          if (!"RequiresNew".equals(AfterInterceptor.lastTransAttributeAccessed))
252             throw new RuntimeException JavaDoc("Failed on metadata test");
253
254
255       }
256       catch (Throwable JavaDoc ex)
257       {
258          throw new RuntimeException JavaDoc(ex);
259       }
260
261
262    }
263
264    public void testFieldInterception()
265    {
266       System.out.println("RUNNING TEST FIELD INTERCEPTION");
267       try
268       {
269
270
271          POJO pojo = new POJO();
272          SimpleInterceptor.lastFieldIntercepted = null;
273          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
274          pojo.accessField();
275
276          if (!"privateField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
277          if (!"NotSupported".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
278
279
280          POJOChild child = new POJOChild();
281          SimpleInterceptor.lastFieldIntercepted = null;
282          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
283          child.accessField();
284          if (!"privateField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
285          if (!"NotSupported".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
286
287          SimpleInterceptor.lastFieldIntercepted = null;
288          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
289          child.accessProtectedField();
290          if (!"protectedField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
291          if (!"Supports".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
292
293          POJORef ref = new POJORef();
294          SimpleInterceptor.lastFieldIntercepted = null;
295          SimpleInterceptor.lastFieldTransAttributeAccessed = null;
296          ref.refPOJO();
297
298
299          if (!"protectedField".equals(SimpleInterceptor.lastFieldIntercepted)) throw new RuntimeException JavaDoc("Failed on interception test");
300          if (!"Supports".equals(SimpleInterceptor.lastFieldTransAttributeAccessed)) throw new RuntimeException JavaDoc("Failed on metadata test");
301
302          pojo.accessStaticField();
303
304
305       }
306       catch (Throwable JavaDoc ex)
307       {
308          throw new RuntimeException JavaDoc(ex);
309       }
310    }
311
312    public void testMethodInterception()
313    {
314       System.out.println("RUNNING METHOD INTERCEPTION");
315       try
316       {
317          PrivatePOJO priv = new PrivatePOJO();
318          SimpleInterceptor.lastIntercepted = null;
319          priv.callPrivate();
320          if (SimpleInterceptor.lastIntercepted == null) throw new RuntimeException JavaDoc("unable to intercept private method");
321          
322          POJO.staticMethod();
323          POJOConstructorTest vanilla;
324          vanilla = new POJOConstructorTest();
325
326          vanilla.data = "error";
327          vanilla.someMethod();
328          if (!vanilla.data.equals("someMethod")) throw new RuntimeException JavaDoc("someMethod() didn't get correct method metadata");
329
330          vanilla.data = "error";
331          vanilla.another();
332          if (!vanilla.data.equals("another()")) throw new RuntimeException JavaDoc("another() didn't get correct method metadata: " + vanilla.data);
333
334          vanilla.data = "nothing";
335          POJOMethodInterceptor.wasHit = false;
336          vanilla.another(1);
337          if (POJOMethodInterceptor.wasHit) throw new RuntimeException JavaDoc("interceptor should not have been called");
338          if (!vanilla.data.equals("nothing")) throw new RuntimeException JavaDoc("another(int) shouldn't get intercepted: " + vanilla.data);
339
340          vanilla.data = "nothing";
341          vanilla.another(1, 1);
342          if (!vanilla.data.equals("another(int, int)")) throw new RuntimeException JavaDoc("another(int, int) didn't get intercepted: " + vanilla.data);
343       }
344       catch (Throwable JavaDoc ex)
345       {
346          ex.printStackTrace();
347          throw new RuntimeException JavaDoc(ex.getMessage());
348       }
349    }
350
351    public void testAspect()
352    {
353       System.out.println("RUNNING ASPECT TEST");
354       try
355       {
356          POJO.staticMethod();
357          POJOAspectTester vanilla;
358          vanilla = new POJOAspectTester();
359          if (!vanilla.marker.equals("interceptConstructor")) throw new RuntimeException JavaDoc("vanilla constructor didn't get intercepted");
360
361          vanilla.marker = "error";
362          vanilla.someMethod();
363          if (!vanilla.marker.equals("interceptMethod")) throw new RuntimeException JavaDoc("vanilla.someMethod() didn't get intercepted");
364
365          vanilla.marker = "error";
366          vanilla.field = 5;
367          if (!vanilla.marker.equals("interceptField")) throw new RuntimeException JavaDoc("vanilla.field didn't get intercepted");
368
369       }
370       catch (Throwable JavaDoc ex)
371       {
372          ex.printStackTrace();
373          throw new RuntimeException JavaDoc(ex);
374       }
375    }
376
377    public void testConstructorInterception()
378    {
379       System.out.println("RUNNING CONSTRUCTOR INTERCEPTION");
380       try
381       {
382
383          POJO pojo = new POJO();
384          System.out.println("pojo=" + pojo);
385          POJOChild child = new POJOChild();
386          System.out.println("child=" + child);
387
388          POJORef ref = new POJORef();
389          ref.constructPOJO();
390
391          POJOWildCardConstructorTest wild;
392          wild = new POJOWildCardConstructorTest();
393          if (wild == null) throw new RuntimeException JavaDoc("wild was null!");
394          if (wild.data.equals("error")) throw new RuntimeException JavaDoc("wild() didn't intercept");
395          wild = new POJOWildCardConstructorTest(1);
396          if (wild.data.equals("error")) throw new RuntimeException JavaDoc("wild(int) didn't intercept");
397
398          POJOConstructorTest vanilla;
399          vanilla = new POJOConstructorTest();
400          if (vanilla == null) throw new RuntimeException JavaDoc("vanilla was null!");
401          System.out.println("calling vanilla.data.equals");
402          if (vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla() didn't intercept");
403          if (!vanilla.data.equals("empty")) throw new RuntimeException JavaDoc("vanilla() didn't get correct constructor metadata");
404          vanilla = new POJOConstructorTest(1, 1);
405          if (vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla(int, int) didn't intercept");
406          if (!vanilla.data.equals("int, int")) throw new RuntimeException JavaDoc("vanilla(int, int) didn't get correct constructor metadata");
407          vanilla = new POJOConstructorTest(1);
408          if (!vanilla.data.equals("error")) throw new RuntimeException JavaDoc("vanilla(int) did intercept when it shouldn't have");
409
410       }
411       catch (Throwable JavaDoc ex)
412       {
413          ex.printStackTrace();
414          throw new RuntimeException JavaDoc(ex.getMessage());
415       }
416    }
417
418    public void testExceptions()
419    {
420       System.out.println("TEST AOP EXCEPTIONS");
421       try
422       {
423          NoInterceptorsPOJO pojo = new NoInterceptorsPOJO();
424
425          pojo.throwException();
426
427       }
428       catch (SomeException ignored)
429       {
430          System.out.println("caught SomeException successfully");
431       }
432       try
433       {
434          POJO pojo = new POJO();
435
436          pojo.throwException();
437       }
438       catch (SomeException ignored)
439       {
440          System.out.println("caught SomeException successfully");
441       }
442    }
443
444    public void testMixin()
445    {
446       try
447       {
448          System.out.println("TEST MIXIN");
449          POJO pojo = new POJO();
450          System.out.println("TEST Introduction");
451          Introduction intro = (Introduction) pojo;
452          System.out.println(intro.helloWorld("world"));
453          System.out.println("TEST Introduction2");
454          Introduction2 intro2 = (Introduction2) pojo;
455          System.out.println(intro2.goodbye("world"));
456          System.out.println("TEST InterfaceMixin");
457          InterfaceMixin mixin = (InterfaceMixin) pojo;
458          System.out.println(mixin.whazup());
459
460          POJOChild child = new POJOChild();
461          System.out.println("TEST child Introduction");
462          intro = (Introduction) child;
463          System.out.println(intro.helloWorld("world"));
464          System.out.println("TEST child Introduction2");
465          intro2 = (Introduction2) child;
466          System.out.println(intro2.goodbye("world"));
467          System.out.println("TEST child AnotherIntroduction");
468          SubclassIntroduction sub = (SubclassIntroduction) child;
469          System.out.println(sub.subclassHelloWorld("world"));
470          System.out.println("TEST metadata introduction pointcut");
471          NoInterceptorsPOJO nopojo = new NoInterceptorsPOJO();
472          intro = (Introduction) nopojo;
473
474       }
475       catch (Exception JavaDoc ex)
476       {
477          ex.printStackTrace();
478          throw new RuntimeException JavaDoc(ex);
479       }
480    }
481
482    public void testInstanceMixin() throws Exception JavaDoc
483    {
484       try
485       {
486          AfterInterceptor.lastIntercepted = null;
487          AfterInterceptor.lastTransAttributeAccessed = null;
488          BeforeInterceptor.lastIntercepted = null;
489          BeforeInterceptor.lastTransAttributeAccessed = null;
490
491          System.out.println("TEST INSTANCE MIXIN");
492          POJO pojo = new POJO();
493          ((Advised)pojo)._getInstanceAdvisor().insertInterceptor(new BeforeInterceptor());
494          Introduction intro = (Introduction) pojo;
495          System.out.println(intro.helloWorld("world"));
496          if (!"helloWorld".equals(BeforeInterceptor.lastIntercepted))
497             throw new RuntimeException JavaDoc("Failed on interception test");
498          
499          AfterInterceptor.lastIntercepted = null;
500          AfterInterceptor.lastTransAttributeAccessed = null;
501          BeforeInterceptor.lastIntercepted = null;
502          BeforeInterceptor.lastTransAttributeAccessed = null;
503          
504          ((Advised)pojo)._getInstanceAdvisor().appendInterceptor(new AfterInterceptor());
505
506          System.out.println(intro.helloWorld("world"));
507          if (!"helloWorld".equals(BeforeInterceptor.lastIntercepted))
508             throw new RuntimeException JavaDoc("Failed on interception test");
509          if (!"helloWorld".equals(AfterInterceptor.lastIntercepted))
510             throw new RuntimeException JavaDoc("Failed on interception test");
511          
512          
513          AfterInterceptor.lastIntercepted = null;
514          AfterInterceptor.lastTransAttributeAccessed = null;
515          BeforeInterceptor.lastIntercepted = null;
516          BeforeInterceptor.lastTransAttributeAccessed = null;
517
518          POJOChild child = new POJOChild();
519          ((Advised)child)._getInstanceAdvisor().insertInterceptor(new BeforeInterceptor());
520          ((Advised)child)._getInstanceAdvisor().appendInterceptor(new AfterInterceptor());
521          Introduction2 intro2 = (Introduction2) child;
522          System.out.println(intro2.goodbye("world"));
523          if (!"goodbye".equals(BeforeInterceptor.lastIntercepted))
524             throw new RuntimeException JavaDoc("Failed on interception test");
525          if (!"goodbye".equals(AfterInterceptor.lastIntercepted))
526             throw new RuntimeException JavaDoc("Failed on interception test");
527
528       }
529       catch (Exception JavaDoc ex)
530       {
531          ex.printStackTrace();
532          throw new RuntimeException JavaDoc(ex);
533       }
534    }
535
536    
537    public void testCallerPointcut()
538    {
539       CallingPOJO callingPOJO = new CallingPOJO();
540       callingPOJO.callSomeMethod();
541       callingPOJO.nocallSomeMethod();
542       callingPOJO.callUnadvised();
543    }
544
545    public void testNot()
546    {
547       System.out.println("TEST !STATIC in pointcut expression");
548       NotPOJO pojo = new NotPOJO();
549       CallerInterceptor.called = false;
550       pojo.hello();
551       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("should have intercepted NotPOJO.hello()");
552
553       CallerInterceptor.called = false;
554       NotPOJO.world();
555       if (CallerInterceptor.called) throw new RuntimeException JavaDoc("should not have intercepted NotPOJO.world()");
556    }
557
558    public void testInterfaceUsedInHas()
559    {
560       System.out.println("TEST Interface used in has");
561       SubclassInterfaceUsedInHasImpl pojo = new SubclassInterfaceUsedInHasImpl();
562
563       Class JavaDoc clazz = InterfaceUsedInHasImpl.class;
564       System.out.println(clazz.getName() + " methods=" + Arrays.asList(clazz.getDeclaredMethods()));
565
566       clazz = SubclassInterfaceUsedInHasImpl.class;
567       System.out.println(clazz.getName() + " methods=" + Arrays.asList(clazz.getDeclaredMethods()));
568       
569       CallerInterceptor.called = false;
570       System.out.println("About to invoke pojo.someMethod()");
571       pojo.someMethod();
572       if (CallerInterceptor.called == false) throw new RuntimeException JavaDoc("should have intercepted InterfaceUsedInHas.someMethod()");
573
574       CallerInterceptor.called = false;
575       System.out.println("About to invoke pojo.someOtherMethod()");
576       pojo.someOtherMethod();
577       if (CallerInterceptor.called == false) throw new RuntimeException JavaDoc("should have intercepted SubclassInterfaceUsedInHasImpl.someOtherMethod()");
578    }
579
580    public void testTypedefExecution()
581    {
582       System.out.println("RUNNING TEST TYPEDEF EXECUTION");
583
584       System.out.println("intercept class constructor");
585       CallerInterceptor.called = false;
586       ExecutionTypedefPOJO pojo = new ExecutionTypedefPOJO();
587       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO constructor");
588       CallerInterceptor.called = false;
589
590       System.out.println("intercept class field read");
591       int i = pojo.field1;
592       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO.field1 read " + i);
593       CallerInterceptor.called = false;
594
595       System.out.println("intercept class field write");
596       pojo.field1 = 1;
597       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO.field1 write");
598       CallerInterceptor.called = false;
599
600       System.out.println("intercept class field read");
601       i = pojo.field2;
602       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO.field2 read");
603       CallerInterceptor.called = false;
604
605       System.out.println("intercept class field write");
606       pojo.field2 = 1;
607       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO.field1 write");
608       CallerInterceptor.called = false;
609
610       System.out.println("intercept class method execution");
611       pojo.method();
612       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO.method() execution");
613       CallerInterceptor.called = false;
614
615       try
616       {
617          Serializable JavaDoc ser = (Serializable JavaDoc)pojo;
618          System.out.println(ser);
619       }
620       catch(ClassCastException JavaDoc e)
621       {
622          throw new RuntimeException JavaDoc("ExecutionTypedefPOJO does not implement serializable");
623       }
624
625       try
626       {
627          EmptyInterface ei = (EmptyInterface)pojo;
628          System.out.println(ei);
629       }
630       catch(ClassCastException JavaDoc e)
631       {
632          throw new RuntimeException JavaDoc("ExecutionTypedefPOJO does not implement EmptyInterface");
633       }
634
635       try
636       {
637          Introduction intro = (Introduction)pojo;
638          intro.helloWorld("lalala");
639       }
640       catch(ClassCastException JavaDoc e)
641       {
642          throw new RuntimeException JavaDoc("ExecutionTypedefPOJO does not implement Introduction");
643       }
644       
645       System.out.println("intercept instanceof constructor");
646       ExecutionTypedefPOJO2 pojo2 = new ExecutionTypedefPOJO2();
647       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2 constructor");
648       CallerInterceptor.called = false;
649
650       System.out.println("intercept instanceof field read");
651       i = pojo2.field1;
652       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2.field1 read");
653       CallerInterceptor.called = false;
654
655       System.out.println("intercept instanceof field write");
656       pojo2.field1 = 1;
657       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2.field1 write");
658       CallerInterceptor.called = false;
659
660       System.out.println("intercept instanceof field read");
661       i = pojo2.field2;
662       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2.field2 read");
663       CallerInterceptor.called = false;
664
665       System.out.println("intercept instanceof field write");
666       pojo2.field2 = 1;
667       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2.field1 write");
668       CallerInterceptor.called = false;
669
670       System.out.println("intercept instanceof method execution");
671       pojo2.method();
672       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO2 method execution");
673       CallerInterceptor.called = false;
674
675       try
676       {
677          Serializable JavaDoc ser = (Serializable JavaDoc)pojo;
678          System.out.println("Serializable " + ser);
679       }
680       catch(ClassCastException JavaDoc e)
681       {
682          throw new RuntimeException JavaDoc("ExecutionTypedefPOJO2 does not implement serializable");
683       }
684       
685       System.out.println("intercept annotation constructor");
686       ExecutionTypedefPOJO3 pojo3 = new ExecutionTypedefPOJO3();
687       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3 constructor");
688       CallerInterceptor.called = false;
689
690       System.out.println("intercept annotation field read");
691       i = pojo3.field1;
692       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3.field1 read");
693       CallerInterceptor.called = false;
694
695       System.out.println("intercept annotation field write");
696       pojo3.field1 = 1;
697       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3.field1 write");
698       CallerInterceptor.called = false;
699
700       System.out.println("intercept annotation field read");
701       i = pojo3.field2;
702       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3.field2 read");
703       CallerInterceptor.called = false;
704
705       System.out.println("intercept annotation field write");
706       pojo3.field2 = 1;
707       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3.field2 write");
708       CallerInterceptor.called = false;
709
710       System.out.println("intercept annotation method execution");
711       pojo3.method();
712       if (!CallerInterceptor.called) throw new RuntimeException JavaDoc("Did not intercept ExecutionTypedefPOJO3 method execution");
713
714       try
715       {
716          Serializable JavaDoc ser = (Serializable JavaDoc)pojo;
717          System.out.println(ser);
718       }
719       catch(ClassCastException JavaDoc e)
720       {
721          throw new RuntimeException JavaDoc("ExecutionTypedefPOJO does not implement serializable");
722       }
723       
724    }
725
726    public void testTypedefCaller()
727    {
728       System.out.println("RUNNING TEST TYPEDEF CALLER");
729       CallerTypedefCaller caller = new CallerTypedefCaller();
730       caller.call();
731    }
732    
733    public void testPointcutExceptions()
734    {
735       System.out.println("RUNNING TEST POINTCUT EXCEPTIONS");
736       try
737       {
738          InstanceOfInterceptor.intercepted = false;
739          POJO pojo = new POJO(1);
740          if (!InstanceOfInterceptor.intercepted) throw new RuntimeException JavaDoc("Constructor not intercepted");
741          
742          InstanceOfInterceptor.intercepted = false;
743          pojo.withSomeException();
744          if (!InstanceOfInterceptor.intercepted) throw new RuntimeException JavaDoc("Method 1) not intercepted");
745          
746          InstanceOfInterceptor.intercepted = false;
747          pojo.withExceptionAndOthers("h");
748          if (InstanceOfInterceptor.intercepted) throw new RuntimeException JavaDoc("Method 2) intercepted");
749          
750          InstanceOfInterceptor.intercepted = false;
751          pojo.withExceptionAndOthers(1);
752          if (!InstanceOfInterceptor.intercepted) throw new RuntimeException JavaDoc("Method 3) not intercepted");
753          
754          InstanceOfInterceptor.intercepted = false;
755          pojo.withClassCastException(1);
756          if (!InstanceOfInterceptor.intercepted) throw new RuntimeException JavaDoc("Method 4) not intercepted");
757       }
758       catch (NotSerializableException JavaDoc e)
759       {
760          // TODO Auto-generated catch block
761
throw new RuntimeException JavaDoc(e);
762       }
763       catch (ClassCastException JavaDoc e)
764       {
765          // TODO Auto-generated catch block
766
throw new RuntimeException JavaDoc(e);
767       }
768       catch (SomeException e)
769       {
770          // TODO Auto-generated catch block
771
throw new RuntimeException JavaDoc(e);
772       }
773    }
774    
775    public void testPointcutExceptionsCaller()
776    {
777       System.out.println("RUNNING TEST POINTCUT EXCEPTIONS CALLER");
778       CallingPOJO callingPOJO = new CallingPOJO();
779       callingPOJO.callUnadvisedWithPointcutException();
780    }
781    
782    public void testNotAllAdvisedFields()
783    {
784       System.out.println("RUNNING TEST NOT ALL ADVISED FIELDS");
785       NotAllAdvisedFieldsPOJO pojo = new NotAllAdvisedFieldsPOJO();
786
787       SimpleInterceptor.lastFieldIntercepted = null;
788       assertEquals(5, pojo.notadvised);
789       assertNull(SimpleInterceptor.lastFieldIntercepted);
790       
791       SimpleInterceptor.lastFieldIntercepted = null;
792       assertEquals(10, pojo.thisisadvised);
793       assertEquals("thisisadvised", SimpleInterceptor.lastFieldIntercepted);
794       
795       SimpleInterceptor.lastFieldIntercepted = null;
796       pojo.thisisadvised = 20;
797       assertEquals("thisisadvised", SimpleInterceptor.lastFieldIntercepted);
798       assertEquals(20, pojo.thisisadvised);
799    }
800    
801    public void testMixWildCardsAndSpecifiedParameters()
802    {
803       NamedInterceptor.invoked.clear();
804       MixedParametersPOJO pojo = new MixedParametersPOJO(1, "5", 6);
805       assertTrue("Found A", NamedInterceptor.invoked.contains("A"));
806       assertTrue("Found A", NamedInterceptor.invoked.contains("B"));
807       assertTrue("Found A", NamedInterceptor.invoked.contains("C"));
808       assertTrue("Found A", NamedInterceptor.invoked.contains("D"));
809       assertTrue("Found A", NamedInterceptor.invoked.contains("E"));
810       assertTrue("Found A", NamedInterceptor.invoked.contains("F"));
811       assertFalse("Didn't find NotBound", NamedInterceptor.invoked.contains("NotBound"));
812
813       NamedInterceptor.invoked.clear();
814       pojo.method(1, "5", 6, "X");
815       assertTrue("Found A", NamedInterceptor.invoked.contains("A"));
816       assertTrue("Found A", NamedInterceptor.invoked.contains("B"));
817       assertTrue("Found A", NamedInterceptor.invoked.contains("C"));
818       assertTrue("Found A", NamedInterceptor.invoked.contains("D"));
819       assertTrue("Found A", NamedInterceptor.invoked.contains("E"));
820       assertTrue("Found A", NamedInterceptor.invoked.contains("F"));
821       assertTrue("Found A", NamedInterceptor.invoked.contains("G"));
822       assertTrue("Found A", NamedInterceptor.invoked.contains("H"));
823       assertFalse("Didn't find NotBound", NamedInterceptor.invoked.contains("NotBound"));
824    }
825    // Inner classes -------------------------------------------------
826
}
827
828
Popular Tags