KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.ServletException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.util.SerializationTestUtils;
24
25 /**
26  * @author Rod Johnson
27  * @author Dmitriy Kopylenko
28  * @since 1.1
29  */

30 public abstract class AbstractRegexpMethodPointcutTests extends TestCase {
31     
32     private AbstractRegexpMethodPointcut rpc;
33
34     protected void setUp() {
35         rpc = getRegexpMethodPointcut();
36     }
37
38     protected abstract AbstractRegexpMethodPointcut getRegexpMethodPointcut();
39
40     public void testNoPatternSupplied() throws Exception JavaDoc {
41         noPatternSuppliedTests(rpc);
42     }
43     
44     public void testSerializationWithNoPatternSupplied() throws Exception JavaDoc {
45         rpc = (AbstractRegexpMethodPointcut) SerializationTestUtils.serializeAndDeserialize(rpc);
46         noPatternSuppliedTests(rpc);
47     }
48     
49     protected void noPatternSuppliedTests(AbstractRegexpMethodPointcut rpc) throws Exception JavaDoc {
50         assertFalse(rpc.matches(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), String JavaDoc.class));
51         assertFalse(rpc.matches(Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null), Object JavaDoc.class));
52         assertEquals(0, rpc.getPatterns().length);
53     }
54     
55     public void testExactMatch() throws Exception JavaDoc {
56         rpc.setPattern("java.lang.Object.hashCode");
57         exactMatchTests(rpc);
58         rpc = (AbstractRegexpMethodPointcut) SerializationTestUtils.serializeAndDeserialize(rpc);
59         exactMatchTests(rpc);
60     }
61     
62     protected void exactMatchTests(AbstractRegexpMethodPointcut rpc) throws Exception JavaDoc {
63         // assumes rpc.setPattern("java.lang.Object.hashCode");
64
assertTrue(rpc.matches(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), String JavaDoc.class));
65         assertFalse(rpc.matches(Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null), Object JavaDoc.class));
66     }
67     
68     public void testWildcard() throws Exception JavaDoc {
69         rpc.setPattern(".*Object.hashCode");
70         assertTrue(rpc.matches(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), Object JavaDoc.class));
71         assertFalse(rpc.matches(Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null), Object JavaDoc.class));
72     }
73     
74     public void testWildcardForOneClass() throws Exception JavaDoc {
75         rpc.setPattern("java.lang.Object.*");
76         assertTrue(rpc.matches(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), String JavaDoc.class));
77         assertTrue(rpc.matches(Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null), String JavaDoc.class));
78     }
79     
80     public void testMatchesObjectClass() throws Exception JavaDoc {
81         rpc.setPattern("java.lang.Object.*");
82         assertTrue(rpc.matches(Exception JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), ServletException JavaDoc.class));
83         // Doesn't match a method from Throwable
84
assertFalse(rpc.matches(Exception JavaDoc.class.getMethod("getMessage", (Class JavaDoc[]) null), Exception JavaDoc.class));
85     }
86
87 }
88
Popular Tags