diff --git a/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java b/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java index a627e6f2349f..f56ac82af9cb 100644 --- a/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java +++ b/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -40,12 +40,13 @@ */ public class DefaultValueStyler implements ValueStyler { - private static final String EMPTY = "[empty]"; + private static final String EMPTY = "[[empty]]"; private static final String NULL = "[null]"; private static final String COLLECTION = "collection"; private static final String SET = "set"; private static final String LIST = "list"; private static final String MAP = "map"; + private static final String EMPTY_MAP = MAP + EMPTY; private static final String ARRAY = "array"; @@ -83,7 +84,7 @@ else if (value.getClass().isArray()) { private String style(Map value) { if (value.isEmpty()) { - return MAP + '[' + EMPTY + ']'; + return EMPTY_MAP; } StringJoiner result = new StringJoiner(", ", "[", "]"); @@ -101,7 +102,7 @@ private String style(Collection value) { String collectionType = getCollectionTypeString(value); if (value.isEmpty()) { - return collectionType + '[' + EMPTY + ']'; + return collectionType + EMPTY; } StringJoiner result = new StringJoiner(", ", "[", "]"); @@ -125,7 +126,7 @@ else if (value instanceof Set) { private String styleArray(Object[] array) { if (array.length == 0) { - return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + '[' + EMPTY + ']'; + return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + EMPTY; } StringJoiner result = new StringJoiner(", ", "[", "]"); diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index 53a0d2b614be..c3cbcb8d8637 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java @@ -710,11 +710,11 @@ public static String nullSafeToString(@Nullable Object[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (Object o : array) { - sj.add(String.valueOf(o)); + stringJoiner.add(String.valueOf(o)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -734,11 +734,11 @@ public static String nullSafeToString(@Nullable boolean[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (boolean b : array) { - sj.add(String.valueOf(b)); + stringJoiner.add(String.valueOf(b)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -758,11 +758,11 @@ public static String nullSafeToString(@Nullable byte[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (byte b : array) { - sj.add(String.valueOf(b)); + stringJoiner.add(String.valueOf(b)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -782,11 +782,11 @@ public static String nullSafeToString(@Nullable char[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (char c : array) { - sj.add('\'' + String.valueOf(c) + '\''); + stringJoiner.add('\'' + String.valueOf(c) + '\''); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -806,11 +806,11 @@ public static String nullSafeToString(@Nullable double[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (double d : array) { - sj.add(String.valueOf(d)); + stringJoiner.add(String.valueOf(d)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -830,11 +830,11 @@ public static String nullSafeToString(@Nullable float[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (float f : array) { - sj.add(String.valueOf(f)); + stringJoiner.add(String.valueOf(f)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -854,11 +854,11 @@ public static String nullSafeToString(@Nullable int[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (int i : array) { - sj.add(String.valueOf(i)); + stringJoiner.add(String.valueOf(i)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -878,11 +878,11 @@ public static String nullSafeToString(@Nullable long[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (long l : array) { - sj.add(String.valueOf(l)); + stringJoiner.add(String.valueOf(l)); } - return sj.toString(); + return stringJoiner.toString(); } /** @@ -902,11 +902,11 @@ public static String nullSafeToString(@Nullable short[] array) { if (length == 0) { return EMPTY_ARRAY; } - StringJoiner sj = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); + StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); for (short s : array) { - sj.add(String.valueOf(s)); + stringJoiner.add(String.valueOf(s)); } - return sj.toString(); + return stringJoiner.toString(); } } diff --git a/spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTest.java b/spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTests.java similarity index 60% rename from spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTest.java rename to spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTests.java index ee026babc6b6..30c88c68cb18 100644 --- a/spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTest.java +++ b/spring-core/src/test/java/org/springframework/core/style/DefaultValueStylerTests.java @@ -1,6 +1,20 @@ -package org.springframework.core.style; +/* + * 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import org.junit.Test; +package org.springframework.core.style; import java.util.Arrays; import java.util.Collections; @@ -10,32 +24,37 @@ import java.util.List; import java.util.Map; +import org.junit.Test; + import static org.assertj.core.api.Assertions.assertThat; -public class DefaultValueStylerTest { +/** + * Unit tests for {@link DefaultValueStyler}. + * + * @since 5.2 + */ +public class DefaultValueStylerTests { + + private final DefaultValueStyler styler = new DefaultValueStyler(); - private DefaultValueStyler styler = new DefaultValueStyler(); @Test - public void style() throws NoSuchMethodException { + public void styleBasics() throws NoSuchMethodException { assertThat(styler.style(null)).isEqualTo("[null]"); - assertThat(styler.style("str")).isEqualTo("'str'"); - assertThat(styler.style(String.class)).isEqualTo("String"); - assertThat(styler.style(String.class.getMethod("toString"))).isEqualTo("toString@String"); } @Test - public void style_plainObject() { - final Object obj = new Object(); + public void stylePlainObject() { + Object obj = new Object(); assertThat(styler.style(obj)).isEqualTo(String.valueOf(obj)); } @Test - public void style_map() { + public void styleMaps() { Map map = Collections.emptyMap(); assertThat(styler.style(map)).isEqualTo("map[[empty]]"); @@ -49,19 +68,19 @@ public void style_map() { } @Test - public void style_entry() { - final Map map = new LinkedHashMap<>(); + public void styleMapEntries() { + Map map = new LinkedHashMap<>(); map.put("key1", 1); map.put("key2", 2); - final Iterator> entries = map.entrySet().iterator(); + Iterator> entries = map.entrySet().iterator(); assertThat(styler.style(entries.next())).isEqualTo("'key1' -> 1"); assertThat(styler.style(entries.next())).isEqualTo("'key2' -> 2"); } @Test - public void style_collection() { + public void styleCollections() { List list = Collections.emptyList(); assertThat(styler.style(list)).isEqualTo("list[[empty]]"); @@ -73,27 +92,27 @@ public void style_collection() { } @Test - public void style_primitiveArray() { + public void stylePrimitiveArrays() { int[] array = new int[0]; assertThat(styler.style(array)).isEqualTo("array[[empty]]"); - array = new int[]{1}; + array = new int[] { 1 }; assertThat(styler.style(array)).isEqualTo("array[1]"); - array = new int[]{1, 2}; + array = new int[] { 1, 2 }; assertThat(styler.style(array)).isEqualTo("array[1, 2]"); } @Test - public void style_objectArray() { + public void styleObjectArrays() { String[] array = new String[0]; assertThat(styler.style(array)).isEqualTo("array[[empty]]"); - array = new String[]{"str1"}; + array = new String[] { "str1" }; assertThat(styler.style(array)).isEqualTo("array['str1']"); - array = new String[]{"str1", "str2"}; + array = new String[] { "str1", "str2" }; assertThat(styler.style(array)).isEqualTo("array['str1', 'str2']"); } -} \ No newline at end of file +}