use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class CustomEditorTests method testCharacterEditor.
@Test
void testCharacterEditor() {
CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb);
bw.setPropertyValue("myChar", Character.valueOf('c'));
assertThat(cb.getMyChar()).isEqualTo('c');
bw.setPropertyValue("myChar", "c");
assertThat(cb.getMyChar()).isEqualTo('c');
bw.setPropertyValue("myChar", "\u0041");
assertThat(cb.getMyChar()).isEqualTo('A');
bw.setPropertyValue("myChar", "\\u0022");
assertThat(cb.getMyChar()).isEqualTo('"');
CharacterEditor editor = new CharacterEditor(false);
editor.setAsText("M");
assertThat(editor.getAsText()).isEqualTo("M");
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class CustomEditorTests method testCustomEditorForSingleProperty.
@Test
void testCustomEditorForSingleProperty() {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
});
bw.setPropertyValue("name", "value");
bw.setPropertyValue("touchy", "value");
assertThat(bw.getPropertyValue("name")).isEqualTo("prefixvalue");
assertThat(tb.getName()).isEqualTo("prefixvalue");
assertThat(bw.getPropertyValue("touchy")).isEqualTo("value");
assertThat(tb.getTouchy()).isEqualTo("value");
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class CustomEditorTests method testComplexObject.
@Test
void testComplexObject() {
TestBean tb = new TestBean();
String newName = "Rod";
String tbString = "Kerry_34";
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
PropertyValues pvs = new PropertyValues();
pvs.add(new PropertyValue("age", 55));
pvs.add(new PropertyValue("name", newName));
pvs.add(new PropertyValue("touchy", "valid"));
pvs.add(new PropertyValue("spouse", tbString));
bw.setPropertyValues(pvs);
assertThat(tb.getSpouse()).as("spouse is non-null").isNotNull();
assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue();
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
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"));
}
use of cn.taketoday.beans.BeanWrapper in project today-infrastructure by TAKETODAY.
the class CustomEditorTests method testCustomEditorForAllStringProperties.
@Test
void testCustomEditorForAllStringProperties() {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
});
bw.setPropertyValue("name", "value");
bw.setPropertyValue("touchy", "value");
assertThat(bw.getPropertyValue("name")).isEqualTo("prefixvalue");
assertThat(tb.getName()).isEqualTo("prefixvalue");
assertThat(bw.getPropertyValue("touchy")).isEqualTo("prefixvalue");
assertThat(tb.getTouchy()).isEqualTo("prefixvalue");
}
Aggregations