1 16 17 package org.springframework.aop.support; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.aop.framework.Advised; 22 import org.springframework.aop.framework.ProxyFactory; 23 import org.springframework.aop.interceptor.NopInterceptor; 24 import org.springframework.aop.interceptor.SerializableNopInterceptor; 25 import org.springframework.beans.Person; 26 import org.springframework.beans.SerializablePerson; 27 import org.springframework.util.SerializationTestUtils; 28 29 33 public class NameMatchMethodPointcutTests extends TestCase { 34 35 protected NameMatchMethodPointcut pc; 36 37 protected Person proxied; 38 39 protected SerializableNopInterceptor nop; 40 41 public NameMatchMethodPointcutTests(String s) { 42 super(s); 43 } 44 45 49 protected void setUp() { 50 ProxyFactory pf = new ProxyFactory(new SerializablePerson()); 51 nop = new SerializableNopInterceptor(); 52 pc = new NameMatchMethodPointcut(); 53 pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop)); 54 proxied = (Person) pf.getProxy(); 55 } 56 57 public void testMatchingOnly() { 58 assertTrue(pc.isMatch("echo", "ech*")); 60 assertTrue(pc.isMatch("setName", "setN*")); 61 assertTrue(pc.isMatch("setName", "set*")); 62 assertFalse(pc.isMatch("getName", "set*")); 63 assertFalse(pc.isMatch("setName", "set")); 64 assertTrue(pc.isMatch("testing", "*ing")); 65 } 66 67 public void testEmpty() throws Throwable { 68 assertEquals(0, nop.getCount()); 69 proxied.getName(); 70 proxied.setName(""); 71 proxied.echo(null); 72 assertEquals(0, nop.getCount()); 73 } 74 75 76 public void testMatchOneMethod() throws Throwable { 77 pc.addMethodName("echo"); 78 pc.addMethodName("set*"); 79 assertEquals(0, nop.getCount()); 80 proxied.getName(); 81 proxied.getName(); 82 assertEquals(0, nop.getCount()); 83 proxied.echo(null); 84 assertEquals(1, nop.getCount()); 85 86 proxied.setName(""); 87 assertEquals(2, nop.getCount()); 88 proxied.setAge(25); 89 assertEquals(25, proxied.getAge()); 90 assertEquals(3, nop.getCount()); 91 } 92 93 public void testSets() throws Throwable { 94 pc.setMappedNames(new String [] { "set*", "echo" }); 95 assertEquals(0, nop.getCount()); 96 proxied.getName(); 97 proxied.setName(""); 98 assertEquals(1, nop.getCount()); 99 proxied.echo(null); 100 assertEquals(2, nop.getCount()); 101 } 102 103 public void testSerializable() throws Throwable { 104 testSets(); 105 Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied); 107 NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice(); 108 p2.getName(); 109 assertEquals(2, nop2.getCount()); 110 p2.echo(null); 111 assertEquals(3, nop2.getCount()); 112 } 113 } 114 | Popular Tags |