1 16 17 package org.springframework.aop.support; 18 19 import java.lang.reflect.Method ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.aop.MethodMatcher; 24 import org.springframework.beans.IOther; 25 import org.springframework.beans.ITestBean; 26 import org.springframework.beans.TestBean; 27 import org.springframework.util.SerializationTestUtils; 28 29 32 public class MethodMatchersTests extends TestCase { 33 34 private final Method EXCEPTION_GETMESSAGE; 35 36 private final Method ITESTBEAN_SETAGE; 37 38 private final Method ITESTBEAN_GETAGE; 39 40 private final Method IOTHER_ABSQUATULATE; 41 42 public MethodMatchersTests() throws Exception { 43 EXCEPTION_GETMESSAGE = Exception .class.getMethod("getMessage", (Class []) null); 44 ITESTBEAN_GETAGE = ITestBean.class.getMethod("getAge", (Class []) null); 45 ITESTBEAN_SETAGE = ITestBean.class.getMethod("setAge", new Class [] { int.class }); 46 IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", (Class []) null); 47 } 48 49 public void testDefaultMatchesAll() throws Exception { 50 MethodMatcher defaultMm = MethodMatcher.TRUE; 51 assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception .class)); 52 assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); 53 } 54 55 public void testMethodMatcherTrueSerializable() throws Exception { 56 assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE); 57 } 58 59 public void testSingle() throws Exception { 60 MethodMatcher defaultMm = MethodMatcher.TRUE; 61 assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception .class)); 62 assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); 63 defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get")); 64 65 assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception .class)); 66 assertFalse(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); 67 } 68 69 70 public void testDynamicAndStaticMethodMatcherIntersection() throws Exception { 71 MethodMatcher mm1 = MethodMatcher.TRUE; 72 MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches(); 73 MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2); 74 assertTrue("Intersection is a dynamic matcher", intersection.isRuntime()); 75 assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class)); 76 assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object [] { new Integer (5) })); 77 intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch()); 79 assertTrue("Intersection is a dynamic matcher", intersection.isRuntime()); 80 assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class)); 81 assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object [] { new Integer (5) })); 82 } 83 84 public void testStaticMethodMatcherUnion() throws Exception { 85 MethodMatcher getterMatcher = new StartsWithMatcher("get"); 86 MethodMatcher setterMatcher = new StartsWithMatcher("set"); 87 MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher); 88 89 assertFalse("Union is a static matcher", union.isRuntime()); 90 assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class)); 91 assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class)); 92 assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class)); 93 94 } 95 96 97 public static class StartsWithMatcher extends StaticMethodMatcher { 98 private String prefix; 99 public StartsWithMatcher(String s) { 100 this.prefix = s; 101 } 102 public boolean matches(Method m, Class targetClass) { 103 return m.getName().startsWith(prefix); 104 } 105 } 106 107 108 private static class TestDynamicMethodMatcherWhichMatches extends DynamicMethodMatcher { 109 public boolean matches(Method m, Class targetClass, Object [] args) { 110 return true; 111 } 112 } 113 114 private static class TestDynamicMethodMatcherWhichDoesNotMatch extends DynamicMethodMatcher { 115 public boolean matches(Method m, Class targetClass, Object [] args) { 116 return false; 117 } 118 } 119 120 } 121 | Popular Tags |