Search in sources :

Example 1 with CountingBeforeAdvice

use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with CountingBeforeAdvice

use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testBeforeAdviceThrowsException.

@Test
public void testBeforeAdviceThrowsException() {
    final RuntimeException rex = new RuntimeException();
    @SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {

        @Override
        public void before(MethodInvocation invocation) throws Throwable {
            super.before(invocation);
            if (invocation.getMethod().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(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 3 with CountingBeforeAdvice

use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) IOException(java.io.IOException) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) Person(cn.taketoday.beans.testfixture.beans.Person) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 4 with CountingBeforeAdvice

use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.

the class CountingAspectJAdvice method testAdviceInvokedCorrectly.

@Test
public void testAdviceInvokedCorrectly() throws Exception {
    CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
    CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");
    ITestBean bean = getTestBean();
    assertThat(getAgeCounter.getCalls("getAge")).as("Incorrect initial getAge count").isEqualTo(0);
    assertThat(getNameCounter.getCalls("getName")).as("Incorrect initial getName count").isEqualTo(0);
    bean.getAge();
    assertThat(getAgeCounter.getCalls("getAge")).as("Incorrect getAge count on getAge counter").isEqualTo(1);
    assertThat(getNameCounter.getCalls("getAge")).as("Incorrect getAge count on getName counter").isEqualTo(0);
    bean.getName();
    assertThat(getNameCounter.getCalls("getName")).as("Incorrect getName count on getName counter").isEqualTo(1);
    assertThat(getAgeCounter.getCalls("getName")).as("Incorrect getName count on getAge counter").isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 5 with CountingBeforeAdvice

use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.

the class ProxyFactoryTests method testRemoveAdvisorByReference.

@Test
public void testRemoveAdvisorByReference() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(pf.removeAdvisor(advisor)).isTrue();
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Advisor(cn.taketoday.aop.Advisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Aggregations

CountingBeforeAdvice (cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice)34 Test (org.junit.jupiter.api.Test)32 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)26 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)22 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)22 Advisor (cn.taketoday.aop.Advisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)14 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)12 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 NameMatchMethodPointcut (cn.taketoday.aop.support.NameMatchMethodPointcut)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 XmlBeanDefinitionReader (cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader)4 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)2 CountingAfterReturningAdvice (cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice)2 MyThrowsHandler (cn.taketoday.aop.testfixture.advice.MyThrowsHandler)2 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)2 BeanFactory (cn.taketoday.beans.factory.BeanFactory)2