use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method parseNull.
@Test
public void parseNull() {
formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
assertThat(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method parseBlankString.
@Test
public void parseBlankString() {
formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
assertThat(formattingService.convert(" ", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method printNull.
@Test
public void printNull() {
formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
assertThat(formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))).isEqualTo("");
}
use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithFormatter.
@Test
void testBindingWithFormatter() {
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", "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 testBindingWithCustomFormatter.
@Test
void testBindingWithCustomFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.addCustomFormatter(new NumberStyleFormatter(), Float.class);
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(((Number) editor.getValue()).floatValue() == 1.6f).isTrue();
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
Aggregations