1 16 17 package org.springframework.aop.support; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.aop.Pointcut; 22 import org.springframework.aop.framework.ProxyFactory; 23 import org.springframework.aop.interceptor.NopInterceptor; 24 import org.springframework.beans.ITestBean; 25 import org.springframework.beans.TestBean; 26 27 31 public class ControlFlowPointcutTests extends TestCase { 32 33 public ControlFlowPointcutTests(String s) { 34 super(s); 35 } 36 37 public void testMatches() { 38 TestBean target = new TestBean(); 39 target.setAge(27); 40 NopInterceptor nop = new NopInterceptor(); 41 ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge"); 42 ProxyFactory pf = new ProxyFactory(target); 43 ITestBean proxied = (ITestBean) pf.getProxy(); 44 pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop)); 45 46 assertEquals(target.getAge(), proxied.getAge()); 48 assertEquals(0, nop.getCount()); 49 50 assertEquals(target.getAge(), new One().getAge(proxied)); 52 assertEquals(1, nop.getCount()); 53 54 assertEquals(target.getAge(), new One().nomatch(proxied)); 56 assertEquals(1, nop.getCount()); 57 assertEquals(3, cflow.getEvaluations()); 58 } 59 60 67 public void testSelectiveApplication() { 68 TestBean target = new TestBean(); 69 target.setAge(27); 70 NopInterceptor nop = new NopInterceptor(); 71 ControlFlowPointcut cflow = new ControlFlowPointcut(One.class); 72 Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow); 73 ProxyFactory pf = new ProxyFactory(target); 74 ITestBean proxied = (ITestBean) pf.getProxy(); 75 pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop)); 76 77 target.setAge(16); 79 assertEquals(0, nop.getCount()); 80 81 assertEquals(16, new One().getAge(proxied)); 83 assertEquals(0, nop.getCount()); 84 85 new One().set(proxied); 87 assertEquals(1, nop.getCount()); 88 89 assertEquals(1, cflow.getEvaluations()); 91 } 92 93 94 public class One { 95 int getAge(ITestBean proxied) { 96 return proxied.getAge(); 97 } 98 int nomatch(ITestBean proxied) { 99 return proxied.getAge(); 100 } 101 void set(ITestBean proxied) { 102 proxied.setAge(5); 103 } 104 } 105 106 } 107 | Popular Tags |