use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method introspectedFormatter.
@Test
public void introspectedFormatter() {
formattingService.addFormatter(new NumberStyleFormatter("#,#00.0#"));
assertThat(formattingService.convert(123, String.class)).isEqualTo("123.0");
assertThat(formattingService.convert("123.0", Integer.class)).isEqualTo(123);
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method parseNullPrimitiveProperty.
@Test
public void parseNullPrimitiveProperty() {
formattingService.addFormatterForFieldType(Integer.class, new NumberStyleFormatter());
assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(int.class)));
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithFormatterAgainstFields.
@Test
void testBindingWithFormatterAgainstFields() {
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);
binder.initDirectFieldAccess();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getMyFloat()).isEqualTo(Float.valueOf(1.2f));
assertThat(binder.getBindingResult().getFieldValue("myFloat")).isEqualTo("1,2");
PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
assertThat(editor).isNotNull();
editor.setValue(1.4f);
assertThat(editor.getAsText()).isEqualTo("1,4");
editor = binder.getBindingResult().findEditor("myFloat", null);
assertThat(editor).isNotNull();
editor.setAsText("1,6");
assertThat(editor.getValue()).isEqualTo(1.6f);
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithCustomFormatter.
@Test
void testBindingErrorWithCustomFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.addCustomFormatter(new NumberStyleFormatter());
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();
assertThat(binder.getBindingResult().getFieldError("myFloat").getCode()).isEqualTo("typeMismatch");
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithFormatterAgainstFields.
@Test
void testBindingErrorWithFormatterAgainstFields() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.initDirectFieldAccess();
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();
}
}
Aggregations