@Test public void trimByDefault() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(); editor.setAsText(" 0,1 , 2 "); Object value = editor.getValue(); String[] array = (String[]) value; for (int i = 0; i < array.length; ++i) { assertEquals("" + i, array[i]); } assertEquals("0,1,2", editor.getAsText()); }
@Override public void setAsText(String text) throws IllegalArgumentException { String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete); if (this.trimValues) { array = StringUtils.trimArrayElements(array); } if (this.emptyArrayAsNull && array.length == 0) { setValue(null); } else { setValue(array); } }
@Test public void withEmptyArrayAsNull() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", true); editor.setAsText(""); assertNull(editor.getValue()); }
@Override public PropertyEditor getEditor() { return new StringArrayPropertyEditor(); } };
@Override public String getAsText() { return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator); }
StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae);
@Test public void withEmptyArray() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(); editor.setAsText(""); Object value = editor.getValue(); assertTrue(value instanceof String[]); assertEquals(0, ((String[]) value).length); }
@Override public String getAsText() { return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator); }
@Test public void withCustomSeparator() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":"); editor.setAsText("0:1:2"); Object value = editor.getValue(); assertTrue(value instanceof String[]); String[] array = (String[]) value; for (int i = 0; i < array.length; ++i) { assertEquals("" + i, array[i]); } assertEquals("0:1:2", editor.getAsText()); }
StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae);
@Override public String getAsText() { return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator); }
@Override public void setAsText(String text) throws IllegalArgumentException { String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete); if (this.trimValues) { array = StringUtils.trimArrayElements(array); } if (this.emptyArrayAsNull && array.length == 0) { setValue(null); } else { setValue(array); } }
@Test public void noTrim() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false); editor.setAsText(" 0,1 , 2 "); Object value = editor.getValue(); String[] array = (String[]) value; for (int i = 0; i < array.length; ++i) { assertEquals(3, array[i].length()); assertEquals("" + i, array[i].trim()); } assertEquals(" 0,1 , 2 ", editor.getAsText()); }
@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")); }
public String getAsText() { String[] array = (String[]) this.getValue(); return StringUtils.arrayToCommaDelimitedString(array); }
@Override public void setAsText(String text) throws IllegalArgumentException { String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete); if (trimValues) { array = StringUtils.trimArrayElements(array); } if (this.emptyArrayAsNull && array.length == 0) { setValue(null); } else { setValue(array); } }
@Test public void withCharsToDelete() throws Exception { StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", "\r\n", false); editor.setAsText("0\r,1,\n2"); Object value = editor.getValue(); assertTrue(value instanceof String[]); String[] array = (String[]) value; for (int i = 0; i < array.length; ++i) { assertEquals("" + i, array[i]); } assertEquals("0,1,2", editor.getAsText()); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String[].class, new StringArrayPropertyEditor(null)); }
@Override public String getAsText() { return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator); }
public void setAsText(String s) throws IllegalArgumentException { String[] array = StringUtils.commaDelimitedListToStringArray(s); setValue(array); }