use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(nop2.getCount()).isEqualTo(1);
// Removes counting before advisor
pf.removeAdvisor(1);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(nop2.getCount()).isEqualTo(2);
// Removes Nop1
pf.removeAdvisor(0);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(nop2.getCount()).isEqualTo(3);
// Check out of bounds
try {
pf.removeAdvisor(-1);
} catch (AopConfigException ex) {
// Ok
}
try {
pf.removeAdvisor(2);
} catch (AopConfigException ex) {
// Ok
}
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(nop2.getCount()).isEqualTo(4);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(pf.removeAdvisor(advisor)).isTrue();
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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);
assertThat(cba1.getCalls()).isEqualTo(1);
assertThat(cba2.getCalls()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)).isFalse();
assertThat(advised.replaceAdvisor(advisor1, advisor2)).isTrue();
assertThat(pf.getAdvisors()[0]).isEqualTo(advisor2);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba1.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(cba2.getCalls()).isEqualTo(1);
assertThat(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)).isFalse();
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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, @Nullable 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);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(cba.getCalls()).isEqualTo(0);
assertThat(cba.getCalls("getAge")).isEqualTo(0);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(cba.getCalls("getAge")).isEqualTo(1);
assertThat(cba.getCalls("setAge")).isEqualTo(0);
// Won't be advised
proxied.setAge(26);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(proxied.getAge()).isEqualTo(26);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.
@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 0;
}
});
NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
}
});
IOverloads proxy = (IOverloads) createProxy(pc);
assertThat(overLoadInts.getCount()).isEqualTo(0);
assertThat(overLoadVoids.getCount()).isEqualTo(0);
proxy.overload();
assertThat(overLoadInts.getCount()).isEqualTo(0);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
assertThat(proxy.overload(25)).isEqualTo(25);
assertThat(overLoadInts.getCount()).isEqualTo(1);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
proxy.noAdvice();
assertThat(overLoadInts.getCount()).isEqualTo(1);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Aggregations