use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setMapPropertyWithTypeConversion.
@Test
public void setMapPropertyWithTypeConversion() {
IndexedTestBean target = new IndexedTestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException();
}
setValue(new TestBean(text));
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("map[key1]", "rod");
pvs.add("map[key2]", "rob");
accessor.setPropertyValues(pvs);
assertEquals("rod", ((TestBean) target.getMap().get("key1")).getName());
assertEquals("rob", ((TestBean) target.getMap().get("key2")).getName());
pvs = new MutablePropertyValues();
pvs.add("map[key1]", "rod");
pvs.add("map[key2]", "");
try {
accessor.setPropertyValues(pvs);
fail("Should have thrown TypeMismatchException");
} catch (PropertyBatchUpdateException ex) {
PropertyAccessException pae = ex.getPropertyAccessException("map[key2]");
assertTrue(pae instanceof TypeMismatchException);
}
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setMapPropertyWithCustomUnmodifiableMap.
@Test
public void setMapPropertyWithCustomUnmodifiableMap() {
IndexedTestBean target = new IndexedTestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException();
}
setValue(new TestBean(text));
}
});
Map<Object, Object> inputMap = new HashMap<>();
inputMap.put(1, "rod");
inputMap.put(2, "rob");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("map", new ReadOnlyMap<>(inputMap));
accessor.setPropertyValues(pvs);
assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderFieldAccessTests method bindingWithErrorsAndCustomEditors.
@Test
public void bindingWithErrorsAndCustomEditors() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.initDirectFieldAccess();
binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean(text, 0));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
pvs.addPropertyValue(new PropertyValue("age", "32x"));
pvs.addPropertyValue(new PropertyValue("spouse", "Kerry"));
binder.bind(pvs);
try {
binder.close();
fail("Should have thrown BindException");
} catch (BindException ex) {
assertTrue("changed name correctly", rod.getName().equals("Rod"));
//assertTrue("changed age correctly", rod.getAge() == 32);
Map<?, ?> model = binder.getBindingResult().getModel();
//assertTrue("There are 3 element in map", m.size() == 1);
FieldAccessBean tb = (FieldAccessBean) model.get("person");
assertTrue("Same object", tb.equals(rod));
BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
assertTrue("Added itself to map", br == binder.getBindingResult());
assertTrue(br.hasErrors());
assertTrue("Correct number of errors", br.getErrorCount() == 1);
assertTrue("Has age errors", br.hasFieldErrors("age"));
assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
assertEquals(0, tb.getAge());
assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
assertNotNull(tb.getSpouse());
}
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testSpecificEditorForNestedIndexedField.
@Test
public void testSpecificEditorForNestedIndexedField() {
IndexedTestBean tb = new IndexedTestBean();
tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
@Override
public String getAsText() {
return ((String) getValue()).substring(4);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
binder.bind(pvs);
assertEquals("listtest1", ((TestBean) tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
assertEquals("test2", ((TestBean) tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor.
@Test
public void testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
Errors errors = binder.getBindingResult();
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
assertEquals(1, errors.getFieldErrorCount("map[key0]"));
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCode());
assertEquals("NOT_NULL.tb.map[key0]", errors.getFieldError("map[key0]").getCodes()[0]);
assertEquals("NOT_NULL.tb.map", errors.getFieldError("map[key0]").getCodes()[1]);
assertEquals("NOT_NULL.map[key0]", errors.getFieldError("map[key0]").getCodes()[2]);
assertEquals("NOT_NULL.map", errors.getFieldError("map[key0]").getCodes()[3]);
// This next code is only generated because of the registered editor, using the
// registered type of the editor as guess for the content type of the collection.
assertEquals("NOT_NULL.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
Aggregations