Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for CollectionUtils #33694

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
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;

Expand Down Expand Up @@ -176,6 +179,32 @@ void findFirstMatch() {
assertThat(CollectionUtils.findFirstMatch(source, candidates)).isEqualTo("def");
}

@Test
void findValueOfType() {
List<Integer> integerList = new ArrayList<>();
integerList.add(1);
assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isEqualTo(1);

Set<Integer> integerSet = new HashSet<>();
integerSet.add(2);
assertThat(CollectionUtils.findValueOfType(integerSet, Integer.class)).isEqualTo(2);
}

@Test
void findValueOfTypeWithNullOrEmptyCollection() {
List<Integer> emptyList = new ArrayList<>();
assertThat(CollectionUtils.findValueOfType(null, Integer.class)).isNull();
assertThat(CollectionUtils.findValueOfType(emptyList, Integer.class)).isNull();
}

@Test
void findValueOfTypeWithMoreThanOneValue() {
List<Integer> integerList = new ArrayList<>();
integerList.add(1);
integerList.add(2);
assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isNull();
}

@Test
void hasUniqueObject() {
List<String> list = new ArrayList<>();
Expand Down Expand Up @@ -210,6 +239,97 @@ void hasUniqueObject() {
assertThat(CollectionUtils.hasUniqueObject(list)).isFalse();
}

@Test
void findCommonElementType() {
List<Integer> integerList = new ArrayList<>();
integerList.add(1);
integerList.add(2);

assertThat(CollectionUtils.findCommonElementType(integerList)).isEqualTo(Integer.class);
}

@Test
void findCommonElementTypeWithNullOrEmptyCollection() {
List<Integer> emptyList = new ArrayList<>();
assertThat(CollectionUtils.findCommonElementType(null)).isNull();
assertThat(CollectionUtils.findCommonElementType(emptyList)).isNull();
}

@Test
void findCommonElementTypeWithDifferentElementType() {
List<Object> list = new ArrayList<>();
list.add(1);
list.add("foo");
assertThat(CollectionUtils.findCommonElementType(list)).isNull();
}

@Test
void firstElementWithSet() {
Set<Integer> 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<Integer> 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<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
assertThat(CollectionUtils.firstElement(list)).isEqualTo(1);
}

@Test
void lastElementWithSet() {
Set<Integer> 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<Integer> 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<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
assertThat(CollectionUtils.lastElement(list)).isEqualTo(3);
}

@Test
void toArray() {
Vector<String> vector = new Vector<>();
vector.add("foo");
vector.add("bar");
Enumeration<String> enumeration = vector.elements();
assertThat(CollectionUtils.toArray(enumeration, new String[]{})).contains("foo", "bar");
}

@Test
void conversionOfEmptyMap() {
MultiValueMap<String, String> asMultiValueMap = CollectionUtils.toMultiValueMap(new HashMap<>());
Expand All @@ -234,6 +354,22 @@ void changesValueByReference() {
assertThat(asMultiValueMap).containsKey("key");
}

@Test
void compositeMap() {
Map<String, String> first = new HashMap<>();
first.put("key1", "value1");
first.put("key2", "value2");

Map<String, String> second = new HashMap<>();
second.put("key3", "value3");
second.put("key4", "value4");

Map<String, String> compositeMap = CollectionUtils.compositeMap(first, second);

assertThat(compositeMap).containsKeys("key1", "key2", "key3", "key4");
assertThat(compositeMap).containsValues("value1", "value2", "value3", "value4");
}


private static final class Instance {

Expand Down