KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

33 public class ComposablePointcutTests extends TestCase {
34     
35     public static MethodMatcher GETTER_METHOD_MATCHER = new StaticMethodMatcher() {
36         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
37             return m.getName().startsWith("get");
38         }
39     };
40     
41     public static MethodMatcher GET_AGE_METHOD_MATCHER = new StaticMethodMatcher() {
42         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
43             return m.getName().equals("getAge");
44         }
45     };
46     
47     public static MethodMatcher ABSQUATULATE_METHOD_MATCHER = new StaticMethodMatcher() {
48         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
49             return m.getName().equals("absquatulate");
50         }
51     };
52     
53     public static MethodMatcher SETTER_METHOD_MATCHER = new StaticMethodMatcher() {
54         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
55             return m.getName().startsWith("set");
56         }
57     };
58     
59     public void testMatchAll() throws NoSuchMethodException JavaDoc {
60         Pointcut pc = new ComposablePointcut();
61         assertTrue(pc.getClassFilter().matches(Object JavaDoc.class));
62         assertTrue(pc.getMethodMatcher().matches(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), Exception JavaDoc.class));
63     }
64
65     public void testFilterByClass() throws NoSuchMethodException JavaDoc {
66         ComposablePointcut pc = new ComposablePointcut();
67     
68         assertTrue(pc.getClassFilter().matches(Object JavaDoc.class));
69         
70         ClassFilter cf = new RootClassFilter(Exception JavaDoc.class);
71         pc.intersection(cf);
72         assertFalse(pc.getClassFilter().matches(Object JavaDoc.class));
73         assertTrue(pc.getClassFilter().matches(Exception JavaDoc.class));
74         pc.intersection(new RootClassFilter(NestedRuntimeException.class));
75         assertFalse(pc.getClassFilter().matches(Exception JavaDoc.class));
76         assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
77         assertFalse(pc.getClassFilter().matches(String JavaDoc.class));
78         pc.union(new RootClassFilter(String JavaDoc.class));
79         assertFalse(pc.getClassFilter().matches(Exception JavaDoc.class));
80         assertTrue(pc.getClassFilter().matches(String JavaDoc.class));
81         assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
82     }
83
84     public void testUnionMethodMatcher() {
85         // Matches the getAge() method in any class
86
ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, GET_AGE_METHOD_MATCHER);
87         assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class, null));
88         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class, null));
89         assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class, null));
90         
91         pc.union(GETTER_METHOD_MATCHER);
92         // Should now match all getter methods
93
assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class, null));
94         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class, null));
95         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class, null));
96         
97         pc.union(ABSQUATULATE_METHOD_MATCHER);
98         // Should now match absquatulate() as well
99
assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class, null));
100         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class, null));
101         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class, null));
102         // But it doesn't match everything
103
assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_SET_AGE, TestBean.class, null));
104     }
105
106     public void testIntersectionMethodMatcher() {
107         ComposablePointcut pc = new ComposablePointcut();
108         assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
109         assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class));
110         assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class));
111         pc.intersection(GETTER_METHOD_MATCHER);
112         assertFalse(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
113         assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class));
114         assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class));
115         pc.intersection(GET_AGE_METHOD_MATCHER);
116         // Use the Pointcuts matches method
117
assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class, null));
118         assertTrue(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_AGE, TestBean.class, null));
119         assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class, null));
120     }
121
122 }
123
Popular Tags