diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 86156fef3bf..bf47d0b917f 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -478,5 +478,4 @@ public abstract class CollectionUtils { return new UnmodifiableMultiValueMap<>(targetMap); } - } diff --git a/spring-core/src/test/java/org/springframework/util/MultiValueMapTests.java b/spring-core/src/test/java/org/springframework/util/MultiValueMapTests.java index 0f7128fbf2e..61a8ebf8ba2 100644 --- a/spring-core/src/test/java/org/springframework/util/MultiValueMapTests.java +++ b/spring-core/src/test/java/org/springframework/util/MultiValueMapTests.java @@ -16,8 +16,9 @@ package org.springframework.util; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -26,9 +27,12 @@ import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Named.named; +import static org.junit.jupiter.params.provider.Arguments.arguments; /** * Tests for {@link MultiValueMap}. @@ -37,11 +41,11 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Arjen Poutsma * @author Juergen Hoeller * @author Stephane Nicoll + * @author Sam Brannen */ class MultiValueMapTests { - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void add(MultiValueMap map) { int initialSize = map.size(); map.add("key", "value1"); @@ -50,31 +54,27 @@ class MultiValueMapTests { assertThat(map.get("key")).containsExactly("value1", "value2"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void addIfAbsentWhenAbsent(MultiValueMap map) { map.addIfAbsent("key", "value1"); assertThat(map.get("key")).containsExactly("value1"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void addIfAbsentWhenPresent(MultiValueMap map) { map.add("key", "value1"); map.addIfAbsent("key", "value2"); assertThat(map.get("key")).containsExactly("value1"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void set(MultiValueMap map) { map.set("key", "value1"); map.set("key", "value2"); assertThat(map.get("key")).containsExactly("value2"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void addAll(MultiValueMap map) { int initialSize = map.size(); map.add("key", "value1"); @@ -83,18 +83,16 @@ class MultiValueMapTests { assertThat(map.get("key")).containsExactly("value1", "value2", "value3"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void addAllWithEmptyList(MultiValueMap map) { int initialSize = map.size(); - map.addAll("key", Collections.emptyList()); + map.addAll("key", List.of()); assertThat(map).hasSize(initialSize + 1); assertThat(map.get("key")).isEmpty(); assertThat(map.getFirst("key")).isNull(); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void getFirst(MultiValueMap map) { List values = List.of("value1", "value2"); map.put("key", values); @@ -102,8 +100,7 @@ class MultiValueMapTests { assertThat(map.getFirst("other")).isNull(); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void toSingleValueMap(MultiValueMap map) { int initialSize = map.size(); List values = List.of("value1", "value2"); @@ -113,41 +110,36 @@ class MultiValueMapTests { assertThat(singleValueMap.get("key")).isEqualTo("value1"); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void toSingleValueMapWithEmptyList(MultiValueMap map) { int initialSize = map.size(); - map.put("key", Collections.emptyList()); + map.put("key", List.of()); Map singleValueMap = map.toSingleValueMap(); assertThat(singleValueMap).hasSize(initialSize); assertThat(singleValueMap.get("key")).isNull(); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void equalsOnExistingValues(MultiValueMap map) { map.clear(); map.set("key1", "value1"); assertThat(map).isEqualTo(map); } - @ParameterizedTest - @MethodSource("objectsUnderTest") + @ParameterizedMultiValueMapTest void equalsOnEmpty(MultiValueMap map) { map.clear(); map.set("key1", "value1"); - MultiValueMap o1 = new LinkedMultiValueMap<>(); - o1.set("key1", "value1"); - assertThat(o1).isEqualTo(map); - assertThat(map).isEqualTo(o1); - Map> o2 = new HashMap<>(); - o2.put("key1", Collections.singletonList("value1")); - assertThat(o2).isEqualTo(map); - assertThat(map).isEqualTo(o2); - } - - @ParameterizedTest - @MethodSource("objectsUnderTest") + MultiValueMap map1 = new LinkedMultiValueMap<>(); + map1.set("key1", "value1"); + assertThat(map1).isEqualTo(map); + assertThat(map).isEqualTo(map1); + Map> map2 = Map.of("key1", List.of("value1")); + assertThat(map2).isEqualTo(map); + assertThat(map).isEqualTo(map2); + } + + @ParameterizedMultiValueMapTest void canNotChangeAnUnmodifiableMultiValueMap(MultiValueMap map) { MultiValueMap asUnmodifiableMultiValueMap = CollectionUtils.unmodifiableMultiValueMap(map); Assertions.assertAll( @@ -171,28 +163,37 @@ class MultiValueMapTests { () -> asUnmodifiableMultiValueMap.putAll(exampleMultiValueMap())), () -> Assertions.assertThrows(UnsupportedOperationException.class, () -> asUnmodifiableMultiValueMap.remove("key1"))); - } - private List exampleListOfValues() { + private static List exampleListOfValues() { return List.of("value1", "value2"); } - private Map exampleHashMap() { + private static Map exampleHashMap() { return Map.of("key2", "key2.value1"); } - private MultiValueMap exampleMultiValueMap() { + private static MultiValueMap exampleMultiValueMap() { LinkedMultiValueMap map = new LinkedMultiValueMap<>(); map.put("key1", Arrays.asList("key1.value1", "key1.value2")); return map; } - static Stream> objectsUnderTest() { - return Stream.of(new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(new HashMap<>()), - new LinkedMultiValueMap<>(new LinkedHashMap<>()), - new LinkedMultiValueMap<>(Map.of("existingkey", Arrays.asList("existingvalue1", "existingvalue2"))), - CollectionUtils.toMultiValueMap(new HashMap<>())); + + @Retention(RetentionPolicy.RUNTIME) + @ParameterizedTest(name = "[{index}] {0}") + @MethodSource("mapsUnderTest") + @interface ParameterizedMultiValueMapTest { + } + + static Stream mapsUnderTest() { + return Stream.of( + arguments(named("new LinkedMultiValueMap<>()", new LinkedMultiValueMap<>())), + arguments(named("new LinkedMultiValueMap<>(new HashMap<>())", new LinkedMultiValueMap<>(new HashMap<>()))), + arguments(named("new LinkedMultiValueMap<>(new LinkedHashMap<>())", new LinkedMultiValueMap<>(new LinkedHashMap<>()))), + arguments(named("new LinkedMultiValueMap<>(Map.of(...))", new LinkedMultiValueMap<>(Map.of("existingkey", List.of("existingvalue1", "existingvalue2"))))), + arguments(named("CollectionUtils.toMultiValueMap", CollectionUtils.toMultiValueMap(new HashMap<>()))) + ); } } diff --git a/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java index 9f9235d8462..4fc4a1c9795 100644 --- a/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java @@ -30,25 +30,23 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Chris Beams * @author Phillip Webb */ -@Deprecated class ComparableComparatorTests { @Test void comparableComparator() { + @SuppressWarnings("deprecation") Comparator c = new ComparableComparator<>(); - String s1 = "abc"; - String s2 = "cde"; - assertThat(c.compare(s1, s2)).isLessThan(0); + assertThat(c.compare("abc", "cde")).isLessThan(0); } - @SuppressWarnings({ "unchecked", "rawtypes" }) @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) void shouldNeedComparable() { + @SuppressWarnings("deprecation") Comparator c = new ComparableComparator(); Object o1 = new Object(); Object o2 = new Object(); - assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> - c.compare(o1, o2)); + assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> c.compare(o1, o2)); } } diff --git a/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java index 03c20ff6c5a..46ee59fa5bf 100644 --- a/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java @@ -29,19 +29,20 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Chris Beams * @author Phillip Webb */ -@Deprecated class NullSafeComparatorTests { - @SuppressWarnings("unchecked") @Test + @SuppressWarnings("unchecked") void shouldCompareWithNullsLow() { + @SuppressWarnings("deprecation") Comparator c = NullSafeComparator.NULLS_LOW; assertThat(c.compare(null, "boo")).isLessThan(0); } - @SuppressWarnings("unchecked") @Test + @SuppressWarnings("unchecked") void shouldCompareWithNullsHigh() { + @SuppressWarnings("deprecation") Comparator c = NullSafeComparator.NULLS_HIGH; assertThat(c.compare(null, "boo")).isGreaterThan(0); assertThat(c.compare(null, null)).isEqualTo(0);