use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testOneAdvisedObjectCallsAnother.
/**
* Check that the two MethodInvocations necessary are independent and
* don't conflict.
* Check also proxy exposure.
*/
@Test
public void testOneAdvisedObjectCallsAnother() {
int age1 = 33;
int age2 = 37;
TestBean target1 = new TestBean();
ProxyFactory pf1 = new ProxyFactory(target1);
// Permit proxy and invocation checkers to get context from AopContext
pf1.setExposeProxy(true);
NopInterceptor di1 = new NopInterceptor();
pf1.addAdvice(0, di1);
pf1.addAdvice(1, new ProxyMatcherInterceptor());
pf1.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
pf1.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
// Must be first
pf1.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
ITestBean advised1 = (ITestBean) pf1.getProxy();
// = 1 invocation
advised1.setAge(age1);
TestBean target2 = new TestBean();
ProxyFactory pf2 = new ProxyFactory(target2);
pf2.setExposeProxy(true);
NopInterceptor di2 = new NopInterceptor();
pf2.addAdvice(0, di2);
pf2.addAdvice(1, new ProxyMatcherInterceptor());
pf2.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
pf2.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
pf2.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
ITestBean advised2 = (ITestBean) createProxy(pf2);
advised2.setAge(age2);
// = 2 invocations
advised1.setSpouse(advised2);
// = 3 invocations
assertThat(advised1.getAge()).as("Advised one has correct age").isEqualTo(age1);
assertThat(advised2.getAge()).as("Advised two has correct age").isEqualTo(age2);
// Means extra call on advised 2
// = 4 invocations on 1 and another one on 2
assertThat(advised1.getSpouse().getAge()).as("Advised one spouse has correct age").isEqualTo(age2);
assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(4);
// Got hit by call to advised1.getSpouse().getAge()
assertThat(di2.getCount()).as("one was invoked correct number of times").isEqualTo(3);
}
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 testStaticMethodPointcut.
@Test
public void testStaticMethodPointcut() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
NopInterceptor di = new NopInterceptor();
TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
pc.addAdvisor(sp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertThat(0).isEqualTo(di.getCount());
it.getAge();
assertThat(1).isEqualTo(di.getCount());
it.setAge(11);
assertThat(11).isEqualTo(it.getAge());
assertThat(2).isEqualTo(di.getCount());
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testTestBeanIntroduction.
private void testTestBeanIntroduction(ProxyFactory pc) {
int newAge = 65;
ITestBean itb = (ITestBean) createProxy(pc);
itb.setAge(newAge);
assertThat(itb.getAge()).isEqualTo(newAge);
Lockable lockable = (Lockable) itb;
assertThat(lockable.locked()).isFalse();
lockable.lock();
assertThat(itb.getAge()).isEqualTo(newAge);
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> itb.setAge(1));
assertThat(itb.getAge()).isEqualTo(newAge);
// Unlock
assertThat(lockable.locked()).isTrue();
lockable.unlock();
itb.setAge(1);
assertThat(itb.getAge()).isEqualTo(1);
}
Aggregations