diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java index eee219ed9e8f..1a7a8ccc24f8 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java @@ -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). *

An empty text (without elements) will be turned into an empty array. */ @@ -62,7 +62,7 @@ public StringArrayPropertyEditor() { } /** - * Create a new StringArrayPropertyEditor with the given separator. + * Create a new {@code StringArrayPropertyEditor} with the given separator. *

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 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} @@ -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: @@ -106,7 +106,7 @@ 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: @@ -114,7 +114,7 @@ public StringArrayPropertyEditor(String separator, @Nullable String charsToDelet * @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 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); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java index 09716d04fd08..0583c78460b7 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java @@ -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; @@ -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)); + } + } + } diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 4b720ac8e7f6..24af9f14d571 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -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 */