Search in sources :

Example 1 with NumberTestBean

use of org.springframework.beans.testfixture.beans.NumberTestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setNumberProperties.

@Test
void setNumberProperties() {
    NumberTestBean target = new NumberTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("short2", "2");
    accessor.setPropertyValue("int2", "8");
    accessor.setPropertyValue("long2", "6");
    accessor.setPropertyValue("bigInteger", "3");
    accessor.setPropertyValue("float2", "8.1");
    accessor.setPropertyValue("double2", "6.1");
    accessor.setPropertyValue("bigDecimal", "4.0");
    assertThat(Short.valueOf("2")).as("Correct short2 value").isEqualTo(accessor.getPropertyValue("short2"));
    assertThat(Short.valueOf("2")).as("Correct short2 value").isEqualTo(target.getShort2());
    assertThat(Integer.valueOf("8")).as("Correct int2 value").isEqualTo(accessor.getPropertyValue("int2"));
    assertThat(Integer.valueOf("8")).as("Correct int2 value").isEqualTo(target.getInt2());
    assertThat(Long.valueOf("6")).as("Correct long2 value").isEqualTo(accessor.getPropertyValue("long2"));
    assertThat(Long.valueOf("6")).as("Correct long2 value").isEqualTo(target.getLong2());
    assertThat(new BigInteger("3")).as("Correct bigInteger value").isEqualTo(accessor.getPropertyValue("bigInteger"));
    assertThat(new BigInteger("3")).as("Correct bigInteger value").isEqualTo(target.getBigInteger());
    assertThat(Float.valueOf("8.1")).as("Correct float2 value").isEqualTo(accessor.getPropertyValue("float2"));
    assertThat(Float.valueOf("8.1")).as("Correct float2 value").isEqualTo(target.getFloat2());
    assertThat(Double.valueOf("6.1").equals(accessor.getPropertyValue("double2"))).as("Correct double2 value").isTrue();
    assertThat(Double.valueOf("6.1")).as("Correct double2 value").isEqualTo(target.getDouble2());
    assertThat(new BigDecimal("4.0")).as("Correct bigDecimal value").isEqualTo(accessor.getPropertyValue("bigDecimal"));
    assertThat(new BigDecimal("4.0")).as("Correct bigDecimal value").isEqualTo(target.getBigDecimal());
}
Also used : NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 2 with NumberTestBean

use of org.springframework.beans.testfixture.beans.NumberTestBean in project spring-framework by spring-projects.

the class CustomEditorTests method testDefaultNumberEditor.

