Search in sources :

Example 31 with ITestBean

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);
}
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 32 with ITestBean

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());
}
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) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 33 with ITestBean

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());
}
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 34 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 35 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)

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