Search in sources :

Example 1 with BooleanTestBean

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

the class CustomEditorTests method testCustomBooleanEditorWithAllowEmpty.

@Test
void testCustomBooleanEditorWithAllowEmpty() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
    bw.setPropertyValue("bool2", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    boolean condition3 = !tb.getBool2();
    assertThat(condition3).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "on");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "off");
    boolean condition2 = !tb.getBool2();
    assertThat(condition2).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "yes");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "no");
    boolean condition1 = !tb.getBool2();
    assertThat(condition1).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "1");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "0");
    boolean condition = !tb.getBool2();
    assertThat(condition).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "");
    assertThat(bw.getPropertyValue("bool2")).as("Correct bool2 value").isNull();
    assertThat(tb.getBool2()).as("Correct bool2 value").isNull();
}
Also used : BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 2 with BooleanTestBean

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

the class AbstractPropertyAccessorTests method setBooleanProperty.

@Test
void setBooleanProperty() {
    BooleanTestBean target = new BooleanTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("bool2", "true");
    assertThat(Boolean.TRUE.equals(accessor.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(target.getBool2()).as("Correct bool2 value").isTrue();
    accessor.setPropertyValue("bool2", "false");
    assertThat(Boolean.FALSE.equals(accessor.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(!target.getBool2()).as("Correct bool2 value").isTrue();
}
Also used : BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) Test(org.junit.jupiter.api.Test)

Example 3 with BooleanTestBean

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

the class CustomEditorTests method testDefaultBooleanEditorForWrapperType.

@Test
void testDefaultBooleanEditorForWrapperType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool2", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    boolean condition3 = !tb.getBool2();
    assertThat(condition3).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "on");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "off");
    boolean condition2 = !tb.getBool2();
    assertThat(condition2).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "yes");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "no");
    boolean condition1 = !tb.getBool2();
    assertThat(condition1).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "1");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "0");
    boolean condition = !tb.getBool2();
    assertThat(condition).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "");
    assertThat(tb.getBool2()).as("Correct bool2 value").isNull();
}
Also used : BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 4 with BooleanTestBean

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

the class CustomEditorTests method testDefaultBooleanEditorForPrimitiveType.

@Test
void testDefaultBooleanEditorForPrimitiveType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool1", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue();
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue();
    boolean condition4 = !tb.isBool1();
    assertThat(condition4).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "  true  ");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "  false  ");
    boolean condition3 = !tb.isBool1();
    assertThat(condition3).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "on");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "off");
    boolean condition2 = !tb.isBool1();
    assertThat(condition2).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "yes");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "no");
    boolean condition1 = !tb.isBool1();
    assertThat(condition1).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "1");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "0");
    boolean condition = !tb.isBool1();
    assertThat(condition).as("Correct bool1 value").isTrue();
    assertThatExceptionOfType(BeansException.class).isThrownBy(() -> bw.setPropertyValue("bool1", "argh"));
}
Also used : BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeansException(org.springframework.beans.BeansException) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)3 BeansException (org.springframework.beans.BeansException)1