Search in sources :

Example 1 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method parseEmptyString.

@Test
public void parseEmptyString() {
    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 2 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method introspectedPrinter.

@Test
public void introspectedPrinter() {
    formattingService.addPrinter(new NumberStyleFormatter("#,#00.0#"));
    assertThat(formattingService.convert(123, String.class)).isEqualTo("123.0");
    assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> formattingService.convert("123.0", Integer.class)).withCauseInstanceOf(NumberFormatException.class);
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 3 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method introspectedParser.

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

Example 4 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method proxiedFormatter.

@Test
public void proxiedFormatter() {
    Formatter<?> formatter = new NumberStyleFormatter();
    formattingService.addFormatter((Formatter<?>) new ProxyFactory(formatter).getProxy());
    assertThat(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 5 with NumberStyleFormatter

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

the class FormattingConversionServiceTests method formatFieldForTypeWithFormatter.

@Test
public void formatFieldForTypeWithFormatter() {
    formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    String formatted = formattingService.convert(3, String.class);
    assertThat(formatted).isEqualTo("3");
    Integer i = formattingService.convert("3", Integer.class);
    assertThat(i).isEqualTo(3);
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) 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