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