Search in sources :

Example 1 with NopInterceptor

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

the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.

@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    ITestBean t = (ITestBean) createProxy(pc);
    assertThat(di.getCount()).isEqualTo(0);
    t.setAge(23);
    assertThat(t.getAge()).isEqualTo(23);
    assertThat(di.getCount()).isEqualTo(2);
    Advised advised = (Advised) t;
    assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
    assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(1);
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(2);
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertThat(cba.getCalls()).isEqualTo(0);
    advised.addAdvice(cba);
    t.setAge(16);
    assertThat(t.getAge()).isEqualTo(16);
    assertThat(cba.getCalls()).isEqualTo(2);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 2 with NopInterceptor

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

the class AbstractAopProxyTests method testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters.

@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    // Could apply dynamically to getAge/setAge but not to getName
    TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
    pc.addAdvisor(dp);
    this.mockTargetSource.setTarget(tb);
    pc.setTargetSource(mockTargetSource);
    ITestBean it = (ITestBean) createProxy(pc);
    assertThat(dp.count).isEqualTo(0);
    it.getAge();
    // Statically vetoed
    assertThat(dp.count).isEqualTo(0);
    it.setAge(11);
    assertThat(it.getAge()).isEqualTo(11);
    assertThat(dp.count).isEqualTo(1);
    // Applies statically but not dynamically
    it.setName("joe");
    assertThat(dp.count).isEqualTo(1);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 3 with NopInterceptor

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

the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.

@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    CountingBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
    pc.addAdvisor(advisor);
    assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
    pc.setOpaque(true);
    assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
    ITestBean proxied = (ITestBean) createProxy(pc);
    proxied.setAge(10);
    assertThat(proxied.getAge()).isEqualTo(10);
    assertThat(mba.getCalls()).isEqualTo(1);
    boolean condition = proxied instanceof Advised;
    assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Advisor(cn.taketoday.aop.Advisor) LockMixinAdvisor(cn.taketoday.aop.mixin.LockMixinAdvisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 4 with NopInterceptor

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

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertThat(0).isEqualTo(nop.getCount());
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    assertThat(1).isEqualTo(nop.getCount());
    // Change to a new static target
    pc.setTarget(tb2);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
    assertThat(2).isEqualTo(nop.getCount());
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertThat(proxy.getAge()).isEqualTo(tb3.getAge());
    assertThat(3).isEqualTo(nop.getCount());
    hts.swap(tb1);
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    tb1.setName("Colin");
    assertThat(proxy.getName()).isEqualTo(tb1.getName());
    assertThat(5).isEqualTo(nop.getCount());
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertThat(advised.getTargetSource()).isSameAs(hts);
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertThat(proxy.getName()).isEqualTo(tb2.getName());
    assertThat(advised.getTargetSource()).isSameAs(sts);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 5 with NopInterceptor

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

the class AbstractAopProxyTests method testAdviceSupportListeners.

@Test
public void testAdviceSupportListeners() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    CountingAdvisorListener l = new CountingAdvisorListener(pc);
    pc.addListener(l);
    RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
    // Should be automatically added as a listener
    pc.addListener(acf);
    assertThat(pc.isActive()).isFalse();
    assertThat(l.activates).isEqualTo(0);
    assertThat(acf.refreshes).isEqualTo(0);
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(acf.refreshes).isEqualTo(1);
    assertThat(l.activates).isEqualTo(1);
    assertThat(pc.isActive()).isTrue();
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(l.adviceChanges).isEqualTo(0);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    assertThat(l.adviceChanges).isEqualTo(1);
    assertThat(acf.refreshes).isEqualTo(2);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.removeAdvice(di);
    assertThat(l.adviceChanges).isEqualTo(2);
    assertThat(acf.refreshes).isEqualTo(3);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.getProxy();
    assertThat(l.activates).isEqualTo(1);
    pc.removeListener(l);
    assertThat(l.adviceChanges).isEqualTo(2);
    pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
    // No longer counting
    assertThat(l.adviceChanges).isEqualTo(2);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (cn.taketoday.aop.NopInterceptor)47 Test (org.junit.jupiter.api.Test)46 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)39 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)30 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)11 Advisor (cn.taketoday.aop.Advisor)10 Advised (cn.taketoday.aop.framework.Advised)9 AopConfigException (cn.taketoday.aop.framework.AopConfigException)9 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)8 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)8 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)7 AopProxy (cn.taketoday.aop.framework.AopProxy)6 LockMixinAdvisor (cn.taketoday.aop.mixin.LockMixinAdvisor)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 CountingAfterReturningAdvice (cn.taketoday.aop.CountingAfterReturningAdvice)4 IJmxTestBean (cn.taketoday.jmx.IJmxTestBean)4