Search in sources :

Example 66 with ITestBean

use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.

the class AroundAdviceBindingTestAspect method onSetUp.

@BeforeEach
public void onSetUp() throws Exception {
    ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
    AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));
    ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
    assertThat(AopUtils.isAopProxy(injectedTestBean)).isTrue();
    this.testBeanProxy = injectedTestBean;
    // we need the real target too, not just the proxy...
    this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
    mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
    aroundAdviceAspect.setCollaborator(mockCollaborator);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AroundAdviceBindingCollaborator(org.springframework.aop.aspectj.AroundAdviceBindingTestAspect.AroundAdviceBindingCollaborator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 67 with ITestBean

use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.

the class AfterReturningAdviceBindingTestAspect method testReturningBeanArray.

@Test
public void testReturningBeanArray() {
    this.testBeanTarget.setSpouse(new TestBean());
    ITestBean[] spouses = this.testBeanTarget.getSpouses();
    testBeanProxy.getSpouses();
    verify(mockCollaborator).testBeanArrayArg(spouses);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 68 with ITestBean

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());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 69 with ITestBean

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);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable)

Example 70 with ITestBean

use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCannotAddIntroductionAdviceWithUnimplementedInterface.

/**
 * Check that the introduction advice isn't allowed to introduce interfaces
 * that are unsupported by the IntroductionInterceptor.
 */
@Test
public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThatIllegalArgumentException().isThrownBy(() -> pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class)));
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Aggregations

ITestBean (org.springframework.beans.testfixture.beans.ITestBean)210 Test (org.junit.jupiter.api.Test)199 TestBean (org.springframework.beans.testfixture.beans.TestBean)121 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)44 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)31 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)23 Advisor (org.springframework.aop.Advisor)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)20 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)19 Method (java.lang.reflect.Method)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)15 IOException (java.io.IOException)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Advised (org.springframework.aop.framework.Advised)13