1 2 17 18 package org.springframework.aop.support; 19 20 import java.lang.reflect.Method ; 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 33 public class ComposablePointcutTests extends TestCase { 34 35 public static MethodMatcher GETTER_METHOD_MATCHER = new StaticMethodMatcher() { 36 public boolean matches(Method m, Class 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 m, Class targetClass) { 43 return m.getName().equals("getAge"); 44 } 45 }; 46 47 public static MethodMatcher ABSQUATULATE_METHOD_MATCHER = new StaticMethodMatcher() { 48 public boolean matches(Method m, Class targetClass) { 49 return m.getName().equals("absquatulate"); 50 } 51 }; 52 53 public static MethodMatcher SETTER_METHOD_MATCHER = new StaticMethodMatcher() { 54 public boolean matches(Method m, Class targetClass) { 55 return m.getName().startsWith("set"); 56 } 57 }; 58 59 public void testMatchAll() throws NoSuchMethodException { 60 Pointcut pc = new ComposablePointcut(); 61 assertTrue(pc.getClassFilter().matches(Object .class)); 62 assertTrue(pc.getMethodMatcher().matches(Object .class.getMethod("hashCode", (Class []) null), Exception .class)); 63 } 64 65 public void testFilterByClass() throws NoSuchMethodException { 66 ComposablePointcut pc = new ComposablePointcut(); 67 68 assertTrue(pc.getClassFilter().matches(Object .class)); 69 70 ClassFilter cf = new RootClassFilter(Exception .class); 71 pc.intersection(cf); 72 assertFalse(pc.getClassFilter().matches(Object .class)); 73 assertTrue(pc.getClassFilter().matches(Exception .class)); 74 pc.intersection(new RootClassFilter(NestedRuntimeException.class)); 75 assertFalse(pc.getClassFilter().matches(Exception .class)); 76 assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class)); 77 assertFalse(pc.getClassFilter().matches(String .class)); 78 pc.union(new RootClassFilter(String .class)); 79 assertFalse(pc.getClassFilter().matches(Exception .class)); 80 assertTrue(pc.getClassFilter().matches(String .class)); 81 assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class)); 82 } 83 84 public void testUnionMethodMatcher() { 85 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 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 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 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 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 |