Search in sources :

Example 16 with AdvisedSupport

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

the class AopProxyUtilsTests method testCompleteProxiedInterfacesWorksWithNull.

@Test
public void testCompleteProxiedInterfacesWorksWithNull() {
    AdvisedSupport as = new AdvisedSupport();
    Class<?>[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as);
    assertThat(completedInterfaces.length).isEqualTo(2);
    List<?> ifaces = Arrays.asList(completedInterfaces);
    assertThat(ifaces.contains(Advised.class)).isTrue();
    assertThat(ifaces.contains(StandardProxy.class)).isTrue();
}
Also used : AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 17 with AdvisedSupport

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

the class AopProxyUtilsTests method testCompleteProxiedInterfacesAdvisedNotIncluded.

@Test
public void testCompleteProxiedInterfacesAdvisedNotIncluded() {
    AdvisedSupport as = new AdvisedSupport();
    as.addInterface(ITestBean.class);
    as.addInterface(Comparable.class);
    Class<?>[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as);
    assertThat(completedInterfaces.length).isEqualTo(4);
    // Can't assume ordering for others, so use a list
    List<?> l = Arrays.asList(completedInterfaces);
    assertThat(l.contains(Advised.class)).isTrue();
    assertThat(l.contains(ITestBean.class)).isTrue();
    assertThat(l.contains(Comparable.class)).isTrue();
}
Also used : AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 18 with AdvisedSupport

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

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

use of cn.taketoday.aop.framework.AdvisedSupport 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)

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