Polish StringArrayPropertyEditor[Tests]
This commit is contained in:
parent
7cedffc707
commit
d9ebc3bbc4
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -53,7 +53,7 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the default separator
|
||||
* Create a new {@code StringArrayPropertyEditor} with the default separator
|
||||
* (a comma).
|
||||
* <p>An empty text (without elements) will be turned into an empty array.
|
||||
*/
|
||||
|
|
@ -62,7 +62,7 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the given separator.
|
||||
* Create a new {@code StringArrayPropertyEditor} with the given separator.
|
||||
* <p>An empty text (without elements) will be turned into an empty array.
|
||||
* @param separator the separator to use for splitting a {@link String}
|
||||
*/
|
||||
|
|
@ -71,7 +71,7 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the given separator.
|
||||
* Create a new {@code StringArrayPropertyEditor} with the given separator.
|
||||
* @param separator the separator to use for splitting a {@link String}
|
||||
* @param emptyArrayAsNull {@code true} if an empty String array
|
||||
* is to be transformed into {@code null}
|
||||
|
|
@ -81,19 +81,19 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the given separator.
|
||||
* Create a new {@code StringArrayPropertyEditor} with the given separator.
|
||||
* @param separator the separator to use for splitting a {@link String}
|
||||
* @param emptyArrayAsNull {@code true} if an empty String array
|
||||
* is to be transformed into {@code null}
|
||||
* @param trimValues {@code true} if the values in the parsed arrays
|
||||
* are to be trimmed of whitespace (default is true).
|
||||
* are to be trimmed of whitespace (default is true)
|
||||
*/
|
||||
public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull, boolean trimValues) {
|
||||
this(separator, null, emptyArrayAsNull, trimValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the given separator.
|
||||
* Create a new {@code StringArrayPropertyEditor} with the given separator.
|
||||
* @param separator the separator to use for splitting a {@link String}
|
||||
* @param charsToDelete a set of characters to delete, in addition to
|
||||
* trimming an input String. Useful for deleting unwanted line breaks:
|
||||
|
|
@ -106,7 +106,7 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new StringArrayPropertyEditor with the given separator.
|
||||
* Create a new {@code StringArrayPropertyEditor} with the given separator.
|
||||
* @param separator the separator to use for splitting a {@link String}
|
||||
* @param charsToDelete a set of characters to delete, in addition to
|
||||
* trimming an input String. Useful for deleting unwanted line breaks:
|
||||
|
|
@ -114,7 +114,7 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
* @param emptyArrayAsNull {@code true} if an empty String array
|
||||
* is to be transformed into {@code null}
|
||||
* @param trimValues {@code true} if the values in the parsed arrays
|
||||
* are to be trimmed of whitespace (default is true).
|
||||
* are to be trimmed of whitespace (default is true)
|
||||
*/
|
||||
public StringArrayPropertyEditor(
|
||||
String separator, @Nullable String charsToDelete, boolean emptyArrayAsNull, boolean trimValues) {
|
||||
|
|
@ -128,13 +128,13 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
|
|||
@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 {
|
||||
if (this.trimValues) {
|
||||
array = StringUtils.trimArrayElements(array);
|
||||
}
|
||||
setValue(array);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,39 +23,31 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class StringArrayPropertyEditorTests {
|
||||
class StringArrayPropertyEditorTests {
|
||||
|
||||
@Test
|
||||
public void withDefaultSeparator() throws Exception {
|
||||
void withDefaultSeparator() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
|
||||
editor.setAsText("0,1,2");
|
||||
Object value = editor.getValue();
|
||||
assertThat(value).isNotNull();
|
||||
boolean condition = value instanceof String[];
|
||||
assertThat(condition).isTrue();
|
||||
String[] array = (String[]) value;
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
assertThat(array[i]).isEqualTo(("" + i));
|
||||
}
|
||||
assertTrimmedElements(value);
|
||||
assertThat(editor.getAsText()).isEqualTo("0,1,2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimByDefault() throws Exception {
|
||||
void trimByDefault() {
|
||||
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) {
|
||||
assertThat(array[i]).isEqualTo(("" + i));
|
||||
}
|
||||
assertTrimmedElements(value);
|
||||
assertThat(editor.getAsText()).isEqualTo("0,1,2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noTrim() throws Exception {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false);
|
||||
void noTrim() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", false, false);
|
||||
editor.setAsText(" 0,1 , 2 ");
|
||||
Object value = editor.getValue();
|
||||
String[] array = (String[]) value;
|
||||
|
|
@ -67,48 +59,45 @@ public class StringArrayPropertyEditorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void withCustomSeparator() throws Exception {
|
||||
void withCustomSeparator() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":");
|
||||
editor.setAsText("0:1:2");
|
||||
Object value = editor.getValue();
|
||||
boolean condition = value instanceof String[];
|
||||
assertThat(condition).isTrue();
|
||||
String[] array = (String[]) value;
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
assertThat(array[i]).isEqualTo(("" + i));
|
||||
}
|
||||
assertTrimmedElements(value);
|
||||
assertThat(editor.getAsText()).isEqualTo("0:1:2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withCharsToDelete() throws Exception {
|
||||
void withCharsToDelete() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", "\r\n", false);
|
||||
editor.setAsText("0\r,1,\n2");
|
||||
Object value = editor.getValue();
|
||||
boolean condition = value instanceof String[];
|
||||
assertThat(condition).isTrue();
|
||||
String[] array = (String[]) value;
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
assertThat(array[i]).isEqualTo(("" + i));
|
||||
}
|
||||
assertTrimmedElements(value);
|
||||
assertThat(editor.getAsText()).isEqualTo("0,1,2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withEmptyArray() throws Exception {
|
||||
void withEmptyArray() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
|
||||
editor.setAsText("");
|
||||
Object value = editor.getValue();
|
||||
boolean condition = value instanceof String[];
|
||||
assertThat(condition).isTrue();
|
||||
assertThat(((String[]) value).length).isEqualTo(0);
|
||||
assertThat(value).isInstanceOf(String[].class);
|
||||
assertThat((String[]) value).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withEmptyArrayAsNull() throws Exception {
|
||||
void withEmptyArrayAsNull() {
|
||||
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",", true);
|
||||
editor.setAsText("");
|
||||
assertThat(editor.getValue()).isNull();
|
||||
}
|
||||
|
||||
private static void assertTrimmedElements(Object value) {
|
||||
assertThat(value).isInstanceOf(String[].class);
|
||||
String[] array = (String[]) value;
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
assertThat(array[i]).isEqualTo(("" + i));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1002,8 +1002,8 @@ public abstract class StringUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Trim the elements of the given {@code String} array,
|
||||
* calling {@code String.trim()} on each of them.
|
||||
* Trim the elements of the given {@code String} array, calling
|
||||
* {@code String.trim()} on each non-null element.
|
||||
* @param array the original {@code String} array (potentially empty)
|
||||
* @return the resulting array (of the same size) with trimmed elements
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue