Search in sources :

Example 81 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 82 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 83 with ITestBean

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

the class AbstractAopProxyTests method testAdviceSupportListeners.

@Test
public void testAdviceSupportListeners() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    CountingAdvisorListener l = new CountingAdvisorListener(pc);
    pc.addListener(l);
    RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
    // Should be automatically added as a listener
    pc.addListener(acf);
    assertThat(pc.isActive()).isFalse();
    assertThat(l.activates).isEqualTo(0);
    assertThat(acf.refreshes).isEqualTo(0);
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(acf.refreshes).isEqualTo(1);
    assertThat(l.activates).isEqualTo(1);
    assertThat(pc.isActive()).isTrue();
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(l.adviceChanges).isEqualTo(0);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    assertThat(l.adviceChanges).isEqualTo(1);
    assertThat(acf.refreshes).isEqualTo(2);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.removeAdvice(di);
    assertThat(l.adviceChanges).isEqualTo(2);
    assertThat(acf.refreshes).isEqualTo(3);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.getProxy();
    assertThat(l.activates).isEqualTo(1);
    pc.removeListener(l);
    assertThat(l.adviceChanges).isEqualTo(2);
    pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
    // No longer counting
    assertThat(l.adviceChanges).isEqualTo(2);
}
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) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 84 with ITestBean

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

the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.

@Test
public void testCannotAddInterceptorWhenFrozen() 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);
    assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() -> pc.addAdvice(0, new NopInterceptor())).withMessageContaining("frozen");
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(((Advised) proxied).getAdvisors().length).isEqualTo(1);
}
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 85 with ITestBean

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

the class SimpleBeforeAdviceInterceptor method testAdvisorAdapterRegistrationManagerPresentInContext.

@Test
public void testAdvisorAdapterRegistrationManagerPresentInContext() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-with-bpp.xml", getClass());
    ITestBean tb = (ITestBean) ctx.getBean("testBean");
    // just invoke any method to see if advice fired
    tb.getName();
    getAdviceImpl(tb).getInvocationCounter();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) 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