Search in sources :

Example 1 with DefaultIntroductionAdvisor

use of cn.taketoday.aop.support.DefaultIntroductionAdvisor in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testRejectsBogusDynamicIntroductionAdviceWithNoAdapter.

@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        // TODO May fail on either call: may want to tighten up definition
        ITestBean proxied = (ITestBean) createProxy(pc);
        proxied.getName();
    });
// TODO used to catch UnknownAdviceTypeException, but
// with CGLIB some errors are in proxy creation and are wrapped
// in aspect exception. Error message is still fine.
// assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) 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) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultIntroductionAdvisor

use of cn.taketoday.aop.support.DefaultIntroductionAdvisor in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testIntroductionThrowsUncheckedException.

/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    @SuppressWarnings("serial")
    class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {

        /**
         * @see TimeStamped#getTimeStamp()
         */
        @Override
        public long getTimeStamp() {
            throw new UnsupportedOperationException();
        }
    }
    pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
    TimeStamped ts = (TimeStamped) createProxy(pc);
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(ts::getTimeStamp);
}
Also used : DelegatingIntroductionInterceptor(cn.taketoday.aop.support.DelegatingIntroductionInterceptor) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Pointcuts(cn.taketoday.aop.support.Pointcuts) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultIntroductionAdvisor

use of cn.taketoday.aop.support.DefaultIntroductionAdvisor in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testCannotAddIntroductionAdviceWithUnimplementedInterface.

/**
 * Check that the introduction advice isn't allowed to introduce interfaces
 * that are unsupported by the IntroductionInterceptor.
 */
@Test
public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThatIllegalArgumentException().isThrownBy(() -> pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class)));
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultIntroductionAdvisor

use of cn.taketoday.aop.support.DefaultIntroductionAdvisor in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testUseAsHashKey.

@Test
public void testUseAsHashKey() {
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvice(new NopInterceptor());
    ITestBean proxy1 = (ITestBean) createProxy(pf1);
    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
    ITestBean proxy2 = (ITestBean) createProxy(pf2);
    HashMap<ITestBean, Object> h = new HashMap<>();
    Object value1 = "foo";
    Object value2 = "bar";
    assertThat(h.get(proxy1)).isNull();
    h.put(proxy1, value1);
    h.put(proxy2, value2);
    assertThat(value1).isEqualTo(h.get(proxy1));
    assertThat(value2).isEqualTo(h.get(proxy2));
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HashMap(java.util.HashMap) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultIntroductionAdvisor

use of cn.taketoday.aop.support.DefaultIntroductionAdvisor in project today-infrastructure by TAKETODAY.

the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.

/**
 * Should see effect immediately on behavior.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
    ProxyFactory config = new ProxyFactory(new TestBean());
    assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to front of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
    TimeStamped ts = (TimeStamped) config.getProxy();
    assertThat(ts.getTimeStamp() == time).isTrue();
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    assertThatExceptionOfType(RuntimeException.class).as("Existing object won't implement this interface any more").isThrownBy(// Existing reference will fail
    ts::getTimeStamp);
    assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // not invoked again
    assertThat(debugInterceptor.getCount() == 1).isTrue();
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)23 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)23 Test (org.junit.jupiter.api.Test)23 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)21 TimestampIntroductionInterceptor (cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor)12 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)8 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)7 DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)5 DelegatingIntroductionInterceptor (cn.taketoday.aop.support.DelegatingIntroductionInterceptor)3 Pointcuts (cn.taketoday.aop.support.Pointcuts)3 IOther (cn.taketoday.beans.testfixture.beans.IOther)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 MarshalException (java.rmi.MarshalException)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)2