Search in sources :

Example 6 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method introspectedFormatter.

@Test
public void introspectedFormatter() {
    formattingService.addFormatter(new NumberStyleFormatter("#,#00.0#"));
    assertThat(formattingService.convert(123, String.class)).isEqualTo("123.0");
    assertThat(formattingService.convert("123.0", Integer.class)).isEqualTo(123);
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 7 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method parseNullPrimitiveProperty.

@Test
public void parseNullPrimitiveProperty() {
    formattingService.addFormatterForFieldType(Integer.class, new NumberStyleFormatter());
    assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(int.class)));
}
Also used : ConversionFailedException(org.springframework.core.convert.ConversionFailedException) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 8 with NumberStyleFormatter

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

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

the class DataBinderTests method testBindingErrorWithCustomFormatter.

@Test
void testBindingErrorWithCustomFormatter() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    binder.addCustomFormatter(new NumberStyleFormatter());
    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();
        assertThat(binder.getBindingResult().getFieldError("myFloat").getCode()).isEqualTo("typeMismatch");
    } 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) Test(org.junit.jupiter.api.Test)

Example 10 with NumberStyleFormatter

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

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