use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class DateTimeFormattingTests method setUp.
private void setUp(DateTimeFormatterRegistrar registrar) {
conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
registrar.registerFormatters(conversionService);
DateTimeBean bean = new DateTimeBean();
bean.getChildren().add(new DateTimeBean());
binder = new DataBinder(bean);
binder.setConversionService(conversionService);
LocaleContextHolder.setLocale(Locale.US);
DateTimeContext context = new DateTimeContext();
context.setTimeZone(ZoneId.of("-05:00"));
DateTimeContextHolder.setDateTimeContext(context);
}
use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.
the class WebMvcAutoConfigurationTests method noDateFormat.
@Test
public void noDateFormat() throws Exception {
load(AllResources.class);
FormattingConversionService cs = this.context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
// formatting cs should use simple toString()
assertThat(cs.convert(date, String.class)).isEqualTo(date.toString());
}
use of org.springframework.format.support.FormattingConversionService in project spring-mvc-showcase by spring-projects.
the class ConvertControllerTests method setup.
@Before
public void setup() throws Exception {
FormattingConversionService cs = new DefaultFormattingConversionService();
cs.addFormatterForFieldAnnotation(new MaskFormatAnnotationFormatterFactory());
this.mockMvc = standaloneSetup(new ConvertController()).setConversionService(cs).alwaysExpect(status().isOk()).build();
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
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);
MutablePropertyValues pvs = new MutablePropertyValues();
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 org.springframework.format.support.FormattingConversionService 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();
}
}
Aggregations