Search in sources :

Example 1 with BooleanTestBean

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

the class CustomEditorTests method testCustomBooleanEditorWithAllowEmpty.

@Test
public void testCustomBooleanEditorWithAllowEmpty() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
    bw.setPropertyValue("bool2", "true");
    assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "false");
    assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "on");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "off");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "yes");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "no");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "1");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "0");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "");
    assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null);
    assertTrue("Correct bool2 value", tb.getBool2() == null);
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 2 with BooleanTestBean

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

the class AbstractPropertyAccessorTests method setBooleanProperty.

@Test
public void setBooleanProperty() {
    BooleanTestBean target = new BooleanTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("bool2", "true");
    assertTrue("Correct bool2 value", Boolean.TRUE.equals(accessor.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", target.getBool2());
    accessor.setPropertyValue("bool2", "false");
    assertTrue("Correct bool2 value", Boolean.FALSE.equals(accessor.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", !target.getBool2());
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) Test(org.junit.Test)

Example 3 with BooleanTestBean

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

the class CustomEditorTests method testDefaultBooleanEditorForWrapperType.

@Test
public void testDefaultBooleanEditorForWrapperType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool2", "true");
    assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "false");
    assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "on");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "off");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "yes");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "no");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "1");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "0");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "");
    assertNull("Correct bool2 value", tb.getBool2());
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 4 with BooleanTestBean

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

the class CustomEditorTests method testDefaultBooleanEditorForPrimitiveType.

@Test
public void testDefaultBooleanEditorForPrimitiveType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool1", "true");
    assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1")));
    assertTrue("Correct bool1 value", tb.isBool1());
    bw.setPropertyValue("bool1", "false");
    assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1")));
    assertTrue("Correct bool1 value", !tb.isBool1());
    bw.setPropertyValue("bool1", "  true  ");
    assertTrue("Correct bool1 value", tb.isBool1());
    bw.setPropertyValue("bool1", "  false  ");
    assertTrue("Correct bool1 value", !tb.isBool1());
    bw.setPropertyValue("bool1", "on");
    assertTrue("Correct bool1 value", tb.isBool1());
    bw.setPropertyValue("bool1", "off");
    assertTrue("Correct bool1 value", !tb.isBool1());
    bw.setPropertyValue("bool1", "yes");
    assertTrue("Correct bool1 value", tb.isBool1());
    bw.setPropertyValue("bool1", "no");
    assertTrue("Correct bool1 value", !tb.isBool1());
    bw.setPropertyValue("bool1", "1");
    assertTrue("Correct bool1 value", tb.isBool1());
    bw.setPropertyValue("bool1", "0");
    assertTrue("Correct bool1 value", !tb.isBool1());
    try {
        bw.setPropertyValue("bool1", "argh");
        fail("Should have thrown BeansException");
    } catch (BeansException ex) {
    // expected
    }
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)3 BeansException (org.springframework.beans.BeansException)1