Polishing

This commit is contained in:
Sam Brannen 2023-08-26 14:37:59 +02:00
parent 44167ecea4
commit ef01a078d3
4 changed files with 54 additions and 55 deletions

View File

@ -478,5 +478,4 @@ public abstract class CollectionUtils {
return new UnmodifiableMultiValueMap<>(targetMap); return new UnmodifiableMultiValueMap<>(targetMap);
} }
} }

View File

@ -16,8 +16,9 @@
package org.springframework.util; package org.springframework.util;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -26,9 +27,12 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat; 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}. * Tests for {@link MultiValueMap}.
@ -37,11 +41,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Sam Brannen
*/ */
class MultiValueMapTests { class MultiValueMapTests {
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void add(MultiValueMap<String, String> map) { void add(MultiValueMap<String, String> map) {
int initialSize = map.size(); int initialSize = map.size();
map.add("key", "value1"); map.add("key", "value1");
@ -50,31 +54,27 @@ class MultiValueMapTests {
assertThat(map.get("key")).containsExactly("value1", "value2"); assertThat(map.get("key")).containsExactly("value1", "value2");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void addIfAbsentWhenAbsent(MultiValueMap<String, String> map) { void addIfAbsentWhenAbsent(MultiValueMap<String, String> map) {
map.addIfAbsent("key", "value1"); map.addIfAbsent("key", "value1");
assertThat(map.get("key")).containsExactly("value1"); assertThat(map.get("key")).containsExactly("value1");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void addIfAbsentWhenPresent(MultiValueMap<String, String> map) { void addIfAbsentWhenPresent(MultiValueMap<String, String> map) {
map.add("key", "value1"); map.add("key", "value1");
map.addIfAbsent("key", "value2"); map.addIfAbsent("key", "value2");
assertThat(map.get("key")).containsExactly("value1"); assertThat(map.get("key")).containsExactly("value1");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void set(MultiValueMap<String, String> map) { void set(MultiValueMap<String, String> map) {
map.set("key", "value1"); map.set("key", "value1");
map.set("key", "value2"); map.set("key", "value2");
assertThat(map.get("key")).containsExactly("value2"); assertThat(map.get("key")).containsExactly("value2");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void addAll(MultiValueMap<String, String> map) { void addAll(MultiValueMap<String, String> map) {
int initialSize = map.size(); int initialSize = map.size();
map.add("key", "value1"); map.add("key", "value1");
@ -83,18 +83,16 @@ class MultiValueMapTests {
assertThat(map.get("key")).containsExactly("value1", "value2", "value3"); assertThat(map.get("key")).containsExactly("value1", "value2", "value3");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void addAllWithEmptyList(MultiValueMap<String, String> map) { void addAllWithEmptyList(MultiValueMap<String, String> map) {
int initialSize = map.size(); int initialSize = map.size();
map.addAll("key", Collections.emptyList()); map.addAll("key", List.of());
assertThat(map).hasSize(initialSize + 1); assertThat(map).hasSize(initialSize + 1);
assertThat(map.get("key")).isEmpty(); assertThat(map.get("key")).isEmpty();
assertThat(map.getFirst("key")).isNull(); assertThat(map.getFirst("key")).isNull();
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void getFirst(MultiValueMap<String, String> map) { void getFirst(MultiValueMap<String, String> map) {
List<String> values = List.of("value1", "value2"); List<String> values = List.of("value1", "value2");
map.put("key", values); map.put("key", values);
@ -102,8 +100,7 @@ class MultiValueMapTests {
assertThat(map.getFirst("other")).isNull(); assertThat(map.getFirst("other")).isNull();
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void toSingleValueMap(MultiValueMap<String, String> map) { void toSingleValueMap(MultiValueMap<String, String> map) {
int initialSize = map.size(); int initialSize = map.size();
List<String> values = List.of("value1", "value2"); List<String> values = List.of("value1", "value2");
@ -113,41 +110,36 @@ class MultiValueMapTests {
assertThat(singleValueMap.get("key")).isEqualTo("value1"); assertThat(singleValueMap.get("key")).isEqualTo("value1");
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void toSingleValueMapWithEmptyList(MultiValueMap<String, String> map) { void toSingleValueMapWithEmptyList(MultiValueMap<String, String> map) {
int initialSize = map.size(); int initialSize = map.size();
map.put("key", Collections.emptyList()); map.put("key", List.of());
Map<String, String> singleValueMap = map.toSingleValueMap(); Map<String, String> singleValueMap = map.toSingleValueMap();
assertThat(singleValueMap).hasSize(initialSize); assertThat(singleValueMap).hasSize(initialSize);
assertThat(singleValueMap.get("key")).isNull(); assertThat(singleValueMap.get("key")).isNull();
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void equalsOnExistingValues(MultiValueMap<String, String> map) { void equalsOnExistingValues(MultiValueMap<String, String> map) {
map.clear(); map.clear();
map.set("key1", "value1"); map.set("key1", "value1");
assertThat(map).isEqualTo(map); assertThat(map).isEqualTo(map);
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void equalsOnEmpty(MultiValueMap<String, String> map) { void equalsOnEmpty(MultiValueMap<String, String> map) {
map.clear(); map.clear();
map.set("key1", "value1"); map.set("key1", "value1");
MultiValueMap<String, String> o1 = new LinkedMultiValueMap<>(); MultiValueMap<String, String> map1 = new LinkedMultiValueMap<>();
o1.set("key1", "value1"); map1.set("key1", "value1");
assertThat(o1).isEqualTo(map); assertThat(map1).isEqualTo(map);
assertThat(map).isEqualTo(o1); assertThat(map).isEqualTo(map1);
Map<String, List<String>> o2 = new HashMap<>(); Map<String, List<String>> map2 = Map.of("key1", List.of("value1"));
o2.put("key1", Collections.singletonList("value1")); assertThat(map2).isEqualTo(map);
assertThat(o2).isEqualTo(map); assertThat(map).isEqualTo(map2);
assertThat(map).isEqualTo(o2);
} }
@ParameterizedTest @ParameterizedMultiValueMapTest
@MethodSource("objectsUnderTest")
void canNotChangeAnUnmodifiableMultiValueMap(MultiValueMap<String, String> map) { void canNotChangeAnUnmodifiableMultiValueMap(MultiValueMap<String, String> map) {
MultiValueMap<String, String> asUnmodifiableMultiValueMap = CollectionUtils.unmodifiableMultiValueMap(map); MultiValueMap<String, String> asUnmodifiableMultiValueMap = CollectionUtils.unmodifiableMultiValueMap(map);
Assertions.assertAll( Assertions.assertAll(
@ -171,28 +163,37 @@ class MultiValueMapTests {
() -> asUnmodifiableMultiValueMap.putAll(exampleMultiValueMap())), () -> asUnmodifiableMultiValueMap.putAll(exampleMultiValueMap())),
() -> Assertions.assertThrows(UnsupportedOperationException.class, () -> Assertions.assertThrows(UnsupportedOperationException.class,
() -> asUnmodifiableMultiValueMap.remove("key1"))); () -> asUnmodifiableMultiValueMap.remove("key1")));
} }
private List<String> exampleListOfValues() { private static List<String> exampleListOfValues() {
return List.of("value1", "value2"); return List.of("value1", "value2");
} }
private Map<String, String> exampleHashMap() { private static Map<String, String> exampleHashMap() {
return Map.of("key2", "key2.value1"); return Map.of("key2", "key2.value1");
} }
private MultiValueMap<String, String> exampleMultiValueMap() { private static MultiValueMap<String, String> exampleMultiValueMap() {
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.put("key1", Arrays.asList("key1.value1", "key1.value2")); map.put("key1", Arrays.asList("key1.value1", "key1.value2"));
return map; return map;
} }
static Stream<MultiValueMap<String, String>> objectsUnderTest() {
return Stream.of(new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(new HashMap<>()), @Retention(RetentionPolicy.RUNTIME)
new LinkedMultiValueMap<>(new LinkedHashMap<>()), @ParameterizedTest(name = "[{index}] {0}")
new LinkedMultiValueMap<>(Map.of("existingkey", Arrays.asList("existingvalue1", "existingvalue2"))), @MethodSource("mapsUnderTest")
CollectionUtils.toMultiValueMap(new HashMap<>())); @interface ParameterizedMultiValueMapTest {
}
static Stream<Arguments> 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<>())))
);
} }
} }

View File

@ -30,25 +30,23 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
*/ */
@Deprecated
class ComparableComparatorTests { class ComparableComparatorTests {
@Test @Test
void comparableComparator() { void comparableComparator() {
@SuppressWarnings("deprecation")
Comparator<String> c = new ComparableComparator<>(); Comparator<String> c = new ComparableComparator<>();
String s1 = "abc"; assertThat(c.compare("abc", "cde")).isLessThan(0);
String s2 = "cde";
assertThat(c.compare(s1, s2)).isLessThan(0);
} }
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test @Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void shouldNeedComparable() { void shouldNeedComparable() {
@SuppressWarnings("deprecation")
Comparator c = new ComparableComparator(); Comparator c = new ComparableComparator();
Object o1 = new Object(); Object o1 = new Object();
Object o2 = new Object(); Object o2 = new Object();
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> c.compare(o1, o2));
c.compare(o1, o2));
} }
} }

View File

@ -29,19 +29,20 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
*/ */
@Deprecated
class NullSafeComparatorTests { class NullSafeComparatorTests {
@SuppressWarnings("unchecked")
@Test @Test
@SuppressWarnings("unchecked")
void shouldCompareWithNullsLow() { void shouldCompareWithNullsLow() {
@SuppressWarnings("deprecation")
Comparator<String> c = NullSafeComparator.NULLS_LOW; Comparator<String> c = NullSafeComparator.NULLS_LOW;
assertThat(c.compare(null, "boo")).isLessThan(0); assertThat(c.compare(null, "boo")).isLessThan(0);
} }
@SuppressWarnings("unchecked")
@Test @Test
@SuppressWarnings("unchecked")
void shouldCompareWithNullsHigh() { void shouldCompareWithNullsHigh() {
@SuppressWarnings("deprecation")
Comparator<String> c = NullSafeComparator.NULLS_HIGH; Comparator<String> c = NullSafeComparator.NULLS_HIGH;
assertThat(c.compare(null, "boo")).isGreaterThan(0); assertThat(c.compare(null, "boo")).isGreaterThan(0);
assertThat(c.compare(null, null)).isEqualTo(0); assertThat(c.compare(null, null)).isEqualTo(0);