Search in sources :

Example 1 with CountingBeforeAdvice

use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
    assertThat(pf.indexOf(nop)).isEqualTo(0);
    assertThat(pf.indexOf(advisor)).isEqualTo(1);
    assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with CountingBeforeAdvice

use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice 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 3 with CountingBeforeAdvice

use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.

@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    ITestBean t = (ITestBean) createProxy(pc);
    assertThat(di.getCount()).isEqualTo(0);
    t.setAge(23);
    assertThat(t.getAge()).isEqualTo(23);
    assertThat(di.getCount()).isEqualTo(2);
    Advised advised = (Advised) t;
    assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
    assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(1);
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(2);
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertThat(cba.getCalls()).isEqualTo(0);
    advised.addAdvice(cba);
    t.setAge(16);
    assertThat(t.getAge()).isEqualTo(16);
    assertThat(cba.getCalls()).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) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 4 with CountingBeforeAdvice

use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testSerializableTargetAndAdvice.

@Test
public void testSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
    ProxyFactory pf = new ProxyFactory(personTarget);
    CountingThrowsAdvice cta = new CountingThrowsAdvice();
    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();
    p.echo(null);
    assertThat(cta.getCalls()).isEqualTo(0);
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertThat(cta.getCalls()).isEqualTo(1);
    // Will throw exception if it fails
    Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo(p.getName());
    assertThat(p2.getAge()).isEqualTo(p.getAge());
    assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
    assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(0);
    a2.addAdvice(ni);
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(1);
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertThat(cta.getCalls()).isEqualTo(1);
    try {
        p2.echo(new IOException());
    } catch (IOException ex) {
    }
    assertThat(cta.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) IOException(java.io.IOException) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Person(org.springframework.beans.testfixture.beans.Person) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 5 with CountingBeforeAdvice

use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testAddAdviceAtRuntime.

@Test
public void testAddAdviceAtRuntime() {
    TestBean bean = new TestBean();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(bean);
    pf.setFrozen(false);
    pf.setOpaque(false);
    pf.setProxyTargetClass(true);
    TestBean proxy = (TestBean) pf.getProxy();
    assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
    proxy.getAge();
    assertThat(cba.getCalls()).isEqualTo(0);
    ((Advised) proxy).addAdvice(cba);
    proxy.getAge();
    assertThat(cba.getCalls()).isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Aggregations

CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)17 Test (org.junit.jupiter.api.Test)16 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 TestBean (org.springframework.beans.testfixture.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)6 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3 LockMixinAdvisor (test.mixin.LockMixinAdvisor)3 Method (java.lang.reflect.Method)2 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Assertions.assertThatIOException (org.assertj.core.api.Assertions.assertThatIOException)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1