Search in sources :

Example 1 with DataBinder

use of cn.taketoday.validation.DataBinder in project today-infrastructure by TAKETODAY.

the class MoneyFormattingTests method testAmountWithNumberFormat4.

@Test
public void testAmountWithNumberFormat4() {
    FormattedMoneyHolder4 bean = new FormattedMoneyHolder4();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    PropertyValues propertyValues = new PropertyValues();
    propertyValues.add("amount", "010.500");
    binder.bind(propertyValues);
    assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
    assertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("010.500");
    assertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
    assertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Example 2 with DataBinder

use of cn.taketoday.validation.DataBinder in project today-framework by TAKETODAY.

the class DataBinderTests method testSetAutoGrowCollectionLimit.

@Test
// SPR-14888
void testSetAutoGrowCollectionLimit() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    DataBinder binder = new DataBinder(tb);
    binder.setAutoGrowCollectionLimit(257);
    PropertyValues pvs = new PropertyValues();
    pvs.add("integerList[256]", "1");
    binder.bind(pvs);
    assertThat(tb.getIntegerList().size()).isEqualTo(257);
    assertThat(tb.getIntegerList().get(256)).isEqualTo(Integer.valueOf(1));
    assertThat(binder.getBindingResult().getFieldValue("integerList[256]")).isEqualTo(1);
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Example 3 with DataBinder

use of cn.taketoday.validation.DataBinder in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingErrorWithRuntimeExceptionFromFormatter.

@Test
void testBindingErrorWithRuntimeExceptionFromFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatter(new Formatter<String>() {

        @Override
        public String parse(String text, Locale locale) throws ParseException {
            throw new RuntimeException(text);
        }

        @Override
        public String print(String object, Locale locale) {
            return object;
        }
    });
    binder.setConversionService(conversionService);
    PropertyValues pvs = new PropertyValues();
    pvs.add("name", "test");
    binder.bind(pvs);
    assertThat(binder.getBindingResult().hasFieldErrors("name")).isTrue();
    assertThat(binder.getBindingResult().getFieldError("name").getCode()).isEqualTo("typeMismatch");
    assertThat(binder.getBindingResult().getFieldValue("name")).isEqualTo("test");
}
Also used : Locale(java.util.Locale) 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) DataBinder(cn.taketoday.validation.DataBinder) ParseException(java.text.ParseException) FormattingConversionService(cn.taketoday.format.support.FormattingConversionService) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 4 with DataBinder

use of cn.taketoday.validation.DataBinder in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingWithAllowedAndDisallowedMapFields.

@Test
void testBindingWithAllowedAndDisallowedMapFields() throws BindException {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod);
    binder.setAllowedFields("someMap[key1]", "someMap[key2]");
    binder.setDisallowedFields("someMap['key3']", "someMap[key4]");
    PropertyValues pvs = new PropertyValues();
    pvs.add("someMap[key1]", "value1");
    pvs.add("someMap['key2']", "value2");
    pvs.add("someMap[key3]", "value3");
    pvs.add("someMap['key4']", "value4");
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getSomeMap().get("key1")).isEqualTo("value1");
    assertThat(rod.getSomeMap().get("key2")).isEqualTo("value2");
    assertThat(rod.getSomeMap().get("key3")).isNull();
    assertThat(rod.getSomeMap().get("key4")).isNull();
    String[] disallowedFields = binder.getBindingResult().getSuppressedFields();
    assertThat(disallowedFields).hasSize(2);
    assertThat(ObjectUtils.containsElement(disallowedFields, "someMap[key3]")).isTrue();
    assertThat(ObjectUtils.containsElement(disallowedFields, "someMap[key4]")).isTrue();
}
Also used : 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) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Example 5 with DataBinder

use of cn.taketoday.validation.DataBinder in project today-framework by TAKETODAY.

the class DataBinderTests method testCustomFormatterForPrimitiveProperty.

@Test
void testCustomFormatterForPrimitiveProperty() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.addCustomFormatter(new Formatter<Integer>() {

        @Override
        public Integer parse(String text, Locale locale) throws ParseException {
            return 99;
        }

        @Override
        public String print(Integer object, Locale locale) {
            return "argh";
        }
    }, "age");
    PropertyValues pvs = new PropertyValues();
    pvs.add("age", "x");
    binder.bind(pvs);
    assertThat(binder.getBindingResult().getFieldValue("age")).isEqualTo("argh");
    assertThat(tb.getAge()).isEqualTo(99);
}
Also used : Locale(java.util.Locale) 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) DataBinder(cn.taketoday.validation.DataBinder) ParseException(java.text.ParseException) Test(org.junit.jupiter.api.Test)

Aggregations

DataBinder (cn.taketoday.validation.DataBinder)90 Test (org.junit.jupiter.api.Test)84 PropertyValues (cn.taketoday.beans.PropertyValues)79 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)63 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)59 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)55 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)55 PropertyEditorSupport (java.beans.PropertyEditorSupport)17 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)9 Errors (cn.taketoday.validation.Errors)9 NumberStyleFormatter (cn.taketoday.format.number.NumberStyleFormatter)8 FormattingConversionService (cn.taketoday.format.support.FormattingConversionService)8 ParseException (java.text.ParseException)7 Locale (java.util.Locale)7 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)6 BindingResult (cn.taketoday.validation.BindingResult)5 DefaultMessageCodesResolver (cn.taketoday.validation.DefaultMessageCodesResolver)4 FieldError (cn.taketoday.validation.FieldError)4 PropertyEditor (java.beans.PropertyEditor)3 InvalidPropertyException (cn.taketoday.beans.InvalidPropertyException)2