PropsTester target = new PropsTester(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() { @Override public void setAsText(String text) {
@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")); }
@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()); }
@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")); }
@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()); }
@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()); }
@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]); }
@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]); }
@Test public void propertyTypeIndexedProperty() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); assertEquals(null, accessor.getPropertyType("map[key0]")); accessor = createAccessor(target); 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]")); }
@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()); }
IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException {
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false)); sw.start("array2"); for (int i = 0; i < 1000; i++) { assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125); accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); for (int i = 0; i < 1000; i++) { assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); for (int i = 0; i < 1000; i++) { assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false)); sw.start("array4"); for (int i = 0; i < 100; i++) {
@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); }
private AbstractPropertyAccessor buildAccessor(Object job) { boolean injectType = configuration.getInjectionType() == InjectionType.FIELD; AbstractPropertyAccessor accessor = injectType ? new DirectFieldAccessor(job) : new BeanWrapperImpl(job); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(configuration.getDateFormat()); CustomDateEditor customDateEditor = new CustomDateEditor(simpleDateFormat, true); accessor.registerCustomEditor(Date.class, customDateEditor); return accessor; } }