Search in sources :

Example 1 with HotSwappableTargetSource

use of cn.taketoday.aop.target.HotSwappableTargetSource in project today-infrastructure 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.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) Test(org.junit.jupiter.api.Test)

Example 2 with HotSwappableTargetSource

use of cn.taketoday.aop.target.HotSwappableTargetSource 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 3 with HotSwappableTargetSource

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

the class BeanFactoryTransactionTests method testDynamicTargetSource.

/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() {
    // Install facade
    CallCountingTransactionManager txMan = new CallCountingTransactionManager();
    PlatformTransactionManagerFacade.delegate = txMan;
    TestBean tb = (TestBean) factory.getBean("hotSwapped");
    assertThat(tb.getAge()).isEqualTo(666);
    int newAge = 557;
    tb.setAge(newAge);
    assertThat(tb.getAge()).isEqualTo(newAge);
    TestBean target2 = new TestBean();
    target2.setAge(65);
    HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
    ts.swap(target2);
    assertThat(tb.getAge()).isEqualTo(target2.getAge());
    tb.setAge(newAge);
    assertThat(target2.getAge()).isEqualTo(newAge);
    assertThat(txMan.inflight).isEqualTo(0);
    assertThat(txMan.commits).isEqualTo(2);
    assertThat(txMan.rollbacks).isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 4 with HotSwappableTargetSource

use of cn.taketoday.aop.target.HotSwappableTargetSource 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.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) Test(org.junit.jupiter.api.Test)

Example 5 with HotSwappableTargetSource

use of cn.taketoday.aop.target.HotSwappableTargetSource in project today-infrastructure by TAKETODAY.

the class BeanFactoryTransactionTests method testDynamicTargetSource.

/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() {
    // Install facade
    CallCountingTransactionManager txMan = new CallCountingTransactionManager();
    PlatformTransactionManagerFacade.delegate = txMan;
    TestBean tb = (TestBean) factory.getBean("hotSwapped");
    assertThat(tb.getAge()).isEqualTo(666);
    int newAge = 557;
    tb.setAge(newAge);
    assertThat(tb.getAge()).isEqualTo(newAge);
    TestBean target2 = new TestBean();
    target2.setAge(65);
    HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
    ts.swap(target2);
    assertThat(tb.getAge()).isEqualTo(target2.getAge());
    tb.setAge(newAge);
    assertThat(target2.getAge()).isEqualTo(newAge);
    assertThat(txMan.inflight).isEqualTo(0);
    assertThat(txMan.commits).isEqualTo(2);
    assertThat(txMan.rollbacks).isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)5 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 Test (org.junit.jupiter.api.Test)5 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)3 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)2 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)2 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)2 CallCountingTransactionManager (cn.taketoday.transaction.CallCountingTransactionManager)2 NopInterceptor (cn.taketoday.aop.NopInterceptor)1 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)1 Advised (cn.taketoday.aop.framework.Advised)1 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)1