1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.stream;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.util.Arrays;
23 import java.util.Objects;
24 import java.util.concurrent.atomic.AtomicLong;
25 import java.util.function.Function;
26 import java.util.stream.Collector;
27 import java.util.stream.Stream;
28
29 import org.apache.commons.lang3.AbstractLangTest;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class LangCollectorsTest extends AbstractLangTest {
36
37 private static final class Fixture {
38 int value;
39
40 private Fixture(final int value) {
41 this.value = value;
42 }
43
44 @Override
45 public String toString() {
46 return Integer.toString(value);
47 }
48 }
49
50 private static final Long _1L = Long.valueOf(1);
51 private static final Long _2L = Long.valueOf(2);
52 private static final Long _3L = Long.valueOf(3);
53
54 private static final Function<Object, String> TO_STRING = Objects::toString;
55
56 private static final Collector<Object, ?, String> JOINING_0 = LangCollectors.joining();
57 private static final Collector<Object, ?, String> JOINING_1 = LangCollectors.joining("-");
58 private static final Collector<Object, ?, String> JOINING_3 = LangCollectors.joining("-", "<", ">");
59 private static final Collector<Object, ?, String> JOINING_4 = LangCollectors.joining("-", "<", ">", TO_STRING);
60 private static final Collector<Object, ?, String> JOINING_4_NUL = LangCollectors.joining("-", "<", ">", o -> Objects.toString(o, "NUL"));
61
62 private String join0(final Object... objects) {
63 return LangCollectors.collect(JOINING_0, objects);
64 }
65
66 private String join1(final Object... objects) {
67 return LangCollectors.collect(JOINING_1, objects);
68 }
69
70 private String join3(final Object... objects) {
71 return LangCollectors.collect(JOINING_3, objects);
72 }
73
74 private String join4(final Object... objects) {
75 return LangCollectors.collect(JOINING_4, objects);
76 }
77
78 private String join4NullToString(final Object... objects) {
79 return LangCollectors.collect(JOINING_4_NUL, objects);
80 }
81
82 @Test
83 void testCollectStrings1Arg() {
84 assertEquals("", join1());
85 assertEquals("1", join1("1"));
86 assertEquals("1-2", join1("1", "2"));
87 assertEquals("1-2-3", join1("1", "2", "3"));
88 assertEquals("1-null-3", join1("1", null, "3"));
89 }
90
91 @Test
92 void testJoinCollectNonStrings0Arg() {
93 assertEquals("", join0());
94 assertEquals("1", join0(_1L));
95 assertEquals("12", join0(_1L, _2L));
96 assertEquals("123", join0(_1L, _2L, _3L));
97 assertEquals("1null3", join0(_1L, null, _3L));
98 assertEquals("12", join0(new AtomicLong(1), new AtomicLong(2)));
99 assertEquals("12", join0(new Fixture(1), new Fixture(2)));
100 }
101
102 @Test
103 void testJoinCollectNonStrings1Arg() {
104 assertEquals("", join1());
105 assertEquals("1", join1(_1L));
106 assertEquals("1-2", join1(_1L, _2L));
107 assertEquals("1-2-3", join1(_1L, _2L, _3L));
108 assertEquals("1-null-3", join1(_1L, null, _3L));
109 assertEquals("1-2", join1(new AtomicLong(1), new AtomicLong(2)));
110 assertEquals("1-2", join1(new Fixture(1), new Fixture(2)));
111 }
112
113 @Test
114 void testJoinCollectNonStrings3Args() {
115 assertEquals("<>", join3());
116 assertEquals("<1>", join3(_1L));
117 assertEquals("<1-2>", join3(_1L, _2L));
118 assertEquals("<1-2-3>", join3(_1L, _2L, _3L));
119 assertEquals("<1-null-3>", join3(_1L, null, _3L));
120 assertEquals("<1-2>", join3(new AtomicLong(1), new AtomicLong(2)));
121 assertEquals("<1-2>", join3(new Fixture(1), new Fixture(2)));
122 }
123
124 @Test
125 void testJoinCollectNonStrings4Args() {
126 assertEquals("<>", join4());
127 assertEquals("<1>", join4(_1L));
128 assertEquals("<1-2>", join4(_1L, _2L));
129 assertEquals("<1-2-3>", join4(_1L, _2L, _3L));
130 assertEquals("<1-null-3>", join4(_1L, null, _3L));
131 assertEquals("<1-NUL-3>", join4NullToString(_1L, null, _3L));
132 assertEquals("<1-2>", join4(new AtomicLong(1), new AtomicLong(2)));
133 assertEquals("<1-2>", join4(new Fixture(1), new Fixture(2)));
134 }
135
136 @Test
137 void testJoinCollectNullArgs() {
138 assertEquals("", join0((Object[]) null));
139 assertEquals("", join1((Object[]) null));
140 assertEquals("<>", join3((Object[]) null));
141 assertEquals("<>", join4NullToString((Object[]) null));
142 }
143
144 @Test
145 void testJoinCollectStrings0Arg() {
146 assertEquals("", join0());
147 assertEquals("1", join0("1"));
148 assertEquals("12", join0("1", "2"));
149 assertEquals("123", join0("1", "2", "3"));
150 assertEquals("1null3", join0("1", null, "3"));
151 }
152
153 @Test
154 void testJoinCollectStrings3Args() {
155 assertEquals("<>", join3());
156 assertEquals("<1>", join3("1"));
157 assertEquals("<1-2>", join3("1", "2"));
158 assertEquals("<1-2-3>", join3("1", "2", "3"));
159 assertEquals("<1-null-3>", join3("1", null, "3"));
160 }
161
162 @Test
163 void testJoinCollectStrings4Args() {
164 assertEquals("<>", join4());
165 assertEquals("<1>", join4("1"));
166 assertEquals("<1-2>", join4("1", "2"));
167 assertEquals("<1-2-3>", join4("1", "2", "3"));
168 assertEquals("<1-null-3>", join4("1", null, "3"));
169 assertEquals("<1-NUL-3>", join4NullToString("1", null, "3"));
170 }
171
172 @Test
173 void testJoiningNonStrings0Arg() {
174
175 assertEquals("", Stream.of().collect(JOINING_0));
176 assertEquals("1", Stream.of(_1L).collect(JOINING_0));
177 assertEquals("12", Stream.of(_1L, _2L).collect(JOINING_0));
178 assertEquals("123", Stream.of(_1L, _2L, _3L).collect(JOINING_0));
179 assertEquals("1null3", Stream.of(_1L, null, _3L).collect(JOINING_0));
180 assertEquals("12", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_0));
181 assertEquals("12", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_0));
182
183 assertEquals("", Arrays.stream(new Object[] {}).collect(JOINING_0));
184 assertEquals("1", Arrays.stream(new Long[] { _1L }).collect(JOINING_0));
185 assertEquals("12", Arrays.stream(new Long[] { _1L, _2L }).collect(JOINING_0));
186 assertEquals("123", Arrays.stream(new Long[] { _1L, _2L, _3L }).collect(JOINING_0));
187 assertEquals("1null3", Arrays.stream(new Long[] { _1L, null, _3L }).collect(JOINING_0));
188 assertEquals("12", Arrays.stream(new AtomicLong[] { new AtomicLong(1), new AtomicLong(2) }).collect(JOINING_0));
189 assertEquals("12", Arrays.stream(new Fixture[] { new Fixture(1), new Fixture(2) }).collect(JOINING_0));
190 }
191
192 @Test
193 void testJoiningNonStrings1Arg() {
194
195 assertEquals("", Stream.of().collect(JOINING_1));
196 assertEquals("1", Stream.of(_1L).collect(JOINING_1));
197 assertEquals("1-2", Stream.of(_1L, _2L).collect(JOINING_1));
198 assertEquals("1-2-3", Stream.of(_1L, _2L, _3L).collect(JOINING_1));
199 assertEquals("1-null-3", Stream.of(_1L, null, _3L).collect(JOINING_1));
200 assertEquals("1-2", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_1));
201 assertEquals("1-2", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_1));
202
203 assertEquals("", Arrays.stream(new Object[] {}).collect(JOINING_1));
204 assertEquals("1", Arrays.stream(new Long[] { _1L }).collect(JOINING_1));
205 assertEquals("1-2", Arrays.stream(new Long[] { _1L, _2L }).collect(JOINING_1));
206 assertEquals("1-2-3", Arrays.stream(new Long[] { _1L, _2L, _3L }).collect(JOINING_1));
207 assertEquals("1-null-3", Arrays.stream(new Long[] { _1L, null, _3L }).collect(JOINING_1));
208 assertEquals("1-2", Arrays.stream(new AtomicLong[] { new AtomicLong(1), new AtomicLong(2) }).collect(JOINING_1));
209 assertEquals("1-2", Arrays.stream(new Fixture[] { new Fixture(1), new Fixture(2) }).collect(JOINING_1));
210 }
211
212 @Test
213 void testJoiningNonStrings3Args() {
214 assertEquals("<>", Stream.of().collect(JOINING_3));
215 assertEquals("<1>", Stream.of(_1L).collect(JOINING_3));
216 assertEquals("<1-2>", Stream.of(_1L, _2L).collect(JOINING_3));
217 assertEquals("<1-2-3>", Stream.of(_1L, _2L, _3L).collect(JOINING_3));
218 assertEquals("<1-null-3>", Stream.of(_1L, null, _3L).collect(JOINING_3));
219 assertEquals("<1-2>", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_3));
220 assertEquals("<1-2>", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_3));
221 }
222
223 @Test
224 void testJoiningNonStrings4Args() {
225 assertEquals("<>", Stream.of().collect(JOINING_4));
226 assertEquals("<1>", Stream.of(_1L).collect(JOINING_4));
227 assertEquals("<1-2>", Stream.of(_1L, _2L).collect(JOINING_4));
228 assertEquals("<1-2-3>", Stream.of(_1L, _2L, _3L).collect(JOINING_4));
229 assertEquals("<1-null-3>", Stream.of(_1L, null, _3L).collect(JOINING_4));
230 assertEquals("<1-NUL-3>", Stream.of(_1L, null, _3L).collect(JOINING_4_NUL));
231 assertEquals("<1-2>", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_4));
232 assertEquals("<1-2>", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_4));
233 }
234
235 @Test
236 void testJoiningStrings0Arg() {
237 assertEquals("", Stream.of().collect(JOINING_0));
238 assertEquals("1", Stream.of("1").collect(JOINING_0));
239 assertEquals("12", Stream.of("1", "2").collect(JOINING_0));
240 assertEquals("123", Stream.of("1", "2", "3").collect(JOINING_0));
241 assertEquals("1null3", Stream.of("1", null, "3").collect(JOINING_0));
242 }
243
244 @Test
245 void testJoiningStrings1Arg() {
246 assertEquals("", Stream.of().collect(JOINING_1));
247 assertEquals("1", Stream.of("1").collect(JOINING_1));
248 assertEquals("1-2", Stream.of("1", "2").collect(JOINING_1));
249 assertEquals("1-2-3", Stream.of("1", "2", "3").collect(JOINING_1));
250 assertEquals("1-null-3", Stream.of("1", null, "3").collect(JOINING_1));
251 }
252
253 @Test
254 void testJoiningStrings3Args() {
255 assertEquals("<>", Stream.of().collect(JOINING_3));
256 assertEquals("<1>", Stream.of("1").collect(JOINING_3));
257 assertEquals("<1-2>", Stream.of("1", "2").collect(JOINING_3));
258 assertEquals("<1-2-3>", Stream.of("1", "2", "3").collect(JOINING_3));
259 assertEquals("<1-null-3>", Stream.of("1", null, "3").collect(JOINING_3));
260 }
261
262 @Test
263 void testJoiningStrings4Args() {
264 assertEquals("<>", Stream.of().collect(JOINING_4));
265 assertEquals("<1>", Stream.of("1").collect(JOINING_4));
266 assertEquals("<1-2>", Stream.of("1", "2").collect(JOINING_4));
267 assertEquals("<1-2-3>", Stream.of("1", "2", "3").collect(JOINING_4));
268 assertEquals("<1-null-3>", Stream.of("1", null, "3").collect(JOINING_4));
269 assertEquals("<1-NUL-3>", Stream.of("1", null, "3").collect(JOINING_4_NUL));
270 }
271 }