Skip to content

Commit

Permalink
Polish StringArrayPropertyEditor[Tests]
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 29, 2019
1 parent 7cedffc commit d9ebc3b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 49 deletions.
@@ -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.
Expand Down Expand Up @@ -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.
*/
Expand All @@ -62,7 +62,7 @@ public StringArrayPropertyEditor() {
}

/**
* 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}
*/
Expand All @@ -71,7 +71,7 @@ public StringArrayPropertyEditor(String separator) {
}

/**
* 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}
Expand All @@ -81,19 +81,19 @@ public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull) {
}

/**
* 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:
Expand All @@ -106,15 +106,15 @@ public StringArrayPropertyEditor(String separator, @Nullable String charsToDelet
}

/**
* 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:
* e.g. "\r\n\f" will delete all new lines and line feeds in a 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, @Nullable String charsToDelete, boolean emptyArrayAsNull, boolean trimValues) {
Expand All @@ -128,13 +128,13 @@ public StringArrayPropertyEditor(
@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);
}
}
Expand Down
Expand Up @@ -23,39 +23,31 @@
/**
* @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;
Expand All @@ -67,48 +59,45 @@ public void noTrim() throws Exception {
}

@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));
}
}

}
Expand Up @@ -1002,8 +1002,8 @@ public static String[] sortStringArray(String[] array) {
}

/**
* 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
*/
Expand Down

0 comments on commit d9ebc3b

Please sign in to comment.