Search in sources :

Example 1 with DebugInterceptor

use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.

@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
    final TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return TestBean.class;
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() throws Exception {
            assertThat(AopContext.currentProxy()).isEqualTo(proxy);
            return target;
        }

        @Override
        public void releaseTarget(Object target) throws Exception {
        }
    });
    // Just test anything: it will fail if context wasn't found
    assertThat(proxy.getAge()).isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) 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 DebugInterceptor

use of cn.taketoday.aop.interceptor.DebugInterceptor 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)

Example 3 with DebugInterceptor

use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.

the class ProxyFactoryBeanTests method testDetectsInterfaces.

@Test
public void testDetectsInterfaces() {
    ProxyFactoryBean fb = new ProxyFactoryBean();
    fb.setTarget(new TestBean());
    fb.addAdvice(new DebugInterceptor());
    fb.setBeanFactory(new StandardBeanFactory());
    ITestBean proxy = (ITestBean) fb.getObject();
    assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 4 with DebugInterceptor

use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.

the class ProxyFactoryBeanTests method testWithInterceptorNames.

@Test
public void testWithInterceptorNames() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerSingleton("debug", new DebugInterceptor());
    ProxyFactoryBean fb = new ProxyFactoryBean();
    fb.setTarget(new TestBean());
    fb.setInterceptorNames("debug");
    fb.setBeanFactory(bf);
    Advised proxy = (Advised) fb.getObject();
    assertThat(proxy.getAdvisorCount()).isEqualTo(1);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 5 with DebugInterceptor

use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
 * Try adding and removing interfaces and interceptors on prototype.
 * Changes will only affect future references obtained from the factory.
 * Each instance will be independent.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(TimeStamped.class);
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head 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) factory.getBean("test2");
    assertThat(ts.getTimeStamp()).isEqualTo(time);
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    // Check no change on existing object reference
    assertThat(ts.getTimeStamp() == time).isTrue();
    assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
    // 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();
    // Won't affect existing reference
    assertThat(debugInterceptor.getCount() == 0).isTrue();
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked with old reference
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // Our own timestamped reference should still work
    assertThat(ts.getTimeStamp()).isEqualTo(time);
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)16 Test (org.junit.jupiter.api.Test)16 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)14 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)12 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)6 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)5 TimestampIntroductionInterceptor (cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor)4 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)4 TargetSource (cn.taketoday.aop.TargetSource)3 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)3 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)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 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2