Search in sources :

Example 11 with NopInterceptor

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

the class UnsupportedInterceptor method testPackageMethodInvocation.

@Test
public void testPackageMethodInvocation() {
    PackageMethodTestBean bean = new PackageMethodTestBean();
    bean.value = "foo";
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy();
    assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
    assertThat(bean.getClass().getClassLoader()).isEqualTo(proxy.getClass().getClassLoader());
    assertThat(proxy.getString()).isEqualTo("foo");
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) 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)

Example 12 with NopInterceptor

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

the class UnsupportedInterceptor method testProtectedMethodInvocation.

@Test
public void testProtectedMethodInvocation() {
    ProtectedMethodTestBean bean = new ProtectedMethodTestBean();
    bean.value = "foo";
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    ProtectedMethodTestBean proxy = (ProtectedMethodTestBean) aop.getProxy();
    assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
    assertThat(bean.getClass().getClassLoader()).isEqualTo(proxy.getClass().getClassLoader());
    assertThat(proxy.getString()).isEqualTo("foo");
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) 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)

Example 13 with NopInterceptor

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

the class UnsupportedInterceptor method testExceptionHandling.

@Test
public void testExceptionHandling() {
    ExceptionThrower bean = new ExceptionThrower();
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();
    try {
        proxy.doTest();
    } catch (Exception ex) {
        assertThat(ex instanceof ApplicationContextException).as("Invalid exception class").isTrue();
    }
    assertThat(proxy.isCatchInvoked()).as("Catch was not invoked").isTrue();
    assertThat(proxy.isFinallyInvoked()).as("Finally was not invoked").isTrue();
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) NopInterceptor(cn.taketoday.aop.NopInterceptor) CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) AopProxy(cn.taketoday.aop.framework.AopProxy) ApplicationContextException(cn.taketoday.context.ApplicationContextException) AopConfigException(cn.taketoday.aop.framework.AopConfigException) ApplicationContextException(cn.taketoday.context.ApplicationContextException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 14 with NopInterceptor

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

the class UnsupportedInterceptor method testMethodInvocationDuringConstructor.

@Test
public void testMethodInvocationDuringConstructor() {
    CglibTestBean bean = new CglibTestBean();
    bean.setName("Rob Harrop");
    AdvisedSupport as = new AdvisedSupport();
    as.setTarget(bean);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    CglibTestBean proxy = (CglibTestBean) aop.getProxy();
    assertThat(proxy.getName()).as("The name property has been overwritten by the constructor").isEqualTo("Rob Harrop");
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) 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)

Example 15 with NopInterceptor

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

the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.

/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() 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 add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(advised.getAdvisors().length).isEqualTo(1);
}
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) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) 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