diff --git a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java index 34324570c9a..1a96590db74 100644 --- a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java @@ -27,6 +27,9 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.Vector; import org.junit.jupiter.api.Test; @@ -176,6 +179,31 @@ class CollectionUtilsTests { assertThat(CollectionUtils.findFirstMatch(source, candidates)).isEqualTo("def"); } + @Test + void findValueOfType() { + List integerList = new ArrayList<>(); + integerList.add(1); + assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isEqualTo(1); + + Set integerSet = new HashSet<>(); + integerSet.add(2); + assertThat(CollectionUtils.findValueOfType(integerSet, Integer.class)).isEqualTo(2); + } + + @Test + void findValueOfTypeWithEmptyCollection() { + List emptyList = new ArrayList<>(); + assertThat(CollectionUtils.findValueOfType(emptyList, Integer.class)).isNull(); + } + + @Test + void findValueOfTypeWithMoreThanOneValue() { + List integerList = new ArrayList<>(); + integerList.add(1); + integerList.add(2); + assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isNull(); + } + @Test void hasUniqueObject() { List list = new ArrayList<>(); @@ -210,6 +238,96 @@ class CollectionUtilsTests { assertThat(CollectionUtils.hasUniqueObject(list)).isFalse(); } + @Test + void findCommonElementType() { + List integerList = new ArrayList<>(); + integerList.add(1); + integerList.add(2); + + assertThat(CollectionUtils.findCommonElementType(integerList)).isEqualTo(Integer.class); + } + + @Test + void findCommonElementTypeWithEmptyCollection() { + List emptyList = new ArrayList<>(); + assertThat(CollectionUtils.findCommonElementType(emptyList)).isNull(); + } + + @Test + void findCommonElementTypeWithDifferentElementType() { + List list = new ArrayList<>(); + list.add(1); + list.add("foo"); + assertThat(CollectionUtils.findCommonElementType(list)).isNull(); + } + + @Test + void firstElementWithSet() { + Set set = new HashSet<>(); + set.add(17); + set.add(3); + set.add(2); + set.add(1); + assertThat(CollectionUtils.firstElement(set)).isEqualTo(17); + } + + @Test + void firstElementWithSortedSet() { + SortedSet sortedSet = new TreeSet<>(); + sortedSet.add(17); + sortedSet.add(3); + sortedSet.add(2); + sortedSet.add(1); + assertThat(CollectionUtils.firstElement(sortedSet)).isEqualTo(1); + } + + @Test + void firstElementWithList() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + assertThat(CollectionUtils.firstElement(list)).isEqualTo(1); + } + + @Test + void lastElementWithSet() { + Set set = new HashSet<>(); + set.add(17); + set.add(3); + set.add(2); + set.add(1); + assertThat(CollectionUtils.lastElement(set)).isEqualTo(3); + } + + @Test + void lastElementWithSortedSet() { + SortedSet sortedSet = new TreeSet<>(); + sortedSet.add(17); + sortedSet.add(3); + sortedSet.add(2); + sortedSet.add(1); + assertThat(CollectionUtils.lastElement(sortedSet)).isEqualTo(17); + } + + @Test + void lastElementWithList() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + assertThat(CollectionUtils.lastElement(list)).isEqualTo(3); + } + + @Test + void toArray() { + Vector vector = new Vector<>(); + vector.add("foo"); + vector.add("bar"); + Enumeration enumeration = vector.elements(); + assertThat(CollectionUtils.toArray(enumeration, new String[]{})).containsExactly("foo", "bar"); + } + @Test void conversionOfEmptyMap() { MultiValueMap asMultiValueMap = CollectionUtils.toMultiValueMap(new HashMap<>()); @@ -234,6 +352,22 @@ class CollectionUtilsTests { assertThat(asMultiValueMap).containsKey("key"); } + @Test + void compositeMap() { + Map first = new HashMap<>(); + first.put("key1", "value1"); + first.put("key2", "value2"); + + Map second = new HashMap<>(); + second.put("key3", "value3"); + second.put("key4", "value4"); + + Map compositeMap = CollectionUtils.compositeMap(first, second); + + assertThat(compositeMap).containsKeys("key1", "key2", "key3", "key4"); + assertThat(compositeMap).containsValues("value1", "value2", "value3", "value4"); + } + private static final class Instance {