KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
22
23 import org.springframework.aop.MethodMatcher;
24 import org.springframework.beans.IOther;
25 import org.springframework.beans.ITestBean;
26 import org.springframework.beans.TestBean;
27 import org.springframework.util.SerializationTestUtils;
28
29 /**
30  * $Id: MethodMatchersTests.java,v 1.7 2005/03/25 09:28:18 jhoeller Exp $
31  */

32 public class MethodMatchersTests extends TestCase {
33
34     private final Method JavaDoc EXCEPTION_GETMESSAGE;
35
36     private final Method JavaDoc ITESTBEAN_SETAGE;
37     
38     private final Method JavaDoc ITESTBEAN_GETAGE;
39     
40     private final Method JavaDoc IOTHER_ABSQUATULATE;
41
42     public MethodMatchersTests() throws Exception JavaDoc {
43         EXCEPTION_GETMESSAGE = Exception JavaDoc.class.getMethod("getMessage", (Class JavaDoc[]) null);
44         ITESTBEAN_GETAGE = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
45         ITESTBEAN_SETAGE = ITestBean.class.getMethod("setAge", new Class JavaDoc[] { int.class });
46         IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", (Class JavaDoc[]) null);
47     }
48
49     public void testDefaultMatchesAll() throws Exception JavaDoc {
50         MethodMatcher defaultMm = MethodMatcher.TRUE;
51         assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception JavaDoc.class));
52         assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
53     }
54     
55     public void testMethodMatcherTrueSerializable() throws Exception JavaDoc {
56         assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE);
57     }
58
59     public void testSingle() throws Exception JavaDoc {
60         MethodMatcher defaultMm = MethodMatcher.TRUE;
61         assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception JavaDoc.class));
62         assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
63         defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get"));
64
65         assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception JavaDoc.class));
66         assertFalse(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
67     }
68
69     
70     public void testDynamicAndStaticMethodMatcherIntersection() throws Exception JavaDoc {
71         MethodMatcher mm1 = MethodMatcher.TRUE;
72         MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches();
73         MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2);
74         assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
75         assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
76         assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(5) }));
77         // Knock out dynamic part
78
intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
79         assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
80         assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
81         assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object JavaDoc[] { new Integer JavaDoc(5) }));
82     }
83     
84     public void testStaticMethodMatcherUnion() throws Exception JavaDoc {
85         MethodMatcher getterMatcher = new StartsWithMatcher("get");
86         MethodMatcher setterMatcher = new StartsWithMatcher("set");
87         MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher);
88         
89         assertFalse("Union is a static matcher", union.isRuntime());
90         assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class));
91         assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class));
92         assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class));
93
94     }
95
96
97     public static class StartsWithMatcher extends StaticMethodMatcher {
98         private String JavaDoc prefix;
99         public StartsWithMatcher(String JavaDoc s) {
100             this.prefix = s;
101         }
102         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass) {
103             return m.getName().startsWith(prefix);
104         }
105     }
106
107
108     private static class TestDynamicMethodMatcherWhichMatches extends DynamicMethodMatcher {
109         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass, Object JavaDoc[] args) {
110             return true;
111         }
112     }
113
114     private static class TestDynamicMethodMatcherWhichDoesNotMatch extends DynamicMethodMatcher {
115         public boolean matches(Method JavaDoc m, Class JavaDoc targetClass, Object JavaDoc[] args) {
116             return false;
117         }
118     }
119
120 }
121
Popular Tags