use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.
the class CharSequenceToObjectConverterTests method convertWhenTargetIsListAndNotUsingApplicationConversionService.
@Test
@SuppressWarnings("unchecked")
void convertWhenTargetIsListAndNotUsingApplicationConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
conversionService.addConverter(new CharSequenceToObjectConverter(conversionService));
StringBuilder source = new StringBuilder("1,2,3");
TypeDescriptor sourceType = TypeDescriptor.valueOf(StringBuilder.class);
TypeDescriptor targetType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
List<String> conveted = (List<String>) conversionService.convert(source, sourceType, targetType);
assertThat(conveted).containsExactly("1", "2", "3");
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class StandaloneMockMvcBuilder method registerMvcSingletons.
private void registerMvcSingletons(StubWebApplicationContext wac) {
StandaloneConfiguration config = new StandaloneConfiguration();
config.setApplicationContext(wac);
ServletContext sc = wac.getServletContext();
wac.addBeans(this.controllers);
wac.addBeans(this.controllerAdvice);
FormattingConversionService mvcConversionService = config.mvcConversionService();
wac.addBean("mvcConversionService", mvcConversionService);
ResourceUrlProvider resourceUrlProvider = config.mvcResourceUrlProvider();
wac.addBean("mvcResourceUrlProvider", resourceUrlProvider);
ContentNegotiationManager mvcContentNegotiationManager = config.mvcContentNegotiationManager();
wac.addBean("mvcContentNegotiationManager", mvcContentNegotiationManager);
Validator mvcValidator = config.mvcValidator();
wac.addBean("mvcValidator", mvcValidator);
RequestMappingHandlerMapping hm = config.getHandlerMapping(mvcConversionService, resourceUrlProvider);
if (sc != null) {
hm.setServletContext(sc);
}
hm.setApplicationContext(wac);
hm.afterPropertiesSet();
wac.addBean("requestMappingHandlerMapping", hm);
RequestMappingHandlerAdapter ha = config.requestMappingHandlerAdapter(mvcContentNegotiationManager, mvcConversionService, mvcValidator);
if (sc != null) {
ha.setServletContext(sc);
}
ha.setApplicationContext(wac);
ha.afterPropertiesSet();
wac.addBean("requestMappingHandlerAdapter", ha);
wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver(mvcContentNegotiationManager));
wac.addBeans(initViewResolvers(wac));
wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver);
wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver());
wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator());
this.flashMapManager = new SessionFlashMapManager();
wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager);
extendMvcSingletons(sc).forEach(wac::addBean);
}
use of org.springframework.format.support.FormattingConversionService 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.support.FormattingConversionService in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithParseExceptionFromFormatter.
@Test
void testBindingErrorWithParseExceptionFromFormatter() {
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 ParseException(text, 0);
}
@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 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