use of org.springframework.beans.testfixture.beans.ITestBean 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.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice.
@Test
public void testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> pc.addAdvice(new DummyIntroductionAdviceImpl())).withMessageContaining("ntroduction");
// Check it still works: proxy factory state shouldn't have been corrupted
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAdviceSupportListeners.
@Test
public void testAdviceSupportListeners() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
CountingAdvisorListener l = new CountingAdvisorListener(pc);
pc.addListener(l);
RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
// Should be automatically added as a listener
pc.addListener(acf);
assertThat(pc.isActive()).isFalse();
assertThat(l.activates).isEqualTo(0);
assertThat(acf.refreshes).isEqualTo(0);
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(acf.refreshes).isEqualTo(1);
assertThat(l.activates).isEqualTo(1);
assertThat(pc.isActive()).isTrue();
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(l.adviceChanges).isEqualTo(0);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
assertThat(l.adviceChanges).isEqualTo(1);
assertThat(acf.refreshes).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.removeAdvice(di);
assertThat(l.adviceChanges).isEqualTo(2);
assertThat(acf.refreshes).isEqualTo(3);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.getProxy();
assertThat(l.activates).isEqualTo(1);
pc.removeListener(l);
assertThat(l.adviceChanges).isEqualTo(2);
pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
// No longer counting
assertThat(l.adviceChanges).isEqualTo(2);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThat(pc.isFrozen()).isFalse();
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() -> pc.addAdvice(0, new NopInterceptor())).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(((Advised) proxied).getAdvisors().length).isEqualTo(1);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class SimpleBeforeAdviceInterceptor method testAdvisorAdapterRegistrationManagerPresentInContext.
@Test
public void testAdvisorAdapterRegistrationManagerPresentInContext() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-with-bpp.xml", getClass());
ITestBean tb = (ITestBean) ctx.getBean("testBean");
// just invoke any method to see if advice fired
tb.getName();
getAdviceImpl(tb).getInvocationCounter();
}
Aggregations