Search in sources :

Example 16 with FormattingConversionService

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");
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) List(java.util.List) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 17 with FormattingConversionService

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);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Validator(org.springframework.validation.Validator) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider)

Example 18 with FormattingConversionService

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();
    }
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditor(java.beans.PropertyEditor) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 19 with FormattingConversionService

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");
}
Also used : Locale(java.util.Locale) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ParseException(java.text.ParseException) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 20 with FormattingConversionService

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();
    }
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

FormattingConversionService (org.springframework.format.support.FormattingConversionService)38 Test (org.junit.jupiter.api.Test)27 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)13 Date (java.util.Date)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)8 Locale (java.util.Locale)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 NumberStyleFormatter (org.springframework.format.number.NumberStyleFormatter)6 ParseException (java.text.ParseException)5 LocalDateTime (java.time.LocalDateTime)4 LocalTime (java.time.LocalTime)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 StringReader (java.io.StringReader)3 Document (org.dom4j.Document)3 Element (org.dom4j.Element)3