use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testUseAsHashKey.
@Test
public void testUseAsHashKey() {
TestBean target1 = new TestBean();
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvice(new NopInterceptor());
ITestBean proxy1 = (ITestBean) createProxy(pf1);
TestBean target2 = new TestBean();
ProxyFactory pf2 = new ProxyFactory(target2);
pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
ITestBean proxy2 = (ITestBean) createProxy(pf2);
HashMap<ITestBean, Object> h = new HashMap<>();
Object value1 = "foo";
Object value2 = "bar";
assertThat(h.get(proxy1)).isNull();
h.put(proxy1, value1);
h.put(proxy2, value2);
assertThat(value1).isEqualTo(h.get(proxy1));
assertThat(value2).isEqualTo(h.get(proxy2));
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAfterReturningAdvisorIsInvoked.
@Test
public void testAfterReturningAdvisorIsInvoked() {
class SummingAfterAdvice implements AfterReturningAdvice {
public int sum;
@Override
public void afterReturning(@Nullable Object returnValue, Method m, Object[] args, @Nullable Object target) throws Throwable {
sum += ((Integer) returnValue).intValue();
}
}
SummingAfterAdvice aa = new SummingAfterAdvice();
@SuppressWarnings("serial") Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getReturnType() == int.class;
}
};
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesInt);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesInt);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(aa.sum).isEqualTo(0);
int i1 = 12;
int i2 = 13;
// Won't be advised
proxied.setAge(i1);
assertThat(proxied.getAge()).isEqualTo(i1);
assertThat(aa.sum).isEqualTo(i1);
proxied.setAge(i2);
assertThat(proxied.getAge()).isEqualTo(i2);
assertThat(aa.sum).isEqualTo((i1 + i2));
assertThat(proxied.getAge()).isEqualTo(i2);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAfterReturningAdvisorIsNotInvokedOnException.
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(car);
assertThat(pf.getAdvisors()[1].getAdvice()).as("Advice was wrapped in Advisor and added").isEqualTo(car);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(car.getCalls()).isEqualTo(0);
int age = 10;
proxied.setAge(age);
assertThat(proxied.getAge()).isEqualTo(age);
assertThat(car.getCalls()).isEqualTo(2);
Exception exc = new Exception();
// On exception it won't be invoked
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> proxied.exceptional(exc)).satisfies(ex -> assertThat(ex).isSameAs(exc));
assertThat(car.getCalls()).isEqualTo(2);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testTargetCanGetProxy.
@Test
public void testTargetCanGetProxy() {
NopInterceptor di = new NopInterceptor();
INeedsToSeeProxy target = new TargetChecker();
ProxyFactory proxyFactory = new ProxyFactory(target);
proxyFactory.setExposeProxy(true);
assertThat(proxyFactory.isExposeProxy()).isTrue();
proxyFactory.addAdvice(0, di);
INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory);
assertThat(di.getCount()).isEqualTo(0);
assertThat(target.getCount()).isEqualTo(0);
proxied.incrementViaThis();
assertThat(target.getCount()).as("Increment happened").isEqualTo(1);
assertThat(di.getCount()).as("Only one invocation via AOP as use of this wasn't proxied").isEqualTo(1);
// 1 invocation
assertThat(proxied.getCount()).as("Increment happened").isEqualTo(1);
// 2 invocations
proxied.incrementViaProxy();
assertThat(target.getCount()).as("Increment happened").isEqualTo(2);
assertThat(di.getCount()).as("3 more invocations via AOP as the first call was reentrant through the proxy").isEqualTo(4);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
Aggregations