Search in sources :

Example 6 with NopInterceptor

use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.

the class AbstractAopProxyTests method testCannotRemoveAdvisorWhenFrozen.

@Test
public void testCannotRemoveAdvisorWhenFrozen() 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);
    Advised advised = (Advised) proxied;
    assertThat(pc.isFrozen()).isTrue();
    assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to remove Advisor when frozen").isThrownBy(() -> advised.removeAdvisor(0)).withMessageContaining("frozen");
    // Didn't get removed
    assertThat(advised.getAdvisors().length).isEqualTo(1);
    pc.setFrozen(false);
    // Can now remove it
    advised.removeAdvisor(0);
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(advised.getAdvisors().length).isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopConfigException(cn.taketoday.aop.framework.AopConfigException) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 7 with NopInterceptor

use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.

the class AbstractAopProxyTests method testAfterReturningAdvisorIsNotInvokedOnException.

@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
    CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvice(car);
    assertThat(pf.getAdvisors()[1].getAdvice()).as("Advice was wrapped in Advisor and added").isEqualTo(car);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(car.getCalls()).isEqualTo(0);
    int age = 10;
    proxied.setAge(age);
    assertThat(proxied.getAge()).isEqualTo(age);
    assertThat(car.getCalls()).isEqualTo(2);
    Exception exc = new Exception();
    // On exception it won't be invoked
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> proxied.exceptional(exc)).satisfies(ex -> assertThat(ex).isSameAs(exc));
    assertThat(car.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 8 with NopInterceptor

use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.

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(cn.taketoday.aop.NopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 9 with NopInterceptor

use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.

the class ProxyFactoryTests method testRemoveAdvisorByIndex.

@Test
public void testRemoveAdvisorByIndex() {
    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);
    NopInterceptor nop2 = new NopInterceptor();
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(nop2.getCount()).isEqualTo(1);
    // Removes counting before advisor
    pf.removeAdvisor(1);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(2);
    // Removes Nop1
    pf.removeAdvisor(0);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(3);
    // Check out of bounds
    try {
        pf.removeAdvisor(-1);
    } catch (AopConfigException ex) {
    // Ok
    }
    try {
        pf.removeAdvisor(2);
    } catch (AopConfigException ex) {
    // Ok
    }
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(4);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopConfigException(cn.taketoday.aop.framework.AopConfigException) NopInterceptor(cn.taketoday.aop.NopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advisor(cn.taketoday.aop.Advisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 10 with NopInterceptor

use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.

the class UnsupportedInterceptor method testNoTarget.

@Test
public void testNoTarget() {
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    AopProxy aop = createAopProxy(pc);
    assertThatExceptionOfType(AopConfigException.class).isThrownBy(aop::getProxy);
}
Also used : AopConfigException(cn.taketoday.aop.framework.AopConfigException) NopInterceptor(cn.taketoday.aop.NopInterceptor) CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) AopProxy(cn.taketoday.aop.framework.AopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (cn.taketoday.aop.NopInterceptor)47 Test (org.junit.jupiter.api.Test)46 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)39 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)30 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)11 Advisor (cn.taketoday.aop.Advisor)10 Advised (cn.taketoday.aop.framework.Advised)9 AopConfigException (cn.taketoday.aop.framework.AopConfigException)9 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)8 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)8 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)7 AopProxy (cn.taketoday.aop.framework.AopProxy)6 LockMixinAdvisor (cn.taketoday.aop.mixin.LockMixinAdvisor)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 CountingAfterReturningAdvice (cn.taketoday.aop.CountingAfterReturningAdvice)4 IJmxTestBean (cn.taketoday.jmx.IJmxTestBean)4