use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithFormatter.
@Test
void testBindingErrorWithFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getMyFloat()).isEqualTo(Float.valueOf(0.0f));
assertThat(binder.getBindingResult().getFieldValue("myFloat")).isEqualTo("1x2");
assertThat(binder.getBindingResult().hasFieldErrors("myFloat")).isTrue();
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithFormatterAgainstList.
@Test
void testBindingWithFormatterAgainstList() {
BeanWithIntegerList tb = new BeanWithIntegerList();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("integerList[0]", "1");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getIntegerList().get(0)).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1");
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithFormatterAgainstList.
@Test
void testBindingErrorWithFormatterAgainstList() {
BeanWithIntegerList tb = new BeanWithIntegerList();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("integerList[0]", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getIntegerList().isEmpty()).isTrue();
assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1x2");
assertThat(binder.getBindingResult().hasFieldErrors("integerList[0]")).isTrue();
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
Aggregations