KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > InterfaceDefinedMethodTest


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
12 import java.util.SortedSet JavaDoc;
13 import java.util.TreeSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.codehaus.aspectwerkz.annotation.Before;
17 import org.codehaus.aspectwerkz.annotation.Around;
18 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
19
20 /**
21  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
22  */

23 public class InterfaceDefinedMethodTest extends TestCase {
24
25     public static String JavaDoc s_log = "";
26
27     public InterfaceDefinedMethodTest(String JavaDoc s) {
28         super(s);
29     }
30
31     public InterfaceDefinedMethodTest() {
32         SortedSet JavaDoc ss = new TreeSet JavaDoc();
33         ss.add("foo"); // Warning, add is in super interface
34
ss.first(); // Ok, first is in SortedSet
35

36         try {
37             Set JavaDoc s = ss;
38             s.add("bar"); // Ok, add is in Set
39
throw new NullPointerException JavaDoc("fake");
40         } catch (NullPointerException JavaDoc npe) {
41             ;
42         }
43     }
44
45     /**
46      * When visiting the bytecode of this method, the classInfo must lookup in the class + intf
47      * hierarchy
48      */

49     public void testInterfaceDefinedMethod() {
50         s_log = "";
51         SortedSet JavaDoc ss = new TreeSet JavaDoc();
52         ss.add("foo"); // Warning, add is in super interface
53
ss.first(); // Ok, first is in SortedSet
54

55         try {
56             Set JavaDoc s = ss;
57             s.add("bar"); // Ok, add is in Set
58
throw new NullPointerException JavaDoc("fake");
59         } catch (NullPointerException JavaDoc npe) {
60             ;
61         }
62         assertEquals("advice advice advice advice advice advice advice ", s_log);
63     }
64
65     public void testWithinCtor() {
66         s_log = "";
67         InterfaceDefinedMethodTest me = new InterfaceDefinedMethodTest();
68         assertEquals("around around around around around around around ", s_log);
69     }
70
71     public void testWithinNot() {
72         s_log = "";
73         withinNot();
74         assertEquals("withincode withincode withincode ", s_log);
75     }
76
77     private void withinNot() {
78         InterfaceDefinedMethodTest me = new InterfaceDefinedMethodTest("ignore");
79         subWithinNot();
80     }
81
82     private void subWithinNot() {
83         ;
84     }
85
86
87     public static class Aspect {
88
89         @Before("withincode(* test.InterfaceDefinedMethodTest.testInterfaceDefinedMethod(..))")
90         public void before(StaticJoinPoint sjp) {
91             s_log += "advice ";
92         }
93
94         @Around("withincode(test.InterfaceDefinedMethodTest.new())")
95         public Object JavaDoc around(StaticJoinPoint sjp) throws Throwable JavaDoc {
96             s_log += "around ";
97             return sjp.proceed();
98         }
99
100         @Before("cflow(within(test.InterfaceDefinedMethodTest) && call(* test.InterfaceDefinedMethodTest.withinNot()))" +
101                 "&& !withincode(* test.InterfaceDefinedMethodTest.withinNot())" +
102                 "&& within(test.InterfaceDefinedMethodTest)")
103         public void neverCalled(StaticJoinPoint sjp) {
104             s_log += "withincode ";
105             //System.out.println(sjp.getType() + " " + sjp.getSignature());
106
}
107     }
108
109
110     public static void main(String JavaDoc[] args) {
111         junit.textui.TestRunner.run(suite());
112     }
113
114     public static junit.framework.Test suite() {
115         return new junit.framework.TestSuite(InterfaceDefinedMethodTest.class);
116     }
117 }
118
Popular Tags