Skip to content

Commit 9937df4

Browse files
committed
test: test for CollectionUtil
1 parent 0ff8feb commit 9937df4

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.*;
23+
import java.util.function.Supplier;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
class CollectionUtilTest {
28+
29+
@Test
30+
void chunk_NullOriginalCollection_ThrowsException() {
31+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
32+
() -> CollectionUtil.chunk(null, 3, ArrayList::new));
33+
assertEquals("Collection must not be null.", ex.getMessage());
34+
}
35+
36+
@Test
37+
void chunk_NegativeMaxSize_ThrowsException() {
38+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
39+
() -> CollectionUtil.chunk(List.of(1, 2), -1, ArrayList::new));
40+
assertEquals("maxSize must greater than 0.", ex.getMessage());
41+
}
42+
43+
@Test
44+
void chunk_NullCollectionFactory_ThrowsException() {
45+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
46+
() -> CollectionUtil.chunk(List.of(1, 2), 2, null));
47+
assertEquals("Factory method cannot be null.", ex.getMessage());
48+
}
49+
50+
@Test
51+
void chunk_EmptyCollection_ReturnsOneEmptySubCollection() {
52+
List<List<Integer>> chunks = CollectionUtil.chunk(Collections.emptyList(), 3, ArrayList::new);
53+
assertEquals(1, chunks.size());
54+
assertTrue(chunks.get(0).isEmpty());
55+
}
56+
57+
@Test
58+
void chunk_CollectionSizeLessThanMaxSize_ReturnsOneSubCollectionWithAllElements() {
59+
List<Integer> list = List.of(1, 2);
60+
List<List<Integer>> chunks = CollectionUtil.chunk(list, 5, ArrayList::new);
61+
assertEquals(1, chunks.size());
62+
assertEquals(list, chunks.get(0));
63+
}
64+
65+
@Test
66+
void chunk_CollectionSizeEqualMaxSize_ReturnsOneSubCollectionWithAllElements() {
67+
List<Integer> list = List.of(1, 2, 3);
68+
List<List<Integer>> chunks = CollectionUtil.chunk(list, 3, ArrayList::new);
69+
assertEquals(1, chunks.size());
70+
assertEquals(list, chunks.get(0));
71+
}
72+
73+
@Test
74+
void chunk_CollectionSizeGreaterThanMaxSize_ReturnsMultipleSubCollections() {
75+
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
76+
int maxSize = 3;
77+
List<List<Integer>> chunks = CollectionUtil.chunk(list, maxSize, ArrayList::new);
78+
79+
// Expect 3 subcollections: [1,2,3], [4,5,6], [7]
80+
assertEquals(3, chunks.size());
81+
assertEquals(List.of(1, 2, 3), chunks.get(0));
82+
assertEquals(List.of(4, 5, 6), chunks.get(1));
83+
assertEquals(List.of(7), chunks.get(2));
84+
}
85+
86+
@Test
87+
void chunk_UsesDifferentCollectionTypeAsSubCollections() {
88+
LinkedList<Integer> list = new LinkedList<>(List.of(1, 2, 3, 4));
89+
Supplier<LinkedList<Integer>> factory = LinkedList::new;
90+
List<LinkedList<Integer>> chunks = CollectionUtil.chunk(list, 2, factory);
91+
assertEquals(2, chunks.size());
92+
assertInstanceOf(LinkedList.class, chunks.get(0));
93+
assertInstanceOf(LinkedList.class, chunks.get(1));
94+
assertEquals(List.of(1, 2), chunks.get(0));
95+
assertEquals(List.of(3, 4), chunks.get(1));
96+
}
97+
98+
@Test
99+
void chunk_CollectionWithOneElementAndMaxSizeOne_ReturnsOneSubCollection() {
100+
List<String> list = List.of("a");
101+
List<List<String>> chunks = CollectionUtil.chunk(list, 1, ArrayList::new);
102+
assertEquals(1, chunks.size());
103+
assertEquals(list, chunks.get(0));
104+
}
105+
106+
@Test
107+
void chunk_MaxSizeZero_ThrowsException() {
108+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
109+
() -> CollectionUtil.chunk(List.of(1), 0, ArrayList::new));
110+
assertEquals("maxSize must greater than 0.", ex.getMessage());
111+
}
112+
}

0 commit comments

Comments
 (0)