use of org.springframework.aop.Advisor 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.Advisor 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();
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
pc.addAdvisor(advisor);
assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
pc.setOpaque(true);
assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertThat(proxied.getAge()).isEqualTo(10);
assertThat(mba.getCalls()).isEqualTo(1);
boolean condition = proxied instanceof Advised;
assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class EnableAsyncTests method customAsyncAnnotationIsPropagated.
@Test
public void customAsyncAnnotationIsPropagated() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class);
ctx.refresh();
Object bean = ctx.getBean(CustomAsyncBean.class);
assertThat(AopUtils.isAopProxy(bean)).isTrue();
boolean isAsyncAdvised = false;
for (Advisor advisor : ((Advised) bean).getAdvisors()) {
if (advisor instanceof AsyncAnnotationAdvisor) {
isAsyncAdvised = true;
break;
}
}
assertThat(isAsyncAdvised).as("bean was not async advised as expected").isTrue();
ctx.close();
}
use of org.springframework.aop.Advisor in project spring-framework by spring-projects.
the class PerThisAspect method aspectMethodThrowsExceptionLegalOnSignature.
@Test
void aspectMethodThrowsExceptionLegalOnSignature() {
TestBean target = new TestBean();
UnsupportedOperationException expectedException = new UnsupportedOperationException();
List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(itb::getAge);
}
Aggregations