use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testRejectsBogusDynamicIntroductionAdviceWithNoAdapter.
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
// TODO May fail on either call: may want to tighten up definition
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.getName();
});
// TODO used to catch UnknownAdviceTypeException, but
// with CGLIB some errors are in proxy creation and are wrapped
// in aspect exception. Error message is still fine.
// assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
pc.addAdvisor(dp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertThat(dp.count).isEqualTo(0);
it.getAge();
assertThat(dp.count).isEqualTo(1);
it.setAge(11);
assertThat(it.getAge()).isEqualTo(11);
assertThat(dp.count).isEqualTo(2);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.
/**
* There are times when we want to call proceed() twice.
* We can do this if we clone the invocation.
*/
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
MethodInterceptor twoBirthdayInterceptor = mi -> {
// Clone the invocation to proceed three times
// "The Moor's Last Sigh": this technology can cause premature aging
MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
clone1.proceed();
clone2.proceed();
return mi.proceed();
};
@SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return "haveBirthday".equals(m.getName());
}
};
pc.addAdvisor(advisor);
ITestBean it = (ITestBean) createProxy(pc);
final int age = 20;
it.setAge(age);
assertThat(it.getAge()).isEqualTo(age);
// Should return the age before the third, AOP-induced birthday
assertThat(it.haveBirthday()).isEqualTo((age + 2));
// Return the final age produced by 3 birthdays
assertThat(it.getAge()).isEqualTo((age + 3));
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.
/**
* Check that casting to Advised can't get around advice freeze.
*/
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() 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);
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(advised.getAdvisors().length).isEqualTo(1);
}
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);
}
Aggregations