KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > MethodFilterTest


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import com.tirsen.nanning.definition.AspectClass;
13 import com.tirsen.nanning.definition.BasicInterceptor;
14 import com.tirsen.nanning.definition.InterceptorDefinition;
15 import junit.framework.TestCase;
16
17 /**
18  * TODO document MethodFilterTest
19  *
20  * <!-- $Id: MethodFilterTest.java,v 1.7 2003/05/11 13:40:52 tirsen Exp $ -->
21  *
22  * @author $Author: tirsen $
23  * @version $Revision: 1.7 $
24  */

25
26 public class MethodFilterTest extends TestCase {
27
28     private static List JavaDoc invokedMethods;
29     private static List JavaDoc expectedMethods;
30
31     protected void setUp() throws Exception JavaDoc {
32         invokedMethods = new ArrayList JavaDoc();
33         expectedMethods = new ArrayList JavaDoc();
34     }
35
36     /**
37      * creates a proxy object with a sample InterceptorDefinition which will have methodNameFilter
38      * attribute set to parameter 'filter'
39      * @param filter
40      * @return
41      */

42     private Object JavaDoc createAspectProxy(String JavaDoc filter) {
43         AspectClass aspectClass = new AspectClass();
44         aspectClass.setInterface(SomeAspect.class);
45         InterceptorDefinition interceptorDefinition = new InterceptorDefinition(TestFilterMethodsInterceptor.class);
46         interceptorDefinition.setAttribute("methodNameFilter", filter);
47         aspectClass.addInterceptor(interceptorDefinition);
48         aspectClass.setTarget(SomeAspectImpl.class);
49
50         return aspectClass.newInstance();
51
52     }
53
54
55     public void testA() {
56         Object JavaDoc proxy = createAspectProxy("doIt.*");
57         expectedMethods.add("doIt");
58         expectedMethods.add("doItAgain");
59         SomeAspect aspect = (SomeAspect) proxy;
60
61         aspect.doIt();
62         aspect.doItAgain();
63         aspect.oupsIDidItAgain();
64
65         verify();
66     }
67
68
69     public void testB() {
70         Object JavaDoc proxy = createAspectProxy(".*Again.*");
71         expectedMethods.add("doItAgain");
72         expectedMethods.add("oupsIDidItAgain");
73
74         SomeAspect aspect = (SomeAspect) proxy;
75
76         aspect.doIt();
77         aspect.doItAgain();
78         aspect.oupsIDidItAgain();
79
80         verify();
81     }
82
83     public void testC() {
84         Object JavaDoc proxy = createAspectProxy(".*");
85         expectedMethods.add("doIt");
86         expectedMethods.add("doItAgain");
87         expectedMethods.add("oupsIDidItAgain");
88
89         SomeAspect aspect = (SomeAspect) proxy;
90         aspect.doIt();
91         aspect.doItAgain();
92         aspect.oupsIDidItAgain();
93
94         verify();
95     }
96
97     public void testD() {
98         Object JavaDoc proxy = createAspectProxy("noMethodLikeThat");
99
100         SomeAspect aspect = (SomeAspect) proxy;
101         aspect.doIt();
102         aspect.doItAgain();
103         aspect.oupsIDidItAgain();
104
105         verify();
106     }
107
108     private void verify() {
109         assertEquals("number of executed methods was not as expected",
110                      expectedMethods.size(), invokedMethods.size());
111         for (int i = 0; i < expectedMethods.size(); i++) {
112             String JavaDoc methodName = (String JavaDoc) expectedMethods.get(i);
113             assertTrue(methodName + " was not invoked", invokedMethods.contains(methodName));
114         }
115     }
116
117
118     public static class TestFilterMethodsInterceptor extends BasicInterceptor {
119         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
120             String JavaDoc methodName = invocation.getMethod().getName();
121             //System.out.println ("method name: " + methodName);
122

123             invokedMethods.add(methodName);
124             return invocation.invokeNext();
125         }
126
127     }
128
129
130     public static class SomeAspectImpl implements SomeAspect {
131         public boolean doIt() {
132             //System.out.println ("I am doing it");
133
return true;
134         }
135
136
137         public boolean doItAgain() {
138             //System.out.println ("I am doing it again");
139
return true;
140         }
141
142         public boolean oupsIDidItAgain() {
143             return true;
144         }
145
146
147     }
148
149     public static interface SomeAspect {
150         public boolean doIt();
151
152         public boolean doItAgain();
153
154         public boolean oupsIDidItAgain();
155
156     }
157
158
159 }
160
Popular Tags