Search in sources :

Example 16 with NumberStyleFormatter

use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.

the class DataBinderTests method testBindingErrorWithFormatter.

@Test
void testBindingErrorWithFormatter() {
    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", "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)

Example 17 with NumberStyleFormatter

use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.

the class DataBinderTests method testBindingWithFormatterAgainstList.

@Test
void testBindingWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    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("integerList[0]", "1");
    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertThat(tb.getIntegerList().get(0)).isEqualTo(1);
        assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1");
    } finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
Also used : 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)

Example 18 with NumberStyleFormatter

use of org.springframework.format.number.NumberStyleFormatter in project spring-framework by spring-projects.

the class DataBinderTests method testBindingErrorWithFormatterAgainstList.

@Test
void testBindingErrorWithFormatterAgainstList() {
    BeanWithIntegerList tb = new BeanWithIntegerList();
    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("integerList[0]", "1x2");
    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertThat(tb.getIntegerList().isEmpty()).isTrue();
        assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1x2");
        assertThat(binder.getBindingResult().hasFieldErrors("integerList[0]")).isTrue();
    } finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
Also used : 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

Test (org.junit.jupiter.api.Test)18 NumberStyleFormatter (org.springframework.format.number.NumberStyleFormatter)18 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)8 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 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)6 FormattingConversionService (org.springframework.format.support.FormattingConversionService)6 PropertyEditor (java.beans.PropertyEditor)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)1