Search in sources :

Example 66 with BeanWrapper

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();
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 67 with BeanWrapper

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();
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 68 with BeanWrapper

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);
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 69 with BeanWrapper

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);
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultMessageCodesResolver(cn.taketoday.validation.DefaultMessageCodesResolver) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapper (cn.taketoday.beans.BeanWrapper)69 Test (org.junit.jupiter.api.Test)63 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)60 NumberTestBean (cn.taketoday.beans.NumberTestBean)42 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)40 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)37 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)37 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)37 PropertyEditorSupport (java.beans.PropertyEditorSupport)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28 PropertyValues (cn.taketoday.beans.PropertyValues)25 BigDecimal (java.math.BigDecimal)8 BeansException (cn.taketoday.beans.BeansException)6 NumberFormat (java.text.NumberFormat)6 PropertyValue (cn.taketoday.beans.PropertyValue)4 BigInteger (java.math.BigInteger)4 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)3 Environment (cn.taketoday.core.env.Environment)2 ResourceEditor (cn.taketoday.core.io.ResourceEditor)2 ResourceLoader (cn.taketoday.core.io.ResourceLoader)2