Search in sources :

Example 11 with AopProxy

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

the class AbstractAopProxyTests method testNoInterceptorsAndNoTarget.

@Test
public void testNoInterceptorsAndNoTarget() {
    assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> {
        AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
        // Add no interceptors
        AopProxy aop = createAopProxy(pc);
        aop.getProxy();
    });
}
Also used : AopConfigException(cn.taketoday.aop.framework.AopConfigException) AopProxy(cn.taketoday.aop.framework.AopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 12 with AopProxy

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

the class AbstractAopProxyTests method testUndeclaredUncheckedException.

@Test
public void testUndeclaredUncheckedException() throws Throwable {
    final RuntimeException unexpectedException = new RuntimeException();
    // 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(RuntimeException.class).isThrownBy(tb::getAge).matches(unexpectedException::equals);
}
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) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 13 with AopProxy

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

the class AbstractAopProxyTests method testContext.

/**
 * @param context if true, want context
 */
private void testContext(final boolean context) throws Throwable {
    final String s = "foo";
    // Test return value
    MethodInterceptor mi = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) {
            if (!context) {
                assertNoInvocationContext();
            } else {
                assertThat(ExposeInvocationInterceptor.currentInvocation()).as("have context").isNotNull();
            }
            return s;
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    if (context) {
        pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    }
    pc.addAdvice(mi);
    // Keep CGLIB happy
    if (requiresTarget()) {
        pc.setTarget(new TestBean());
    }
    AopProxy aop = createAopProxy(pc);
    assertNoInvocationContext();
    ITestBean tb = (ITestBean) aop.getProxy();
    assertNoInvocationContext();
    assertThat(tb.getName()).as("correct return value").isSameAs(s);
}
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) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport)

Example 14 with AopProxy

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

the class AbstractAopProxyTests method testDeclaredException.

@Test
public void testDeclaredException() throws Throwable {
    final Exception expectedException = new Exception();
    // Test return value
    MethodInterceptor mi = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw expectedException;
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    mockTargetSource.setTarget(new TestBean());
    pc.setTargetSource(mockTargetSource);
    AopProxy aop = createAopProxy(pc);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        ITestBean tb = (ITestBean) aop.getProxy();
        // Note: exception param below isn't used
        tb.exceptional(expectedException);
    }).matches(expectedException::equals);
}
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 15 with AopProxy

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

the class AbstractAopProxyTests method testTargetCanGetInvocationEvenIfNoAdviceChain.

/**
 * Check that although a method is eligible for advice chain optimization and
 * direct reflective invocation, it doesn't happen if we've asked to see the proxy,
 * so as to guarantee a consistent programming model.
 */
@Test
public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable {
    NeedsToSeeProxy target = new NeedsToSeeProxy();
    AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class);
    pc.setTarget(target);
    pc.setExposeProxy(true);
    // Now let's try it with the special target
    AopProxy aop = createAopProxy(pc);
    INeedsToSeeProxy proxied = (INeedsToSeeProxy) aop.getProxy();
    // It will complain if it can't get the proxy
    proxied.incrementViaProxy();
}
Also used : AopProxy(cn.taketoday.aop.framework.AopProxy) 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