use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithNestedObjectCreation.
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "person");
binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean());
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("spouse", "someValue");
pvs.add("spouse.name", "test");
binder.bind(pvs);
assertNotNull(tb.getSpouse());
assertEquals("test", tb.getSpouse().getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testCustomEditorWithOldValueAccess.
@Test
public void testCustomEditorWithOldValueAccess() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, null, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (getValue() == null || !text.equalsIgnoreCase(getValue().toString())) {
setValue(text);
}
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "value");
binder.bind(pvs);
assertEquals("value", tb.getName());
pvs = new MutablePropertyValues();
pvs.add("name", "vaLue");
binder.bind(pvs);
assertEquals("value", tb.getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor.
@Test
public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() {
PrimitiveArrayBean target = new PrimitiveArrayBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {
@Override
public void setValue(Object value) {
if (value instanceof Integer) {
super.setValue(new Integer((Integer) value + 1));
}
}
});
int[] input = new int[1024];
accessor.setPropertyValue("array", input);
assertEquals(1024, target.getArray().length);
assertEquals(0, target.getArray()[0]);
assertEquals(1, target.getArray()[1]);
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setStringPropertyWithCustomEditor.
@Test
public void setStringPropertyWithCustomEditor() throws Exception {
TestBean target = new TestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
@Override
public void setValue(Object value) {
if (value instanceof String[]) {
setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-"));
} else {
super.setValue(value != null ? value : "");
}
}
});
accessor.setPropertyValue("name", new String[] {});
assertEquals("", target.getName());
accessor.setPropertyValue("name", new String[] { "a1", "b2" });
assertEquals("a1-b2", target.getName());
accessor.setPropertyValue("name", null);
assertEquals("", target.getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor.
@Test
public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() {
PrimitiveArrayBean target = new PrimitiveArrayBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {
@Override
public void setValue(Object value) {
if (value instanceof Integer) {
super.setValue(new Integer((Integer) value + 1));
}
}
});
int[] input = new int[1024];
accessor.setPropertyValue("array", input);
assertEquals(1024, target.getArray().length);
assertEquals(1, target.getArray()[0]);
assertEquals(1, target.getArray()[1]);
}
Aggregations