use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class BeanInfoTests method testComplexObject.
@Test
public void testComplexObject() {
ValueBean bean = new ValueBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
Integer value = 1;
bw.setPropertyValue("value", value);
assertThat(value).as("value not set correctly").isEqualTo(bean.getValue());
value = 2;
bw.setPropertyValue("value", value.toString());
assertThat(value).as("value not converted").isEqualTo(bean.getValue());
bw.setPropertyValue("value", null);
assertThat(bean.getValue()).as("value not null").isNull();
bw.setPropertyValue("value", "");
assertThat(bean.getValue()).as("value not converted to null").isNull();
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class CustomEditorTests method testCharacterEditorWithAllowEmpty.
@Test
void testCharacterEditorWithAllowEmpty() {
CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb);
bw.registerCustomEditor(Character.class, new CharacterEditor(true));
bw.setPropertyValue("myCharacter", 'c');
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
bw.setPropertyValue("myCharacter", "c");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
bw.setPropertyValue("myCharacter", "\u0041");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('A'));
bw.setPropertyValue("myCharacter", " ");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf(' '));
bw.setPropertyValue("myCharacter", "");
assertThat(cb.getMyCharacter()).isNull();
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class DataBinderTests method setCustomMessageCodesResolverBeforeInitializeBindingResultForBeanPropertyAccess.
@Test
// SPR-15009
void setCustomMessageCodesResolverBeforeInitializeBindingResultForBeanPropertyAccess() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
DefaultMessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver();
messageCodesResolver.setPrefix("errors.");
binder.setMessageCodesResolver(messageCodesResolver);
// allow configuration after set a MessageCodesResolver
binder.setAutoGrowCollectionLimit(512);
binder.initBeanPropertyAccess();
PropertyValues mpv = new PropertyValues();
mpv.add("age", "invalid");
binder.bind(mpv);
assertThat(binder.getBindingResult().getFieldError("age").getCode()).isEqualTo("errors.typeMismatch");
assertThat(((BeanWrapper) binder.getInternalBindingResult().getPropertyAccessor()).getAutoGrowCollectionLimit()).isEqualTo(512);
}
use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.
the class DataBinderTests method testSetCustomMessageCodesResolverBeforeInitializeBindingResultForBeanPropertyAccess.
@Test
// SPR-15009
void testSetCustomMessageCodesResolverBeforeInitializeBindingResultForBeanPropertyAccess() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
DefaultMessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver();
messageCodesResolver.setPrefix("errors.");
binder.setMessageCodesResolver(messageCodesResolver);
// allow configuration after set a MessageCodesResolver
binder.setAutoGrowCollectionLimit(512);
binder.initBeanPropertyAccess();
PropertyValues mpv = new PropertyValues();
mpv.add("age", "invalid");
binder.bind(mpv);
assertThat(binder.getBindingResult().getFieldError("age").getCode()).isEqualTo("errors.typeMismatch");
assertThat(((BeanWrapper) binder.getInternalBindingResult().getPropertyAccessor()).getAutoGrowCollectionLimit()).isEqualTo(512);
}
Aggregations