Search in sources :

Example 21 with FormattingConversionService

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);
}
Also used : DataBinder(org.springframework.validation.DataBinder) FormattingConversionService(org.springframework.format.support.FormattingConversionService)

Example 22 with FormattingConversionService

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());
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) Date(java.util.Date) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 23 with FormattingConversionService

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();
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Before(org.junit.Before)

Example 24 with FormattingConversionService

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");
}
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 25 with FormattingConversionService

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();
    }
}
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)

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