@Test
void testDefaultNumberEditor() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("short1", "1");
    bw.setPropertyValue("short2", "2");
    bw.setPropertyValue("int1", "7");
    bw.setPropertyValue("int2", "8");
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    bw.setPropertyValue("bigInteger", "3");
    bw.setPropertyValue("float1", "7.1");
    bw.setPropertyValue("float2", "8.1");
    bw.setPropertyValue("double1", "5.1");
    bw.setPropertyValue("double2", "6.1");
    bw.setPropertyValue("bigDecimal", "4.5");
    assertThat(Short.valueOf("1").equals(bw.getPropertyValue("short1"))).as("Correct short1 value").isTrue();
    assertThat(tb.getShort1() == 1).as("Correct short1 value").isTrue();
    assertThat(Short.valueOf("2").equals(bw.getPropertyValue("short2"))).as("Correct short2 value").isTrue();
    assertThat(Short.valueOf("2").equals(tb.getShort2())).as("Correct short2 value").isTrue();
    assertThat(Integer.valueOf("7").equals(bw.getPropertyValue("int1"))).as("Correct int1 value").isTrue();
    assertThat(tb.getInt1() == 7).as("Correct int1 value").isTrue();
    assertThat(Integer.valueOf("8").equals(bw.getPropertyValue("int2"))).as("Correct int2 value").isTrue();
    assertThat(Integer.valueOf("8").equals(tb.getInt2())).as("Correct int2 value").isTrue();
    assertThat(Long.valueOf("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue();
    assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
    assertThat(Long.valueOf("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue();
    assertThat(Long.valueOf("6").equals(tb.getLong2())).as("Correct long2 value").isTrue();
    assertThat(new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue();
    assertThat(new BigInteger("3").equals(tb.getBigInteger())).as("Correct bigInteger value").isTrue();
    assertThat(Float.valueOf("7.1").equals(bw.getPropertyValue("float1"))).as("Correct float1 value").isTrue();
    assertThat(Float.valueOf("7.1").equals(tb.getFloat1())).as("Correct float1 value").isTrue();
    assertThat(Float.valueOf("8.1").equals(bw.getPropertyValue("float2"))).as("Correct float2 value").isTrue();
    assertThat(Float.valueOf("8.1").equals(tb.getFloat2())).as("Correct float2 value").isTrue();
    assertThat(Double.valueOf("5.1").equals(bw.getPropertyValue("double1"))).as("Correct double1 value").isTrue();
    assertThat(tb.getDouble1() == 5.1).as("Correct double1 value").isTrue();
    assertThat(Double.valueOf("6.1").equals(bw.getPropertyValue("double2"))).as("Correct double2 value").isTrue();
    assertThat(Double.valueOf("6.1").equals(tb.getDouble2())).as("Correct double2 value").isTrue();
    assertThat(new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue();
    assertThat(new BigDecimal("4.5").equals(tb.getBigDecimal())).as("Correct bigDecimal value").isTrue();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 3 with NumberTestBean

use of org.springframework.beans.testfixture.beans.NumberTestBean in project spring-framework by spring-projects.

the class CustomEditorTests method testCustomNumberEditorWithAllowEmpty.

@Test
void testCustomNumberEditorWithAllowEmpty() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
    bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    assertThat(Long.valueOf("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue();
    assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
    assertThat(Long.valueOf("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue();
    assertThat(Long.valueOf("6").equals(tb.getLong2())).as("Correct long2 value").isTrue();
    bw.setPropertyValue("long2", "");
    assertThat(bw.getPropertyValue("long2") == null).as("Correct long2 value").isTrue();
    assertThat(tb.getLong2() == null).as("Correct long2 value").isTrue();
    assertThatExceptionOfType(BeansException.class).isThrownBy(() -> bw.setPropertyValue("long1", ""));
    assertThat(bw.getPropertyValue("long1")).isEqualTo(5L);
    assertThat(tb.getLong1()).isEqualTo(5);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) NumberFormat(java.text.NumberFormat) BeansException(org.springframework.beans.BeansException) Test(org.junit.jupiter.api.Test)

Example 4 with NumberTestBean

use of org.springframework.beans.testfixture.beans.NumberTestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setNumberPropertiesWithCoercion.

@Test
void setNumberPropertiesWithCoercion() {
    NumberTestBean target = new NumberTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("short2", 2);
    accessor.setPropertyValue("int2", 8L);
    accessor.setPropertyValue("long2", new BigInteger("6"));
    accessor.setPropertyValue("bigInteger", 3L);
    accessor.setPropertyValue("float2", 8.1D);
    accessor.setPropertyValue("double2", new BigDecimal("6.1"));
    accessor.setPropertyValue("bigDecimal", 4.0F);
    assertThat(Short.valueOf("2")).as("Correct short2 value").isEqualTo(accessor.getPropertyValue("short2"));
    assertThat(Short.valueOf("2")).as("Correct short2 value").isEqualTo(target.getShort2());
    assertThat(Integer.valueOf("8")).as("Correct int2 value").isEqualTo(accessor.getPropertyValue("int2"));
    assertThat(Integer.valueOf("8")).as("Correct int2 value").isEqualTo(target.getInt2());
    assertThat(Long.valueOf("6")).as("Correct long2 value").isEqualTo(accessor.getPropertyValue("long2"));
    assertThat(Long.valueOf("6")).as("Correct long2 value").isEqualTo(target.getLong2());
    assertThat(new BigInteger("3").equals(accessor.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue();
    assertThat(new BigInteger("3")).as("Correct bigInteger value").isEqualTo(target.getBigInteger());
    assertThat(Float.valueOf("8.1")).as("Correct float2 value").isEqualTo(accessor.getPropertyValue("float2"));
    assertThat(Float.valueOf("8.1")).as("Correct float2 value").isEqualTo(target.getFloat2());
    assertThat(Double.valueOf("6.1")).as("Correct double2 value").isEqualTo(accessor.getPropertyValue("double2"));
    assertThat(Double.valueOf("6.1")).as("Correct double2 value").isEqualTo(target.getDouble2());
    assertThat(new BigDecimal("4.0")).as("Correct bigDecimal value").isEqualTo(accessor.getPropertyValue("bigDecimal"));
    assertThat(new BigDecimal("4.0")).as("Correct bigDecimal value").isEqualTo(target.getBigDecimal());
}
Also used : NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 5 with NumberTestBean

use of org.springframework.beans.testfixture.beans.NumberTestBean in project spring-framework by spring-projects.

the class CustomEditorTests method testCustomNumberEditorWithFrenchBigDecimal.

@Test
void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
    bw.setPropertyValue("bigDecimal", "1000");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.0f, within(0f));
    bw.setPropertyValue("bigDecimal", "1000,5");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f));
    bw.setPropertyValue("bigDecimal", "1 000,5");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f));
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)6 BigDecimal (java.math.BigDecimal)5 BigInteger (java.math.BigInteger)4 BeanWrapper (org.springframework.beans.BeanWrapper)4 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 NumberFormat (java.text.NumberFormat)3 BeansException (org.springframework.beans.BeansException)1