use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class CustomEditorTests method testArrayToStringConversion.
@Test
public void testArrayToStringConversion() throws PropertyVetoException {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("-" + text + "-");
}
});
bw.setPropertyValue("name", new String[] { "a", "b" });
assertEquals("-a,b-", tb.getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setMapPropertyWithUnmodifiableMap.
@Test
public void setMapPropertyWithUnmodifiableMap() {
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<Integer, String> inputMap = new HashMap<>();
inputMap.put(1, "rod");
inputMap.put(2, "rob");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("map", Collections.unmodifiableMap(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 AbstractPropertyAccessorTests method setPropertyWithCustomEditor.
@Test
public void setPropertyWithCustomEditor() {
MutablePropertyValues values = new MutablePropertyValues();
values.add("name", Integer.class);
TestBean target = new TestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setValue(Object value) {
super.setValue(value.toString());
}
});
accessor.setPropertyValues(values);
assertEquals(Integer.class.toString(), target.getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setStringArrayPropertyWithCustomStringEditor.
@Test
public void setStringArrayPropertyWithCustomStringEditor() throws Exception {
PropsTester target = new PropsTester();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(text.substring(1));
}
});
accessor.setPropertyValue("stringArray", new String[] { "4foo", "7fi", "6fi", "5fum" });
assertTrue("stringArray length = 4", target.stringArray.length == 4);
assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum"));
List<String> list = new ArrayList<>();
list.add("4foo");
list.add("7fi");
list.add("6fi");
list.add("5fum");
accessor.setPropertyValue("stringArray", list);
assertTrue("stringArray length = 4", target.stringArray.length == 4);
assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum"));
Set<String> set = new HashSet<>();
set.add("4foo");
set.add("7fi");
set.add("6fum");
accessor.setPropertyValue("stringArray", set);
assertTrue("stringArray length = 3", target.stringArray.length == 3);
List<String> result = Arrays.asList(target.stringArray);
assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));
accessor.setPropertyValue("stringArray", "8one");
assertTrue("stringArray length = 1", target.stringArray.length == 1);
assertTrue("correct values", target.stringArray[0].equals("one"));
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setIntArrayPropertyWithCustomEditor.
@Test
public void setIntArrayPropertyWithCustomEditor() {
PropsTester target = new PropsTester();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(int.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(new Integer(Integer.parseInt(text) + 1));
}
});
accessor.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 });
assertTrue("intArray length = 4", target.intArray.length == 4);
assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2 && target.intArray[3] == 3);
accessor.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" });
assertTrue("intArray length = 4", target.intArray.length == 4);
assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2 && target.intArray[3] == 3);
accessor.setPropertyValue("intArray", new Integer(1));
assertTrue("intArray length = 4", target.intArray.length == 1);
assertTrue("correct values", target.intArray[0] == 1);
accessor.setPropertyValue("intArray", new String[] { "0" });
assertTrue("intArray length = 4", target.intArray.length == 1);
assertTrue("correct values", target.intArray[0] == 1);
accessor.setPropertyValue("intArray", "0");
assertTrue("intArray length = 4", target.intArray.length == 1);
assertTrue("correct values", target.intArray[0] == 1);
}
Aggregations