1 16 17 package org.springframework.aop.support; 18 19 import java.lang.reflect.Method ; 20 21 import org.springframework.aop.ClassFilter; 22 import org.springframework.aop.Pointcut; 23 import org.springframework.beans.TestBean; 24 25 import junit.framework.TestCase; 26 27 31 public class PointcutsTests extends TestCase { 32 33 public static Method TEST_BEAN_SET_AGE; 34 public static Method TEST_BEAN_GET_AGE; 35 public static Method TEST_BEAN_GET_NAME; 36 public static Method TEST_BEAN_ABSQUATULATE; 37 38 static { 39 try { 40 TEST_BEAN_SET_AGE = TestBean.class.getMethod("setAge", new Class [] { int.class }); 41 TEST_BEAN_GET_AGE = TestBean.class.getMethod("getAge", (Class []) null); 42 TEST_BEAN_GET_NAME = TestBean.class.getMethod("getName", (Class []) null); 43 TEST_BEAN_ABSQUATULATE = TestBean.class.getMethod("absquatulate", (Class []) null); 44 } 45 catch (Exception ex) { 46 throw new RuntimeException ("Shouldn't happen: error in test suite"); 47 } 48 } 49 50 53 public static Pointcut allTestBeanMethodsPointcut = new StaticMethodMatcherPointcut() { 54 public ClassFilter getClassFilter() { 55 return new ClassFilter() { 56 public boolean matches(Class clazz) { 57 return clazz.equals(TestBean.class); 58 } 59 }; 60 } 61 62 public boolean matches(Method m, Class targetClass) { 63 return true; 64 } 65 }; 66 67 public static Pointcut allClassSetterPointcut = Pointcuts.SETTERS; 68 69 public static class MyTestBean extends TestBean { 71 } 72 73 public static Pointcut myTestBeanSetterPointcut = new StaticMethodMatcherPointcut() { 74 public ClassFilter getClassFilter() { 75 return new RootClassFilter(MyTestBean.class); 76 } 77 78 public boolean matches(Method m, Class targetClass) { 79 return m.getName().startsWith("set"); 80 } 81 }; 82 83 public static Pointcut myTestBeanGetterPointcut = new StaticMethodMatcherPointcut() { 85 public ClassFilter getClassFilter() { 86 return new RootClassFilter(MyTestBean.class); 87 } 88 89 public boolean matches(Method m, Class targetClass) { 90 return m.getName().startsWith("get"); 91 } 92 }; 93 94 public static class MyTestBeanSubclass extends MyTestBean { 96 } 97 98 public static Pointcut myTestBeanSubclassGetterPointcut = new StaticMethodMatcherPointcut() { 99 public ClassFilter getClassFilter() { 100 return new RootClassFilter(MyTestBeanSubclass.class); 101 } 102 103 public boolean matches(Method m, Class targetClass) { 104 return m.getName().startsWith("get"); 105 } 106 }; 107 108 public static Pointcut allClassGetterPointcut = Pointcuts.GETTERS; 109 110 public static Pointcut allClassGetAgePointcut = new NameMatchMethodPointcut().addMethodName("getAge"); 111 112 public static Pointcut allClassGetNamePointcut = new NameMatchMethodPointcut().addMethodName("getName"); 113 114 115 public PointcutsTests(String s) { 116 super(s); 117 } 118 119 public void testTrue() { 120 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 121 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class, null)); 122 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 123 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 124 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class, null)); 125 assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 126 } 127 128 public void testMatches() { 129 assertTrue(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 130 assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null)); 131 assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 132 assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 133 assertTrue(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null)); 134 assertFalse(Pointcuts.matches(allClassGetterPointcut, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 135 } 136 137 140 public void testUnionOfSettersAndGetters() { 141 Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut); 142 assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 143 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); 144 assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 145 } 146 147 public void testUnionOfSpecificGetters() { 148 Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut); 149 assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 150 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); 151 assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null)); 152 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null)); 153 assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 154 155 union = Pointcuts.union(union, allClassSetterPointcut); 157 assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 158 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); 159 assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class, null)); 160 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null)); 161 assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 162 163 assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 164 } 165 166 170 public void testUnionOfAllSettersAndSubclassSetters() { 171 assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 172 assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Object [] { new Integer (6)})); 173 assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null)); 174 175 Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut); 176 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); 177 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 178 assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, new Object [] { new Integer (6)})); 180 assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 181 } 182 183 187 public void testIntersectionOfSpecificGettersAndSubclassGetters() { 188 assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class, null)); 189 assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 190 assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, TestBean.class, null)); 191 assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null)); 192 assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, MyTestBean.class, null)); 193 assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 194 195 Pointcut intersection = Pointcuts.intersection(allClassGetAgePointcut, myTestBeanGetterPointcut); 196 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class, null)); 197 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null)); 198 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class, null)); 199 assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 200 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null)); 202 assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null)); 203 204 intersection = Pointcuts.intersection(intersection, myTestBeanSubclassGetterPointcut); 206 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class, null)); 207 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null)); 208 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class, null)); 209 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 210 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null)); 212 assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null)); 213 214 Pointcut union = Pointcuts.union(intersection, allTestBeanMethodsPointcut); 216 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class, null)); 217 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class, null)); 218 assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBean.class, null)); 219 assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class, null)); 220 assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class, null)); 222 assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class, null)); 223 224 assertTrue(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 225 assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, MyTestBean.class, null)); 226 } 227 228 229 232 public void testSimpleIntersection() { 233 Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut); 234 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Object [] { new Integer (6)})); 235 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class, null)); 236 assertFalse(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class, null)); 237 } 238 239 } 240 | Popular Tags |