List of usage examples for java.beans PropertyEditorSupport PropertyEditorSupport
public PropertyEditorSupport()
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@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/*from w ww . j a va 2s. co m*/ public void setValue(Object value) { super.setValue(value.toString()); } }); accessor.setPropertyValues(values); assertEquals(Integer.class.toString(), target.getName()); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setStringPropertyWithCustomEditor() throws Exception { TestBean target = new TestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "name", new PropertyEditorSupport() { @Override// w w w. j a va2 s.c o m 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()); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setStringArrayPropertyWithCustomStringEditor() throws Exception { PropsTester target = new PropsTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() { @Override/*from ww w . j av a 2s. c om*/ 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")); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setIntArrayPropertyWithCustomEditor() { PropsTester target = new PropsTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(int.class, new PropertyEditorSupport() { @Override// w w w .j a v a 2 s .c om 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); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() { PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(int.class, "array", new PropertyEditorSupport() { @Override//from ww w .j ava 2 s .c o m 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]); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() { PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() { @Override//from ww w . j av a2s . c om 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]); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setMapPropertyWithTypeConversion() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() { @Override//from w w w. ja va 2s . c om 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); } }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setMapPropertyWithUnmodifiableMap() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override/*from w w w . j av a 2s .c om*/ 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()); }
From source file:org.springframework.beans.AbstractPropertyAccessorTests.java
@Test public void setMapPropertyWithCustomUnmodifiableMap() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override// w w w .ja v a2s . c o m 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()); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testConvertClassToString() { MutablePropertyValues values = new MutablePropertyValues(); values.add("name", Integer.class); TestBean tb = new TestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override/*from ww w . j a v a 2 s . c o m*/ public void setValue(Object value) { super.setValue(value.toString()); } }); bw.setPropertyValues(values); assertEquals(Integer.class.toString(), tb.getName()); }