Search in sources :

Example 1 with AopProxy

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

the class AbstractAopProxyTests method testTargetCanGetInvocation.

@Test
public void testTargetCanGetInvocation() throws Throwable {
    final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    TrapTargetInterceptor tii = new TrapTargetInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            // Assert that target matches BEFORE invocation returns
            assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
            return super.invoke(invocation);
        }
    };
    pc.addAdvice(tii);
    pc.setTarget(expectedTarget);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    tb.getName();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopProxy(cn.taketoday.aop.framework.AopProxy) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 2 with AopProxy

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

the class AbstractAopProxyTests method testUserAttributes.

@Test
public void testUserAttributes() throws Throwable {
    class MapAwareMethodInterceptor implements MethodInterceptor {

        private final Map<String, String> expectedValues;

        private final Map<String, String> valuesToAdd;

        public MapAwareMethodInterceptor(Map<String, String> expectedValues, Map<String, String> valuesToAdd) {
            this.expectedValues = expectedValues;
            this.valuesToAdd = valuesToAdd;
        }

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            DefaultMethodInvocation rmi = (DefaultMethodInvocation) invocation;
            for (Iterator<String> it = rmi.getAttributes().keySet().iterator(); it.hasNext(); ) {
                Object key = it.next();
                assertThat(rmi.getAttributes().get(key)).isEqualTo(expectedValues.get(key));
            }
            rmi.getAttributes().putAll(valuesToAdd);
            return invocation.proceed();
        }
    }
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<>(), new HashMap<String, String>());
    Map<String, String> firstValuesToAdd = new HashMap<>();
    firstValuesToAdd.put("test", "");
    MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap<>(), firstValuesToAdd);
    MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    Map<String, String> secondValuesToAdd = new HashMap<>();
    secondValuesToAdd.put("foo", "bar");
    secondValuesToAdd.put("cat", "dog");
    MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
    Map<String, String> finalExpected = new HashMap<>(firstValuesToAdd);
    finalExpected.putAll(secondValuesToAdd);
    MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
    pc.addAdvice(mami1);
    pc.addAdvice(mami2);
    pc.addAdvice(mami3);
    pc.addAdvice(mami4);
    pc.addAdvice(mami5);
    pc.addAdvice(mami6);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    String newName = "foo";
    tb.setName(newName);
    assertThat(tb.getName()).isEqualTo(newName);
}
Also used : HashMap(java.util.HashMap) MethodInvocation(org.aopalliance.intercept.MethodInvocation) 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) Map(java.util.Map) HashMap(java.util.HashMap) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 3 with AopProxy

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

the class JdkDynamicAopProxyTests method testTargetCanGetInvocationWithPrivateClass.

@Test
public void testTargetCanGetInvocationWithPrivateClass() {
    final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {

        @Override
        protected void assertions(MethodInvocation invocation) {
            assertThat(invocation.getThis()).isEqualTo(this);
            assertThat(invocation.getMethod().getDeclaringClass()).as("Invocation should be on ITestBean: " + invocation.getMethod()).isEqualTo(ITestBean.class);
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    TrapTargetInterceptor tii = new TrapTargetInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            // Assert that target matches BEFORE invocation returns
            assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
            return super.invoke(invocation);
        }
    };
    pc.addAdvice(tii);
    pc.setTarget(expectedTarget);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    tb.getName();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopProxy(cn.taketoday.aop.framework.AopProxy) JdkDynamicAopProxy(cn.taketoday.aop.framework.JdkDynamicAopProxy) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 4 with AopProxy

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

the class UnsupportedInterceptor method testNoTarget.

@Test
public void testNoTarget() {
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    AopProxy aop = createAopProxy(pc);
    assertThatExceptionOfType(AopConfigException.class).isThrownBy(aop::getProxy);
}
Also used : AopConfigException(cn.taketoday.aop.framework.AopConfigException) 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 5 with AopProxy

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

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