1   /***************************************************************************************
2    * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test.thistarget;
9   
10  import junit.framework.TestCase;
11  import org.codehaus.aspectwerkz.definition.Pointcut;
12  
13  /***
14   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
15   */
16  public class TargetReferencedAndRuntimeCheckTest extends TestCase {
17  
18      private static String s_log = "";
19  
20      //--- Target implements 2 interface and a complex rutime check will thus be done
21      public static interface I0Target {
22          public void call();
23      }
24  
25      public static interface I1Target {
26      }
27  
28      public static interface I2Target {
29      }
30  
31      public static class ImplementsTwoTarget implements I0Target, I1Target, I2Target {
32          public void call() {
33              log("ImplementsTwoTarget");
34          }
35      }
36  
37      public static class ImplementsOneTarget implements I0Target, I1Target {
38          public void call() {
39              log("ImplementsOneTarget");
40          }
41      }
42  
43      public static class ImplementsZeroTarget implements I0Target {
44          public void call() {
45              log("ImplementsZeroTarget");
46          }
47      }
48  
49      //--- Aspect
50  
51      public static class Aspect {
52  
53          /***
54           * @Expression target(myTarget) && call(* test.thistarget.*.call()) && within(test.thistarget.TargetReferencedAndRuntimeCheckTest)
55           */
56          Pointcut referenceI1Target(I1Target myTarget) {
57              return null;
58          }
59  
60          /***
61           * @Expression target(myTarget) && call(* test.thistarget.*.call()) && within(test.thistarget.TargetReferencedAndRuntimeCheckTest)
62           */
63          Pointcut referenceI2Target(I2Target myTarget) {
64              return null;
65          }
66  
67          /***
68           * @Before referenceI1Target(t) && referenceI2Target(t)
69           */
70          void before(Object t) {// if we don't use Object here but f.e. I1Target, the validation visitor will complain
71              log("before_I1TargetAndI2Target");
72              ThisTargetAspect.validate(t, I1Target.class);
73              ThisTargetAspect.validate(t, I2Target.class);
74          }
75  
76          /***
77           * @Before referenceI1Target(t)
78           */
79          void before2(I1Target t) {
80              log("before_I1Target");
81              ThisTargetAspect.validate(t, I1Target.class);
82          }
83      }
84  
85      public void testRuntimeChecks() {
86          I0Target i1 = new ImplementsTwoTarget();
87          s_log = "";
88          i1.call();
89          assertEquals("before_I1TargetAndI2Target before_I1Target ImplementsTwoTarget ", s_log);
90  
91          I0Target i2 = new ImplementsOneTarget();
92          s_log = "";
93          i2.call();
94          assertEquals("before_I1Target ImplementsOneTarget ", s_log);
95  
96          I0Target i3 = new ImplementsZeroTarget();
97          s_log = "";
98          i3.call();
99          assertEquals("ImplementsZeroTarget ", s_log);
100     }
101 
102 
103 
104 
105 
106     //--- JUnit
107 
108     public static void main(String[] args) {
109         junit.textui.TestRunner.run(suite());
110     }
111 
112     public static junit.framework.Test suite() {
113         return new junit.framework.TestSuite(TargetReferencedAndRuntimeCheckTest.class);
114     }
115 
116     static void log(String s) {
117         s_log += s + " ";
118     }
119 
120 
121 }