use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryTests method testRemoveAdvisorByReference.
@Test
public void testRemoveAdvisorByReference() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertEquals(1, cba.getCalls());
assertEquals(1, nop.getCount());
assertTrue(pf.removeAdvisor(advisor));
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null)));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryTests method testRemoveAdvisorByIndex.
@Test
public void testRemoveAdvisorByIndex() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
NopInterceptor nop2 = new NopInterceptor();
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertEquals(1, cba.getCalls());
assertEquals(1, nop.getCount());
assertEquals(1, nop2.getCount());
// Removes counting before advisor
pf.removeAdvisor(1);
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertEquals(2, nop2.getCount());
// Removes Nop1
pf.removeAdvisor(0);
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertEquals(3, nop2.getCount());
// Check out of bounds
try {
pf.removeAdvisor(-1);
} catch (AopConfigException ex) {
// Ok
}
try {
pf.removeAdvisor(2);
} catch (AopConfigException ex) {
// Ok
}
assertEquals(5, proxied.getAge());
assertEquals(4, nop2.getCount());
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryTests method testReplaceAdvisor.
@Test
public void testReplaceAdvisor() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
pf.addAdvisor(advisor1);
pf.addAdvice(nop);
ITestBean proxied = (ITestBean) pf.getProxy();
// Use the type cast feature
// Replace etc methods on advised should be same as on ProxyFactory
Advised advised = (Advised) proxied;
proxied.setAge(5);
assertEquals(1, cba1.getCalls());
assertEquals(0, cba2.getCalls());
assertEquals(1, nop.getCount());
assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2));
assertTrue(advised.replaceAdvisor(advisor1, advisor2));
assertEquals(advisor2, pf.getAdvisors()[0]);
assertEquals(5, proxied.getAge());
assertEquals(1, cba1.getCalls());
assertEquals(2, nop.getCount());
assertEquals(1, cba2.getCalls());
assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class CountingAspectJAdvice method testAdviceInvokedCorrectly.
@Test
public void testAdviceInvokedCorrectly() throws Exception {
CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");
ITestBean bean = getTestBean();
assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge"));
assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName"));
bean.getAge();
assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge"));
assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge"));
bean.getName();
assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName"));
assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName"));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.
@Test
public void testBeforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterCount() == 0;
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cba.getCalls());
assertEquals(0, cba.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(1, cba.getCalls("getAge"));
assertEquals(0, cba.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(1, cba.getCalls());
assertEquals(26, proxied.getAge());
}
Aggregations