Search in sources :

Example 6 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method mvcConversionService.

/**
 * Return a {@link FormattingConversionService} for use with annotated controllers.
 * <p>See {@link #addFormatters} as an alternative to overriding this method.
 */
@Bean
public FormattingConversionService mvcConversionService() {
    FormattingConversionService conversionService = new DefaultFormattingConversionService();
    addFormatters(conversionService);
    return conversionService;
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Bean(org.springframework.context.annotation.Bean)

Example 7 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.

the class DelegatingWebFluxConfigurationTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() {
    delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
    ReactiveAdapterRegistry reactiveAdapterRegistry = delegatingConfig.webFluxAdapterRegistry();
    ServerCodecConfigurer serverCodecConfigurer = delegatingConfig.serverCodecConfigurer();
    FormattingConversionService formattingConversionService = delegatingConfig.webFluxConversionService();
    Validator validator = delegatingConfig.webFluxValidator();
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) this.delegatingConfig.requestMappingHandlerAdapter(reactiveAdapterRegistry, serverCodecConfigurer, formattingConversionService, validator).getWebBindingInitializer();
    verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
    verify(webFluxConfigurer).getValidator();
    verify(webFluxConfigurer).getMessageCodesResolver();
    verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
    verify(webFluxConfigurer).configureArgumentResolvers(any());
    assertThat(initializer).isNotNull();
    boolean condition = initializer.getValidator() instanceof LocalValidatorFactoryBean;
    assertThat(condition).isTrue();
    assertThat(initializer.getConversionService()).isSameAs(formatterRegistry.getValue());
    assertThat(codecsConfigurer.getValue().getReaders().size()).isEqualTo(14);
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 8 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.

the class CheckboxesTagTests method withMultiValueWithFormatter.

@Test
public void withMultiValueWithFormatter() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "   foo", "   bar", "   baz" });
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    FormattingConversionService cs = new FormattingConversionService();
    cs.addFormatterForFieldType(String.class, new Formatter<String>() {

        @Override
        public String print(String object, Locale locale) {
            return object;
        }

        @Override
        public String parse(String text, Locale locale) throws ParseException {
            return text.trim();
        }
    });
    bindingResult.initConversion(cs);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = document.getRootElement().elements().get(0);
    Element checkboxElement1 = spanElement1.elements().get(0);
    assertThat(checkboxElement1.getName()).isEqualTo("input");
    assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo("   foo");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element checkboxElement2 = spanElement2.elements().get(0);
    assertThat(checkboxElement2.getName()).isEqualTo("input");
    assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo("   bar");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element checkboxElement3 = spanElement3.elements().get(0);
    assertThat(checkboxElement3.getName()).isEqualTo("input");
    assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
    assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo("   baz");
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ParseException(java.text.ParseException) Document(org.dom4j.Document) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 9 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method defaultDateFormat.

@Test
void defaultDateFormat() {
    this.contextRunner.run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
        // formatting conversion service should use simple toString()
        assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString());
    });
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) Date(java.util.Date) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method customDateTimeTimeFormat.

@Test
void customDateTimeTimeFormat() {
    this.contextRunner.withPropertyValues("spring.webflux.format.date-time=yyyy-MM-dd HH:mm:ss").run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        LocalDateTime dateTime = LocalDateTime.of(2020, 4, 28, 11, 43, 10);
        assertThat(conversionService.convert(dateTime, String.class)).isEqualTo("2020-04-28 11:43:10");
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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