1 16 17 package org.springframework.aop.support; 18 19 import java.lang.reflect.Method ; 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 36 public class AopUtilsTests extends TestCase { 37 38 public void testPointcutCanNeverApply() { 39 class TestPointcut extends StaticMethodMatcherPointcut { 40 public boolean matches(Method method, Class clazzy) { 41 return false; 42 } 43 } 44 45 Pointcut no = new TestPointcut(); 46 assertFalse(AopUtils.canApply(no, Object .class)); 47 } 48 49 public void testPointcutAlwaysApplies() { 50 assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object .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 method, Class clazz) { 57 return method.getName().equals("hashCode"); 58 } 59 } 60 61 Pointcut pc = new TestPointcut(); 62 63 assertTrue(AopUtils.canApply(pc, Object .class)); 65 } 66 67 72 public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception { 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 m, Class targetClass, Object [] args) { 86 throw new UnsupportedOperationException (); 87 } 88 }; 89 assertSame(mmpc, mmpc.getMethodMatcher()); 90 assertSame(ClassFilter.TRUE, mmpc.getClassFilter()); 91 92 DynamicMethodMatcherPointcutAdvisor a = new DynamicMethodMatcherPointcutAdvisor() { 93 public boolean matches(Method m, Class targetClass, Object [] args) { 94 throw new UnsupportedOperationException (); 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 |