Search in sources :

Example 26 with AdvisedSupport

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

the class AbstractAopProxyTests method testUndeclaredCheckedException.

/**
 * An interceptor throws a checked exception not on the method signature.
 * For efficiency, we don't bother unifying java.lang.reflect and
 * cn.taketoday.cglib UndeclaredThrowableException
 */
@Test
public void testUndeclaredCheckedException() throws Throwable {
    final Exception unexpectedException = new Exception();
    // Test return value
    MethodInterceptor mi = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw unexpectedException;
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(tb::getAge).satisfies(ex -> assertThat(ex.getUndeclaredThrowable()).isEqualTo(unexpectedException));
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) AopProxy(cn.taketoday.aop.framework.AopProxy) MethodInvocation(org.aopalliance.intercept.MethodInvocation) 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) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 27 with AdvisedSupport

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

the class JdkDynamicAopProxyTests method testEqualsAndHashCodeDefined.

@Test
public void testEqualsAndHashCodeDefined() {
    AdvisedSupport as = new AdvisedSupport(Named.class);
    as.setTarget(new Person());
    JdkDynamicAopProxy aopProxy = new JdkDynamicAopProxy(as);
    Named proxy = (Named) aopProxy.getProxy();
    Named named = new Person();
    assertThat(proxy).isEqualTo(named);
    assertThat(named.hashCode()).isEqualTo(proxy.hashCode());
}
Also used : JdkDynamicAopProxy(cn.taketoday.aop.framework.JdkDynamicAopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 28 with AdvisedSupport

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

the class UnsupportedInterceptor method testUnadvisedProxyCreationWithCallDuringConstructor.

@Test
public void testUnadvisedProxyCreationWithCallDuringConstructor() {
    CglibTestBean target = new CglibTestBean();
    target.setName("Rob Harrop");
    AdvisedSupport pc = new AdvisedSupport();
    pc.setFrozen(true);
    pc.setTarget(target);
    CglibAopProxy aop = new CglibAopProxy(pc);
    CglibTestBean proxy = (CglibTestBean) aop.getProxy();
    assertThat(proxy).as("Proxy should not be null").isNotNull();
    assertThat(proxy.getName()).as("Constructor overrode the value of name").isEqualTo("Rob Harrop");
}
Also used : CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 29 with AdvisedSupport

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

the class UnsupportedInterceptor method testToStringInvocation.

@Test
public void testToStringInvocation() {
    PrivateCglibTestBean bean = new PrivateCglibTestBean();
    bean.setName("Rob Harrop");
    AdvisedSupport as = new AdvisedSupport();
    as.setTarget(bean);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    PrivateCglibTestBean proxy = (PrivateCglibTestBean) aop.getProxy();
    assertThat(proxy.toString()).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 30 with AdvisedSupport

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

the class UnsupportedInterceptor method testProxyAProxyWithAdditionalInterface.

@Test
public void testProxyAProxyWithAdditionalInterface() {
    ITestBean target = new TestBean();
    mockTargetSource.setTarget(target);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    as.addInterface(Serializable.class);
    CglibAopProxy cglib = new CglibAopProxy(as);
    ITestBean proxy1 = (ITestBean) cglib.getProxy();
    mockTargetSource.setTarget(proxy1);
    as = new AdvisedSupport(new Class<?>[] {});
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    cglib = new CglibAopProxy(as);
    ITestBean proxy2 = (ITestBean) cglib.getProxy();
    assertThat(proxy2 instanceof Serializable).isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) CglibAopProxy(cn.taketoday.aop.framework.CglibAopProxy) NopInterceptor(cn.taketoday.aop.NopInterceptor) Serializable(java.io.Serializable) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Aggregations

AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)34 Test (org.junit.jupiter.api.Test)27 AopProxy (cn.taketoday.aop.framework.AopProxy)17 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)12 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)11 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 NopInterceptor (cn.taketoday.aop.NopInterceptor)8 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 AopConfigException (cn.taketoday.aop.framework.AopConfigException)5 JdkDynamicAopProxy (cn.taketoday.aop.framework.JdkDynamicAopProxy)4 TargetSource (cn.taketoday.aop.TargetSource)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 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