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());
}
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());
}
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);
}
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());
}
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);
}
Aggregations