Skip to content

Commit 5bcce17

Browse files
jukekxmsdeleuze
authored andcommitted
Add tests for CollectionUtils
- findValueOfType - findCommonElementType - firstElement - lastElement - toArray - compositeMap Closes gh-33694
1 parent 9be3d85 commit 5bcce17

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Map;
2828
import java.util.Properties;
2929
import java.util.Set;
30+
import java.util.SortedSet;
31+
import java.util.TreeSet;
32+
import java.util.Vector;
3033

3134
import org.junit.jupiter.api.Test;
3235

@@ -176,6 +179,31 @@ void findFirstMatch() {
176179
assertThat(CollectionUtils.findFirstMatch(source, candidates)).isEqualTo("def");
177180
}
178181

182+
@Test
183+
void findValueOfType() {
184+
List<Integer> integerList = new ArrayList<>();
185+
integerList.add(1);
186+
assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isEqualTo(1);
187+
188+
Set<Integer> integerSet = new HashSet<>();
189+
integerSet.add(2);
190+
assertThat(CollectionUtils.findValueOfType(integerSet, Integer.class)).isEqualTo(2);
191+
}
192+
193+
@Test
194+
void findValueOfTypeWithEmptyCollection() {
195+
List<Integer> emptyList = new ArrayList<>();
196+
assertThat(CollectionUtils.findValueOfType(emptyList, Integer.class)).isNull();
197+
}
198+
199+
@Test
200+
void findValueOfTypeWithMoreThanOneValue() {
201+
List<Integer> integerList = new ArrayList<>();
202+
integerList.add(1);
203+
integerList.add(2);
204+
assertThat(CollectionUtils.findValueOfType(integerList, Integer.class)).isNull();
205+
}
206+
179207
@Test
180208
void hasUniqueObject() {
181209
List<String> list = new ArrayList<>();
@@ -210,6 +238,96 @@ void hasUniqueObject() {
210238
assertThat(CollectionUtils.hasUniqueObject(list)).isFalse();
211239
}
212240

241+
@Test
242+
void findCommonElementType() {
243+
List<Integer> integerList = new ArrayList<>();
244+
integerList.add(1);
245+
integerList.add(2);
246+
247+
assertThat(CollectionUtils.findCommonElementType(integerList)).isEqualTo(Integer.class);
248+
}
249+
250+
@Test
251+
void findCommonElementTypeWithEmptyCollection() {
252+
List<Integer> emptyList = new ArrayList<>();
253+
assertThat(CollectionUtils.findCommonElementType(emptyList)).isNull();
254+
}
255+
256+
@Test
257+
void findCommonElementTypeWithDifferentElementType() {
258+
List<Object> list = new ArrayList<>();
259+
list.add(1);
260+
list.add("foo");
261+
assertThat(CollectionUtils.findCommonElementType(list)).isNull();
262+
}
263+
264+
@Test
265+
void firstElementWithSet() {
266+
Set<Integer> set = new HashSet<>();
267+
set.add(17);
268+
set.add(3);
269+
set.add(2);
270+
set.add(1);
271+
assertThat(CollectionUtils.firstElement(set)).isEqualTo(17);
272+
}
273+
274+
@Test
275+
void firstElementWithSortedSet() {
276+
SortedSet<Integer> sortedSet = new TreeSet<>();
277+
sortedSet.add(17);
278+
sortedSet.add(3);
279+
sortedSet.add(2);
280+
sortedSet.add(1);
281+
assertThat(CollectionUtils.firstElement(sortedSet)).isEqualTo(1);
282+
}
283+
284+
@Test
285+
void firstElementWithList() {
286+
List<Integer> list = new ArrayList<>();
287+
list.add(1);
288+
list.add(2);
289+
list.add(3);
290+
assertThat(CollectionUtils.firstElement(list)).isEqualTo(1);
291+
}
292+
293+
@Test
294+
void lastElementWithSet() {
295+
Set<Integer> set = new HashSet<>();
296+
set.add(17);
297+
set.add(3);
298+
set.add(2);
299+
set.add(1);
300+
assertThat(CollectionUtils.lastElement(set)).isEqualTo(3);
301+
}
302+
303+
@Test
304+
void lastElementWithSortedSet() {
305+
SortedSet<Integer> sortedSet = new TreeSet<>();
306+
sortedSet.add(17);
307+
sortedSet.add(3);
308+
sortedSet.add(2);
309+
sortedSet.add(1);
310+
assertThat(CollectionUtils.lastElement(sortedSet)).isEqualTo(17);
311+
}
312+
313+
@Test
314+
void lastElementWithList() {
315+
List<Integer> list = new ArrayList<>();
316+
list.add(1);
317+
list.add(2);
318+
list.add(3);
319+
assertThat(CollectionUtils.lastElement(list)).isEqualTo(3);
320+
}
321+
322+
@Test
323+
void toArray() {
324+
Vector<String> vector = new Vector<>();
325+
vector.add("foo");
326+
vector.add("bar");
327+
Enumeration<String> enumeration = vector.elements();
328+
assertThat(CollectionUtils.toArray(enumeration, new String[]{})).containsExactly("foo", "bar");
329+
}
330+
213331
@Test
214332
void conversionOfEmptyMap() {
215333
MultiValueMap<String, String> asMultiValueMap = CollectionUtils.toMultiValueMap(new HashMap<>());
@@ -234,6 +352,22 @@ void changesValueByReference() {
234352
assertThat(asMultiValueMap).containsKey("key");
235353
}
236354

355+
@Test
356+
void compositeMap() {
357+
Map<String, String> first = new HashMap<>();
358+
first.put("key1", "value1");
359+
first.put("key2", "value2");
360+
361+
Map<String, String> second = new HashMap<>();
362+
second.put("key3", "value3");
363+
second.put("key4", "value4");
364+
365+
Map<String, String> compositeMap = CollectionUtils.compositeMap(first, second);
366+
367+
assertThat(compositeMap).containsKeys("key1", "key2", "key3", "key4");
368+
assertThat(compositeMap).containsValues("value1", "value2", "value3", "value4");
369+
}
370+
237371

238372
private static final class Instance {
239373

0 commit comments

Comments
 (0)