use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryTests method testIndexOfMethods.
@Test
public void testIndexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
Advised advised = (Advised) pf.getProxy();
// Can use advised and ProxyFactory interchangeably
advised.addAdvice(nop);
pf.addAdvisor(advisor);
assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
assertThat(pf.indexOf(nop)).isEqualTo(0);
assertThat(pf.indexOf(advisor)).isEqualTo(1);
assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testBeforeAdviceThrowsException.
@Test
public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {
@Override
public void before(Method m, Object[] args, Object target) throws Throwable {
super.before(m, args, target);
if (m.getName().startsWith("set")) {
throw rex;
}
}
};
TestBean target = new TestBean();
target.setAge(80);
NopInterceptor nop1 = new NopInterceptor();
NopInterceptor nop2 = new NopInterceptor();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(nop1);
pf.addAdvice(ba);
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) createProxy(pf);
// Won't throw an exception
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(ba.getCalls()).isEqualTo(1);
assertThat(ba.getCalls("getAge")).isEqualTo(1);
assertThat(nop1.getCount()).isEqualTo(1);
assertThat(nop2.getCount()).isEqualTo(1);
// Will fail, after invoking Nop1
assertThatExceptionOfType(RuntimeException.class).as("before advice should have ended chain").isThrownBy(() -> proxied.setAge(26)).matches(rex::equals);
assertThat(ba.getCalls()).isEqualTo(2);
assertThat(nop1.getCount()).isEqualTo(2);
// Nop2 didn't get invoked when the exception was thrown
assertThat(nop2.getCount()).isEqualTo(1);
// Shouldn't have changed value in joinpoint
assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.
@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
ITestBean t = (ITestBean) createProxy(pc);
assertThat(di.getCount()).isEqualTo(0);
t.setAge(23);
assertThat(t.getAge()).isEqualTo(23);
assertThat(di.getCount()).isEqualTo(2);
Advised advised = (Advised) t;
assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
NopInterceptor di2 = new NopInterceptor();
advised.addAdvice(1, di2);
t.getName();
assertThat(di.getCount()).isEqualTo(3);
assertThat(di2.getCount()).isEqualTo(1);
// will remove di
advised.removeAdvisor(0);
t.getAge();
// Unchanged
assertThat(di.getCount()).isEqualTo(3);
assertThat(di2.getCount()).isEqualTo(2);
CountingBeforeAdvice cba = new CountingBeforeAdvice();
assertThat(cba.getCalls()).isEqualTo(0);
advised.addAdvice(cba);
t.setAge(16);
assertThat(t.getAge()).isEqualTo(16);
assertThat(cba.getCalls()).isEqualTo(2);
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializableTargetAndAdvice.
@Test
public void testSerializableTargetAndAdvice() throws Throwable {
SerializablePerson personTarget = new SerializablePerson();
personTarget.setName("jim");
personTarget.setAge(26);
assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
ProxyFactory pf = new ProxyFactory(personTarget);
CountingThrowsAdvice cta = new CountingThrowsAdvice();
pf.addAdvice(new SerializableNopInterceptor());
// Try various advice types
pf.addAdvice(new CountingBeforeAdvice());
pf.addAdvice(new CountingAfterReturningAdvice());
pf.addAdvice(cta);
Person p = (Person) createAopProxy(pf).getProxy();
p.echo(null);
assertThat(cta.getCalls()).isEqualTo(0);
try {
p.echo(new IOException());
} catch (IOException ex) {
/* expected */
}
assertThat(cta.getCalls()).isEqualTo(1);
// Will throw exception if it fails
Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p2).isNotSameAs(p);
assertThat(p2.getName()).isEqualTo(p.getName());
assertThat(p2.getAge()).isEqualTo(p.getAge());
assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
Advised a1 = (Advised) p;
Advised a2 = (Advised) p2;
// Check we can manipulate state of p2
assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
// This should work as SerializablePerson is equal
assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
// Check we can add a new advisor to the target
NopInterceptor ni = new NopInterceptor();
p2.getAge();
assertThat(ni.getCount()).isEqualTo(0);
a2.addAdvice(ni);
p2.getAge();
assertThat(ni.getCount()).isEqualTo(1);
cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
p2.echo(null);
assertThat(cta.getCalls()).isEqualTo(1);
try {
p2.echo(new IOException());
} catch (IOException ex) {
}
assertThat(cta.getCalls()).isEqualTo(2);
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testAddAdviceAtRuntime.
@Test
public void testAddAdviceAtRuntime() {
TestBean bean = new TestBean();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.setTarget(bean);
pf.setFrozen(false);
pf.setOpaque(false);
pf.setProxyTargetClass(true);
TestBean proxy = (TestBean) pf.getProxy();
assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
proxy.getAge();
assertThat(cba.getCalls()).isEqualTo(0);
((Advised) proxy).addAdvice(cba);
proxy.getAge();
assertThat(cba.getCalls()).isEqualTo(1);
}
Aggregations