use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class CountingAspectJAdvice method testIsProxy.
@Test
public void testIsProxy() throws Exception {
ITestBean bean = getTestBean();
assertThat(AopUtils.isAopProxy(bean)).as("Bean is not a proxy").isTrue();
// check the advice details
Advised advised = (Advised) bean;
Advisor[] advisors = advised.getAdvisors();
assertThat(advisors.length > 0).as("Advisors should not be empty").isTrue();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class CountingAspectJAdvice method testAspectAppliedForInitializeBeanWithEmptyName.
@Test
public void testAspectAppliedForInitializeBeanWithEmptyName() {
ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), "");
CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(0);
assertThat(advice.getAfterCount()).as("Incorrect after count").isEqualTo(0);
bean.setName("Sally");
assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(1);
assertThat(advice.getAfterCount()).as("Incorrect after count").isEqualTo(1);
bean.getName();
assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(1);
assertThat(advice.getAfterCount()).as("Incorrect after count").isEqualTo(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);
}
Aggregations