Search in sources :

Example 6 with AopProxy

use of cn.taketoday.aop.framework.AopProxy 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 7 with AopProxy

use of cn.taketoday.aop.framework.AopProxy 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 8 with AopProxy

use of cn.taketoday.aop.framework.AopProxy 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 9 with AopProxy

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

the class UnsupportedInterceptor method testProxyCanBeClassNotInterface.

@Test
public void testProxyCanBeClassNotInterface() {
    TestBean raw = new TestBean();
    raw.setAge(32);
    mockTargetSource.setTarget(raw);
    AdvisedSupport pc = new AdvisedSupport();
    pc.setTargetSource(mockTargetSource);
    AopProxy aop = new CglibAopProxy(pc);
    Object proxy = aop.getProxy();
    assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
    assertThat(proxy instanceof ITestBean).isTrue();
    assertThat(proxy instanceof TestBean).isTrue();
    TestBean tb = (TestBean) proxy;
    assertThat(tb.getAge()).isEqualTo(32);
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) 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 10 with AopProxy

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

the class JdkDynamicAopProxyTests method testInterceptorIsInvokedWithNoTarget.

@Test
public void testInterceptorIsInvokedWithNoTarget() {
    // Test return value
    final int age = 25;
    MethodInterceptor mi = (invocation -> age);
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(mi);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    assertThat(tb.getAge()).as("correct return value").isEqualTo(age);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AopProxy(cn.taketoday.aop.framework.AopProxy) JdkDynamicAopProxy(cn.taketoday.aop.framework.JdkDynamicAopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Aggregations

AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)17 AopProxy (cn.taketoday.aop.framework.AopProxy)17 Test (org.junit.jupiter.api.Test)16 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)9 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)7 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 NopInterceptor (cn.taketoday.aop.NopInterceptor)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)6 AopConfigException (cn.taketoday.aop.framework.AopConfigException)5 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 JdkDynamicAopProxy (cn.taketoday.aop.framework.JdkDynamicAopProxy)2 LockedException (cn.taketoday.aop.mixin.LockedException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 MarshalException (java.rmi.MarshalException)2 SQLException (java.sql.SQLException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 ApplicationContextException (cn.taketoday.context.ApplicationContextException)1