KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > CflowBelowTest


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;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.annotation.Before;
12 import org.codehaus.aspectwerkz.annotation.Expression;
13
14 /**
15  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
16  */

17 public class CflowBelowTest extends TestCase {
18
19     static int s_inAspectCount = 0;
20
21     public void testCflow() {
22         s_inAspectCount = 0;
23         recursiveCflow(3);
24         assertEquals(3, s_inAspectCount);
25     }
26
27     public void testCflowBelow() {
28         s_inAspectCount = 0;
29         recursiveCflowBelow(3);
30         assertEquals(2, s_inAspectCount);// one less
31
}
32
33     /**
34      * Recursive i times.
35      *
36      * @param i
37      */

38     void recursiveCflow(int i) {
39         if (i <= 1) {
40             return;
41         } else {
42             recursiveCflow(i-1);
43         }
44     }
45
46     /**
47      * Recursive i times.
48      *
49      * @param i
50      */

51     void recursiveCflowBelow(int i) {
52         if (i <= 1) {
53             return;
54         } else {
55             recursiveCflowBelow(i-1);
56         }
57     }
58
59     public void testWithincodeAndCflowRuntimeTestOnEnclosingJP() {
60         s_inAspectCount = 0;
61         startCflowWithinCode();
62         assertEquals(1, s_inAspectCount);
63
64         s_inAspectCount = 0;
65         startCflowNotWithinCode();
66         assertEquals(0, s_inAspectCount);
67     }
68
69     public void startCflowWithinCode() {
70         withinCode();
71     }
72
73     public void startCflowNotWithinCode() {
74         notWithinCode();
75     }
76
77     public void withinCode() {
78         targetCall();// call of this method advised by withincode && cflow
79
}
80
81     public void notWithinCode() {
82         targetCall();// call of this method advised by withincode && cflow but don't match
83
}
84
85     public void targetCall() {
86         ;
87     }
88
89
90     //--- JUnit
91

92     public static void main(String JavaDoc[] args) {
93         junit.textui.TestRunner.run(suite());
94     }
95
96     public static junit.framework.Test suite() {
97         return new junit.framework.TestSuite(CflowBelowTest.class);
98     }
99
100
101
102     /**
103      * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
104      */

105     public static class Aspect {
106
107         @Expression("execution(* test.CflowBelowTest.recursiveCflow(..))")
108         void pcCflow(){};
109
110         @Expression("execution(* test.CflowBelowTest.recursiveCflowBelow(..))")
111         void pcCflowBelow(){};
112
113         @Before("pcCflow() && cflow(pcCflow())")
114         public void beforeCflow() {
115             s_inAspectCount++;
116         }
117
118         @Before("pcCflowBelow() && cflowbelow(pcCflowBelow())")
119         public void beforeCflowBelow() {
120             s_inAspectCount++;
121         }
122
123         @Before("call(* test.CflowBelowTest.targetCall())" +
124                 " && withincode(* test.CflowBelowTest.withinCode())" +
125                 " && cflow(execution(* test.CflowBelowTest.startCflowWithinCode()))")
126         public void withinCodeAndCflow() {
127             s_inAspectCount++;
128         }
129
130         @Before("call(* test.CflowBelowTest.targetCall())" +
131                 " && withincode(* test.CflowBelowTest.withinCode())" +
132                 " && cflow(execution(* test.CflowBelowTest.startCflowNotWithinCode()))")
133         public void notWithinCodeAndCflow() {
134             s_inAspectCount++;
135         }
136
137     }
138 }
139
Popular Tags