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