KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.aopalliance.aop.Advice;
23
24 import org.springframework.aop.ClassFilter;
25 import org.springframework.aop.MethodMatcher;
26 import org.springframework.aop.Pointcut;
27 import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
28 import org.springframework.aop.interceptor.NopInterceptor;
29 import org.springframework.aop.target.EmptyTargetSource;
30 import org.springframework.beans.TestBean;
31 import org.springframework.util.SerializationTestUtils;
32
33 /**
34  * @author Rod Johnson
35  */

36 public class AopUtilsTests extends TestCase {
37
38     public void testPointcutCanNeverApply() {
39         class TestPointcut extends StaticMethodMatcherPointcut {
40             public boolean matches(Method JavaDoc method, Class JavaDoc clazzy) {
41                 return false;
42             }
43         }
44     
45         Pointcut no = new TestPointcut();
46         assertFalse(AopUtils.canApply(no, Object JavaDoc.class));
47     }
48
49     public void testPointcutAlwaysApplies() {
50         assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object JavaDoc.class));
51         assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), TestBean.class));
52     }
53
54     public void testPointcutAppliesToOneMethodOnObject() {
55         class TestPointcut extends StaticMethodMatcherPointcut {
56             public boolean matches(Method JavaDoc method, Class JavaDoc clazz) {
57                 return method.getName().equals("hashCode");
58             }
59         }
60
61         Pointcut pc = new TestPointcut();
62     
63         // will return true if we're not proxying interfaces
64
assertTrue(AopUtils.canApply(pc, Object JavaDoc.class));
65     }
66
67     /**
68      * Test that when we serialize and deserialize various canonical instances
69      * of AOP classes, they return the same instance, not a new instance
70      * that's subverted the singleton construction limitation.
71      */

72     public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception JavaDoc {
73         assertSame(MethodMatcher.TRUE, SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE));
74         assertSame(ClassFilter.TRUE, SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE));
75         assertSame(Pointcut.TRUE, SerializationTestUtils.serializeAndDeserialize(Pointcut.TRUE));
76         assertSame(EmptyTargetSource.INSTANCE, SerializationTestUtils.serializeAndDeserialize(EmptyTargetSource.INSTANCE));
77         assertSame(Pointcuts.SETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.SETTERS));
78         assertSame(Pointcuts.GETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.GETTERS));
79         assertSame(ExposeInvocationInterceptor.INSTANCE,
80                 SerializationTestUtils.serializeAndDeserialize(ExposeInvocationInterceptor.INSTANCE));
81     }
82
83     public void testDynamicSuperclasses() {
84         DynamicMethodMatcherPointcut mmpc = new DynamicMethodMatcherPointcut() {
85             public boolean matches(Method JavaDoc m, Class JavaDoc targetClass, Object JavaDoc[] args) {
86                 throw new UnsupportedOperationException JavaDoc();
87             }
88         };
89         assertSame(mmpc, mmpc.getMethodMatcher());
90         assertSame(ClassFilter.TRUE, mmpc.getClassFilter());
91         
92         DynamicMethodMatcherPointcutAdvisor a = new DynamicMethodMatcherPointcutAdvisor() {
93             public boolean matches(Method JavaDoc m, Class JavaDoc targetClass, Object JavaDoc[] args) {
94                 throw new UnsupportedOperationException JavaDoc();
95             }
96         };
97         Advice advice = new NopInterceptor();
98         a.setAdvice(advice);
99         assertSame(a, a.getMethodMatcher());
100         assertSame(ClassFilter.TRUE, a.getClassFilter());
101         assertSame(a, a.getPointcut());
102         assertSame(advice, a.getAdvice());
103     }
104
105 }
106
Popular Tags