Search in sources :

Example 11 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method parseNull.

@Test
public void parseNull() {
    formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    assertThat(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 12 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method parseBlankString.

@Test
public void parseBlankString() {
    formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    assertThat(formattingService.convert("     ", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 13 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method printNull.

@Test
public void printNull() {
    formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    assertThat(formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))).isEqualTo("");
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 14 with NumberStyleFormatter

use of org.springframework.format.number.NumberStyleFormatter 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)

Example 15 with NumberStyleFormatter

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

the class DataBinderTests method testBindingWithCustomFormatter.

@Test
void testBindingWithCustomFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    binder.addCustomFormatter(new NumberStyleFormatter(), Float.class);
    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(((Number) editor.getValue()).floatValue() == 1.6f).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) PropertyEditor(java.beans.PropertyEditor) 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