KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > PointcutsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.support;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 import org.springframework.aop.ClassFilter;
22 import org.springframework.aop.Pointcut;
23 import org.springframework.beans.TestBean;
24
25 import junit.framework.TestCase;
26
27 /**
28  *
29  * @author Rod Johnson
30  */

31 public class PointcutsTests extends TestCase {
32     
33     public static Method JavaDoc TEST_BEAN_SET_AGE;
34     public static Method JavaDoc TEST_BEAN_GET_AGE;
35     public static Method JavaDoc TEST_BEAN_GET_NAME;
36     public static Method JavaDoc TEST_BEAN_ABSQUATULATE;
37     
38     static {
39         try {
40             TEST_BEAN_SET_AGE = TestBean.class.getMethod("setAge", new Class JavaDoc[] { int.class });
41             TEST_BEAN_GET_AGE = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
42             TEST_BEAN_GET_NAME = TestBean.class.getMethod("getName", (Class JavaDoc[]) null);
43             TEST_BEAN_ABSQUATULATE = TestBean.class.getMethod("absquatulate", (Class JavaDoc[]) null);
44         }
45         catch (Exception JavaDoc ex) {
46             throw new RuntimeException JavaDoc("Shouldn't happen: error in test suite");
47         }
48     }
49     
50     /**
51      * Matches only TestBean class, not subclasses
52      */

53     public static Pointcut allTestBeanMethodsPointcut = new StaticMethodMatcherPointcut() {
54         public ClassFilter getClassFilter() {
55             return new ClassFilter() {
56                 public boolean matches(Class JavaDoc clazz) {
57                     return clazz.equals(TestBean.class);
58                 }
59             };
60         }
61
62         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
63             return true;
64         }
65     };
66         
67     public static Pointcut allClassSetterPointcut = Pointcuts.SETTERS;
68     
69     // Subclass used for matching
70
public static class MyTestBean extends TestBean {
71     }
72     
73     public static Pointcut myTestBeanSetterPointcut = new StaticMethodMatcherPointcut() {
74         public ClassFilter getClassFilter() {
75             return new RootClassFilter(MyTestBean.class);
76         }
77
78         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
79             return m.getName().startsWith("set");
80         }
81     };
82     
83     // Will match MyTestBeanSubclass
84
public static Pointcut myTestBeanGetterPointcut = new StaticMethodMatcherPointcut() {
85         public ClassFilter getClassFilter() {
86             return new RootClassFilter(MyTestBean.class);
87         }
88
89         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
90             return m.getName().startsWith("get");
91         }
92     };
93     
94     // Still more specific class
95
public static class MyTestBeanSubclass extends MyTestBean {
96     }
97     
98     public static Pointcut myTestBeanSubclassGetterPointcut = new StaticMethodMatcherPointcut() {
99         public ClassFilter getClassFilter() {
100             return new RootClassFilter(MyTestBeanSubclass.class);
101         }
102
103         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
104             return m.getName().startsWith("get");
105         }
106     };
107     
108     public static Pointcut allClassGetterPointcut = Pointcuts.GETTERS;
109     
110     public static Pointcut allClassGetAgePointcut = new NameMatchMethodPointcut().addMethodName("getAge");
111     
112     public static Pointcut allClassGetNamePointcut = new NameMatchMethodPointcut().addMethodName("getName");
113     
114     
115     public PointcutsTests(String JavaDoc s) {
116         super(s);
117     }
118     
119     public void testTrue() {
120         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
121         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class, null));
122         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
123         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
124         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class, null));
125         assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
126     }
127
128     public void testMatches() {
129         assertTrue(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
130         assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
131         assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
132         assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
133         assertTrue(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
134         assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
135     }
136     
137     /**
138      * Should match all setters and getters on any class
139      */

140     public void testUnionOfSettersAndGetters() {
141         Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut);
142         assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
143         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null));
144         assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
145     }
146     
147     public void testUnionOfSpecificGetters() {
148         Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut);
149         assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
150         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null));
151         assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null));
152         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null));
153         assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
154         
155         // Union with all setters
156
union = Pointcuts.union(union, allClassSetterPointcut);
157         assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
158         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null));
159         assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null));
160         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null));
161         assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
162         
163         assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
164     }
165     
166     /**
167      * Tests vertical composition. First pointcut matches all setters.
168      * Second one matches all getters in the MyTestBean class. TestBean getters shouldn't pass.
169      */

170     public void testUnionOfAllSettersAndSubclassSetters() {
171         assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
172         assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
173         assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
174         
175         Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut);
176         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null));
177         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class, null));
178         // Still doesn't match superclass setter
179
assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
180         assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
181     }
182     
183     /**
184      * Intersection should be MyTestBean getAge() only:
185      * it's the union of allClassGetAge and subclass getters
186      */

187     public void testIntersectionOfSpecificGettersAndSubclassGetters() {
188         assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
189         assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class, null));
190         assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, TestBean.class, null));
191         assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
192         assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, MyTestBean.class, null));
193         assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, MyTestBean.class, null));
194         
195         Pointcut intersection = Pointcuts.intersection(allClassGetAgePointcut, myTestBeanGetterPointcut);
196         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class, null));
197         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null));
198         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class, null));
199         assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class, null));
200         // Matches subclass of MyTestBean
201
assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null));
202         assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null));
203         
204         // Now intersection with MyTestBeanSubclass getters should eliminate MyTestBean target
205
intersection = Pointcuts.intersection(intersection, myTestBeanSubclassGetterPointcut);
206         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class, null));
207         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null));
208         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class, null));
209         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class, null));
210         // Still matches subclass of MyTestBean
211
assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null));
212         assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null));
213         
214         // Now union with all TestBean methods
215
Pointcut union = Pointcuts.union(intersection, allTestBeanMethodsPointcut);
216         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null));
217         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null));
218         assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBean.class, null));
219         assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class, null));
220         // Still matches subclass of MyTestBean
221
assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null));
222         assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null));
223         
224         assertTrue(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
225         assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, MyTestBean.class, null));
226     }
227     
228     
229     /**
230      * The intersection of these two pointcuts leaves nothing.
231      */

232     public void testSimpleIntersection() {
233         Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut);
234         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(6)}));
235         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null));
236         assertFalse(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
237     }
238
239 }
240
Popular Tags