use of cn.taketoday.beans.testfixture.beans.DerivedTestBean in project today-infrastructure by TAKETODAY.
the class ConcurrencyThrottleInterceptorTests method testSerializable.
@Test
public void testSerializable() throws Exception {
DerivedTestBean tb = new DerivedTestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(ITestBean.class);
ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
proxyFactory.addAdvice(cti);
proxyFactory.setTarget(tb);
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
proxy.getAge();
ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
Advised advised = (Advised) serializedProxy;
ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
serializedProxy.getAge();
}
use of cn.taketoday.beans.testfixture.beans.DerivedTestBean in project today-infrastructure by TAKETODAY.
the class DataBinderTests method directBindingToEmptyIndexedFieldWithRegisteredGenericEditor.
@Test
void directBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
Errors errors = binder.getBindingResult();
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
// This next code is only generated because of the registered editor, using the
// registered type of the editor as guess for the content type of the collection.
assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL.cn.taketoday.beans.testfixture.beans.TestBean");
assertThat(errors.getFieldError("map[key0]").getCodes()[5]).isEqualTo("NOT_NULL");
}
use of cn.taketoday.beans.testfixture.beans.DerivedTestBean in project today-infrastructure by TAKETODAY.
the class BeanUtilsTests method copyPropertiesWithDifferentTypes2.
@Test
void copyPropertiesWithDifferentTypes2() throws Exception {
TestBean tb = new TestBean();
tb.setName("rod");
tb.setAge(32);
tb.setTouchy("touchy");
DerivedTestBean tb2 = new DerivedTestBean();
assertThat(tb2.getName() == null).as("Name empty").isTrue();
assertThat(tb2.getAge() == 0).as("Age empty").isTrue();
assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue();
BeanUtils.copyProperties(tb, tb2);
assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue();
assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue();
assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue();
}
use of cn.taketoday.beans.testfixture.beans.DerivedTestBean in project today-infrastructure by TAKETODAY.
the class SimpleTransactionScopeTests method getFromScope.
@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
RootBeanDefinition bd1 = new RootBeanDefinition();
bd1.setBeanClass(TestBean.class);
bd1.setScope("tx");
bd1.setPrimary(true);
context.registerBeanDefinition("txScopedObject1", bd1);
RootBeanDefinition bd2 = new RootBeanDefinition();
bd2.setBeanClass(DerivedTestBean.class);
bd2.setScope("tx");
context.registerBeanDefinition("txScopedObject2", bd2);
context.refresh();
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
TestBean bean1 = null;
DerivedTestBean bean2 = null;
DerivedTestBean bean2a = null;
DerivedTestBean bean2b = null;
TransactionSynchronizationManager.initSynchronization();
try {
bean1 = context.getBean(TestBean.class);
assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
bean2 = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
context.getBeanFactory().destroyScopedBean("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2.wasDestroyed()).isTrue();
bean2a = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
assertThat(bean2a).isNotSameAs(bean2);
context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2a.wasDestroyed()).isFalse();
bean2b = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
assertThat(bean2b).isNotSameAs(bean2);
assertThat(bean2b).isNotSameAs(bean2a);
} finally {
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
TransactionSynchronizationManager.clearSynchronization();
}
assertThat(bean2a.wasDestroyed()).isFalse();
assertThat(bean2b.wasDestroyed()).isTrue();
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
}
use of cn.taketoday.beans.testfixture.beans.DerivedTestBean in project today-infrastructure by TAKETODAY.
the class SimpleTransactionScopeTests method getWithTransactionManager.
@Test
public void getWithTransactionManager() throws Exception {
try (GenericApplicationContext context = new GenericApplicationContext()) {
context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
RootBeanDefinition bd1 = new RootBeanDefinition();
bd1.setBeanClass(TestBean.class);
bd1.setScope("tx");
bd1.setPrimary(true);
context.registerBeanDefinition("txScopedObject1", bd1);
RootBeanDefinition bd2 = new RootBeanDefinition();
bd2.setBeanClass(DerivedTestBean.class);
bd2.setScope("tx");
context.registerBeanDefinition("txScopedObject2", bd2);
context.refresh();
CallCountingTransactionManager tm = new CallCountingTransactionManager();
TransactionTemplate tt = new TransactionTemplate(tm);
Set<DerivedTestBean> finallyDestroy = new HashSet<>();
tt.execute(status -> {
TestBean bean1 = context.getBean(TestBean.class);
assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
context.getBeanFactory().destroyScopedBean("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2.wasDestroyed()).isTrue();
DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
assertThat(bean2a).isNotSameAs(bean2);
context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2a.wasDestroyed()).isFalse();
DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
finallyDestroy.add(bean2b);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
assertThat(bean2b).isNotSameAs(bean2);
assertThat(bean2b).isNotSameAs(bean2a);
Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
tt2.execute(status2 -> {
DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
immediatelyDestroy.add(bean2c);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2c);
assertThat(bean2c).isNotSameAs(bean2);
assertThat(bean2c).isNotSameAs(bean2a);
assertThat(bean2c).isNotSameAs(bean2b);
return null;
});
assertThat(immediatelyDestroy.iterator().next().wasDestroyed()).isTrue();
assertThat(bean2b.wasDestroyed()).isFalse();
return null;
});
assertThat(finallyDestroy.iterator().next().wasDestroyed()).isTrue();
}
}
Aggregations