Example usage for org.springframework.beans AbstractPropertyAccessor registerCustomEditor

List of usage examples for org.springframework.beans AbstractPropertyAccessor registerCustomEditor

Introduction

In this page you can find the example usage for org.springframework.beans AbstractPropertyAccessor registerCustomEditor.

Prototype

@Override
    public void registerCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath,
            PropertyEditor propertyEditor) 

Source Link

Usage

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/*from w  w w .java2s .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// w w  w.j a va  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 setStringArrayPropertyWithCustomStringDelimiter() throws Exception {
    PropsTester target = new PropsTester();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String[].class, "stringArray", new StringArrayPropertyEditor("-"));
    accessor.setPropertyValue("stringArray", "a1-b2");
    assertTrue("stringArray length = 2", target.stringArray.length == 2);
    assertTrue("correct values", target.stringArray[0].equals("a1") && target.stringArray[1].equals("b2"));
}

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   w ww.  j  a  v a 2s  .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(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 a v  a  2  s. co  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(0, target.getArray()[0]);
    assertEquals(1, target.getArray()[1]);
}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
    accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

    accessor.setPropertyValue("set", "set1 ");
    accessor.setPropertyValue("sortedSet", "sortedSet1");
    accessor.setPropertyValue("list", "list1 ");
    assertEquals(1, target.getSet().size());
    assertTrue(target.getSet().contains("set1"));
    assertEquals(1, target.getSortedSet().size());
    assertTrue(target.getSortedSet().contains("sortedSet1"));
    assertEquals(1, target.getList().size());
    assertTrue(target.getList().contains("list1"));

    accessor.setPropertyValue("list", Collections.singletonList("list1 "));
    assertTrue(target.getList().contains("list1"));
}

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  ww  w .  jav a2s  . c  o m*/
        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//from  w w w  .  ja  v a2 s . c  om
        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.AbstractPropertyAccessorTests.java

@Test
public void propertyTypeIndexedProperty() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    assertEquals(null, accessor.getPropertyType("map[key0]"));

    accessor = createAccessor(target);/*from   www  . java  2s. c om*/
    accessor.setPropertyValue("map[key0]", "my String");
    assertEquals(String.class, accessor.getPropertyType("map[key0]"));

    accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
    assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}