View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.util.ArrayDeque;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.Deque;
34  import java.util.HashSet;
35  import java.util.LinkedHashSet;
36  import java.util.LinkedList;
37  import java.util.List;
38  import java.util.Set;
39  
40  import org.apache.commons.collections4.bag.HashBag;
41  import org.apache.commons.collections4.functors.EqualPredicate;
42  import org.apache.commons.lang3.StringUtils;
43  import org.junit.jupiter.api.BeforeEach;
44  import org.junit.jupiter.api.Test;
45  
46  /**
47   * Tests for IterableUtils.
48   */
49  public class IterableUtilsTest {
50  
51      private static final Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
52  
53      private static final Predicate<Number> EVEN = input -> input.intValue() % 2 == 0;
54  
55      /**
56       * Iterable of {@link Integer}s
57       */
58      private Iterable<Integer> iterableA;
59  
60      /**
61       * Iterable of {@link Long}s
62       */
63      private Iterable<Long> iterableB;
64  
65      /**
66       * An empty Iterable.
67       */
68      private Iterable<Integer> emptyIterable;
69  
70      public void firstFromIterable() throws Exception {
71          // Collection, entry exists
72          final Bag<String> bag = new HashBag<>();
73          bag.add("element", 1);
74          assertEquals("element", IterableUtils.first(bag));
75      }
76  
77      public void getFromIterable() throws Exception {
78          // Collection, entry exists
79          final Bag<String> bag = new HashBag<>();
80          bag.add("element", 1);
81          assertEquals("element", IterableUtils.get(bag, 0));
82      }
83  
84      @BeforeEach
85      public void setUp() {
86          final Collection<Integer> collectionA = new ArrayList<>();
87          collectionA.add(1);
88          collectionA.add(2);
89          collectionA.add(2);
90          collectionA.add(3);
91          collectionA.add(3);
92          collectionA.add(3);
93          collectionA.add(4);
94          collectionA.add(4);
95          collectionA.add(4);
96          collectionA.add(4);
97          iterableA = collectionA;
98  
99          final Collection<Long> collectionB = new LinkedList<>();
100         collectionB.add(5L);
101         collectionB.add(4L);
102         collectionB.add(4L);
103         collectionB.add(3L);
104         collectionB.add(3L);
105         collectionB.add(3L);
106         collectionB.add(2L);
107         collectionB.add(2L);
108         collectionB.add(2L);
109         collectionB.add(2L);
110         iterableB = collectionB;
111 
112         emptyIterable = Collections.emptyList();
113     }
114 
115     @Test
116     public void testContainsWithEquator() {
117         final List<String> base = new ArrayList<>();
118         base.add("AC");
119         base.add("BB");
120         base.add("CA");
121 
122         final Equator<String> secondLetterEquator = new Equator<String>() {
123 
124             @Override
125             public boolean equate(final String o1, final String o2) {
126                 return o1.charAt(1) == o2.charAt(1);
127             }
128 
129             @Override
130             public int hash(final String o) {
131                 return o.charAt(1);
132             }
133 
134         };
135 
136         assertFalse(base.contains("CC"));
137         assertTrue(IterableUtils.contains(base, "AC", secondLetterEquator));
138         assertTrue(IterableUtils.contains(base, "CC", secondLetterEquator));
139         assertFalse(IterableUtils.contains(base, "CX", secondLetterEquator));
140         assertFalse(IterableUtils.contains(null, null, secondLetterEquator));
141 
142         assertThrows(NullPointerException.class, () -> IterableUtils.contains(base, "AC", null), "expecting NullPointerException");
143     }
144 
145     @Test
146     public void testCountMatches() {
147         assertEquals(4, IterableUtils.countMatches(iterableB, EQUALS_TWO));
148         assertEquals(0, IterableUtils.countMatches(null, EQUALS_TWO));
149         assertThrows(NullPointerException.class, () -> assertEquals(0, IterableUtils.countMatches(iterableA, null)), "predicate must not be null");
150         assertThrows(NullPointerException.class, () -> assertEquals(0, IterableUtils.countMatches(null, null)), "predicate must not be null");
151     }
152 
153     @Test
154     public void testDuplicateListAllSameInList() {
155         final List<Integer> input = Arrays.asList(5, 5, 5, 5);
156         assertEquals(Arrays.asList(5), IterableUtils.duplicateList(input));
157     }
158 
159     @Test
160     public void testDuplicateListEmptyDeque() {
161         assertTrue(IterableUtils.duplicateList(new ArrayDeque<>()).isEmpty());
162     }
163 
164     @Test
165     public void testDuplicateListEmptyList() {
166         final List<Integer> input = Arrays.asList();
167         assertTrue(IterableUtils.duplicateList(input).isEmpty());
168     }
169 
170     @Test
171     public void testDuplicateListEmptySet() {
172         assertTrue(IterableUtils.duplicateList(new HashSet<>()).isEmpty());
173     }
174 
175     @Test
176     public void testDuplicateListMultipleDuplicatesInDeque() {
177         final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4));
178         final List<Integer> expected = Arrays.asList(1, 2, 3, 4);
179         assertEquals(expected, IterableUtils.duplicateList(input));
180     }
181 
182     @Test
183     public void testDuplicateListMultipleDuplicatesInDequeReverse() {
184         // We want to make sure that the actual list is in the expected order
185         final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(4, 4, 3, 3, 2, 2, 1, 1));
186         final List<Integer> expected = Arrays.asList(4, 3, 2, 1);
187         assertEquals(expected, IterableUtils.duplicateList(input));
188     }
189 
190     @Test
191     public void testDuplicateListMultipleDuplicatesInList() {
192         final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
193         final List<Integer> expected = Arrays.asList(1, 2, 3, 4);
194         assertEquals(expected, IterableUtils.duplicateList(input));
195     }
196 
197     @Test
198     public void testDuplicateListMultipleDuplicatesInListReverse() {
199         // We want to make sure that the actual list is in the expected order
200         final List<Integer> input = Arrays.asList(4, 4, 3, 3, 2, 2, 1, 1);
201         final List<Integer> expected = Arrays.asList(4, 3, 2, 1);
202         assertEquals(expected, IterableUtils.duplicateList(input));
203     }
204 
205     @Test
206     public void testDuplicateListNoDuplicates() {
207         final List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
208         assertTrue(IterableUtils.duplicateList(input).isEmpty());
209     }
210 
211     @Test
212     public void testDuplicateListSingleElement() {
213         final List<Integer> input = Arrays.asList(1);
214         assertTrue(IterableUtils.duplicateList(input).isEmpty());
215     }
216 
217     @Test
218     public void testDuplicateListWithDuplicates() {
219         final List<Integer> input = Arrays.asList(1, 2, 3, 2, 4, 5, 3);
220         final List<Integer> expected = Arrays.asList(2, 3);
221         assertEquals(expected, IterableUtils.duplicateList(input));
222     }
223 
224     @Test
225     public void testDuplicateSequencedSetMultipleDuplicates() {
226         final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
227         final List<Integer> list = Arrays.asList(1, 2, 3, 4);
228         assertEquals(list, new ArrayList<>(IterableUtils.duplicateSequencedSet(input)));
229         assertEquals(new LinkedHashSet<>(list), IterableUtils.duplicateSequencedSet(input));
230     }
231 
232     @Test
233     public void testDuplicateSetEmptyDeque() {
234         assertTrue(IterableUtils.duplicateSet(new ArrayDeque<>()).isEmpty());
235     }
236 
237     @Test
238     public void testDuplicateSetEmptyList() {
239         final List<Integer> input = Arrays.asList();
240         assertTrue(IterableUtils.duplicateSet(input).isEmpty());
241     }
242 
243     @Test
244     public void testDuplicateSetEmptySet() {
245         assertTrue(IterableUtils.duplicateSet(new HashSet<>()).isEmpty());
246     }
247 
248     @Test
249     public void testDuplicateSetInSet() {
250         // Sets don't have duplicates, so the result is always an empty set.
251         final Set<Integer> input = new HashSet<>(Arrays.asList(5));
252         assertTrue(IterableUtils.duplicateSet(input).isEmpty());
253     }
254 
255     @Test
256     public void testDuplicateSetMultipleDuplicatesInDeque() {
257         final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4));
258         final Set<Integer> expected = new HashSet<>(Arrays.asList(1, 2, 3, 4));
259         assertEquals(expected, IterableUtils.duplicateSet(input));
260     }
261 
262     @Test
263     public void testDuplicateSetMultipleDuplicatesInList() {
264         final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
265         final Set<Integer> expected = new HashSet<>(Arrays.asList(1, 2, 3, 4));
266         assertEquals(expected, IterableUtils.duplicateSet(input));
267     }
268 
269     @Test
270     public void testDuplicateSetNoDuplicates() {
271         final List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
272         assertTrue(IterableUtils.duplicateSet(input).isEmpty());
273     }
274 
275     @Test
276     public void testDuplicateSetSingleElement() {
277         final List<Integer> input = Arrays.asList(1);
278         assertTrue(IterableUtils.duplicateSet(input).isEmpty());
279     }
280 
281     @Test
282     public void testDuplicateSetWithDuplicates() {
283         final List<Integer> input = Arrays.asList(1, 2, 3, 2, 4, 5, 3);
284         final Set<Integer> expected = new HashSet<>(Arrays.asList(2, 3));
285         assertEquals(expected, IterableUtils.duplicateSet(input));
286     }
287 
288     @Test
289     public void testDuplicatListAllSameInDeque() {
290         final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(5, 5, 5, 5));
291         assertEquals(Arrays.asList(5), IterableUtils.duplicateList(input));
292     }
293 
294     @Test
295     public void testDuplicatSetAllSameInDeque() {
296         final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(5, 5, 5, 5));
297         assertEquals(new HashSet<>(Arrays.asList(5)), IterableUtils.duplicateSet(input));
298     }
299 
300     @Test
301     public void testFind() {
302         Predicate<Number> testPredicate = EqualPredicate.equalPredicate(4);
303         Integer test = IterableUtils.find(iterableA, testPredicate);
304         assertEquals(4, (int) test);
305         testPredicate = EqualPredicate.equalPredicate(45);
306         test = IterableUtils.find(iterableA, testPredicate);
307         assertNull(test);
308         assertNull(IterableUtils.find(null, testPredicate));
309 
310         assertThrows(NullPointerException.class, () -> IterableUtils.find(iterableA, null), "expecting NullPointerException");
311     }
312 
313     @Test
314     public void testFirstFromIterableIndexOutOfBoundsException() throws Exception {
315         // Collection, entry exists
316         final Bag<String> bag = new HashBag<>();
317         // Collection, non-existent entry
318         assertThrows(IndexOutOfBoundsException.class, () -> IterableUtils.first(bag));
319     }
320 
321     @Test
322     public void testForEach() {
323         final List<Integer> listA = new ArrayList<>();
324         listA.add(1);
325 
326         final List<Integer> listB = new ArrayList<>();
327         listB.add(2);
328 
329         final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear");
330         final Collection<List<Integer>> col = new ArrayList<>();
331         col.add(listA);
332         col.add(listB);
333         IterableUtils.forEach(col, testClosure);
334         assertTrue(listA.isEmpty() && listB.isEmpty());
335 
336         assertThrows(NullPointerException.class, () -> IterableUtils.forEach(col, null), "expecting NullPointerException");
337 
338         IterableUtils.forEach(null, testClosure);
339 
340         // null should be OK
341         col.add(null);
342         IterableUtils.forEach(col, testClosure);
343     }
344 
345     @Test
346     public void testForEachButLast() {
347         final List<Integer> listA = new ArrayList<>();
348         listA.add(1);
349 
350         final List<Integer> listB = new ArrayList<>();
351         listB.add(2);
352 
353         final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear");
354         final Collection<List<Integer>> col = new ArrayList<>();
355         col.add(listA);
356         col.add(listB);
357         List<Integer> last = IterableUtils.forEachButLast(col, testClosure);
358         assertTrue(listA.isEmpty() && !listB.isEmpty());
359         assertSame(listB, last);
360 
361         assertThrows(NullPointerException.class, () -> IterableUtils.forEachButLast(col, null), "expecting NullPointerException");
362 
363         IterableUtils.forEachButLast(null, testClosure);
364 
365         // null should be OK
366         col.add(null);
367         col.add(null);
368         last = IterableUtils.forEachButLast(col, testClosure);
369         assertNull(last);
370     }
371 
372     @Test
373     public void testForEachFailure() {
374         final Closure<String> testClosure = ClosureUtils.invokerClosure("clear");
375         final Collection<String> col = new ArrayList<>();
376         col.add("x");
377         assertThrows(FunctorException.class, () -> IterableUtils.forEach(col, testClosure));
378     }
379 
380     @Test
381     public void testFrequency() {
382         // null iterable test
383         assertEquals(0, IterableUtils.frequency(null, 1));
384 
385         assertEquals(1, IterableUtils.frequency(iterableA, 1));
386         assertEquals(2, IterableUtils.frequency(iterableA, 2));
387         assertEquals(3, IterableUtils.frequency(iterableA, 3));
388         assertEquals(4, IterableUtils.frequency(iterableA, 4));
389         assertEquals(0, IterableUtils.frequency(iterableA, 5));
390 
391         assertEquals(0, IterableUtils.frequency(iterableB, 1L));
392         assertEquals(4, IterableUtils.frequency(iterableB, 2L));
393         assertEquals(3, IterableUtils.frequency(iterableB, 3L));
394         assertEquals(2, IterableUtils.frequency(iterableB, 4L));
395         assertEquals(1, IterableUtils.frequency(iterableB, 5L));
396 
397         // Ensure that generic bounds accept valid parameters, but return
398         // expected results
399         // for example no longs in the "int" Iterable<Number>, and vice versa.
400         final Iterable<Number> iterableIntAsNumber = Arrays.<Number>asList(1, 2, 3, 4, 5);
401         final Iterable<Number> iterableLongAsNumber = Arrays.<Number>asList(1L, 2L, 3L, 4L, 5L);
402         assertEquals(0, IterableUtils.frequency(iterableIntAsNumber, 2L));
403         assertEquals(0, IterableUtils.frequency(iterableLongAsNumber, 2));
404 
405         final Set<String> set = new HashSet<>();
406         set.add("A");
407         set.add("C");
408         set.add("E");
409         set.add("E");
410         assertEquals(1, IterableUtils.frequency(set, "A"));
411         assertEquals(0, IterableUtils.frequency(set, "B"));
412         assertEquals(1, IterableUtils.frequency(set, "C"));
413         assertEquals(0, IterableUtils.frequency(set, "D"));
414         assertEquals(1, IterableUtils.frequency(set, "E"));
415 
416         final Bag<String> bag = new HashBag<>();
417         bag.add("A", 3);
418         bag.add("C");
419         bag.add("E");
420         bag.add("E");
421         assertEquals(3, IterableUtils.frequency(bag, "A"));
422         assertEquals(0, IterableUtils.frequency(bag, "B"));
423         assertEquals(1, IterableUtils.frequency(bag, "C"));
424         assertEquals(0, IterableUtils.frequency(bag, "D"));
425         assertEquals(2, IterableUtils.frequency(bag, "E"));
426     }
427 
428     @Test
429     public void testFrequencyOfNull() {
430         final List<String> list = new ArrayList<>();
431         assertEquals(0, IterableUtils.frequency(list, null));
432         list.add("A");
433         assertEquals(0, IterableUtils.frequency(list, null));
434         list.add(null);
435         assertEquals(1, IterableUtils.frequency(list, null));
436         list.add("B");
437         assertEquals(1, IterableUtils.frequency(list, null));
438         list.add(null);
439         assertEquals(2, IterableUtils.frequency(list, null));
440         list.add("B");
441         assertEquals(2, IterableUtils.frequency(list, null));
442         list.add(null);
443         assertEquals(3, IterableUtils.frequency(list, null));
444     }
445 
446     @Test
447     public void testGetFromIterableIndexOutOfBoundsException() throws Exception {
448         // Collection, entry exists
449         final Bag<String> bag = new HashBag<>();
450         bag.add("element", 1);
451         // Collection, non-existent entry
452         assertThrows(IndexOutOfBoundsException.class, () -> IterableUtils.get(bag, 1));
453     }
454 
455     @Test
456     public void testIndexOf() {
457         Predicate<Number> testPredicate = EqualPredicate.equalPredicate((Number) 4);
458         int index = IterableUtils.indexOf(iterableA, testPredicate);
459         assertEquals(6, index);
460         testPredicate = EqualPredicate.equalPredicate((Number) 45);
461         index = IterableUtils.indexOf(iterableA, testPredicate);
462         assertEquals(-1, index);
463         assertEquals(-1, IterableUtils.indexOf(null, testPredicate));
464 
465         assertThrows(NullPointerException.class, () -> IterableUtils.indexOf(iterableA, null), "expecting NullPointerException");
466     }
467 
468     @Test
469     public void testMatchesAll() {
470         assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(null, null)), "predicate must not be null");
471 
472         assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(iterableA, null)), "predicate must not be null");
473 
474         final Predicate<Integer> lessThanFive = object -> object < 5;
475         assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive));
476 
477         final Predicate<Integer> lessThanFour = object -> object < 4;
478         assertFalse(IterableUtils.matchesAll(iterableA, lessThanFour));
479 
480         assertTrue(IterableUtils.matchesAll(null, lessThanFour));
481         assertTrue(IterableUtils.matchesAll(emptyIterable, lessThanFour));
482     }
483 
484     @Test
485     public void testMatchesAny() {
486         final List<Integer> list = new ArrayList<>();
487 
488         assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(null, null)), "predicate must not be null");
489 
490         assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(list, null)), "predicate must not be null");
491 
492         assertFalse(IterableUtils.matchesAny(null, EQUALS_TWO));
493         assertFalse(IterableUtils.matchesAny(list, EQUALS_TWO));
494         list.add(1);
495         list.add(3);
496         list.add(4);
497         assertFalse(IterableUtils.matchesAny(list, EQUALS_TWO));
498 
499         list.add(2);
500         assertTrue(IterableUtils.matchesAny(list, EQUALS_TWO));
501     }
502 
503     @SuppressWarnings("unchecked")
504     @Test
505     public void testPartition() {
506         final List<Integer> input = new ArrayList<>();
507         input.add(1);
508         input.add(2);
509         input.add(3);
510         input.add(4);
511         List<List<Integer>> partitions = IterableUtils.partition(input, EQUALS_TWO);
512         assertEquals(2, partitions.size());
513 
514         // first partition contains 2
515         Collection<Integer> partition = partitions.get(0);
516         assertEquals(1, partition.size());
517         assertEquals(2, CollectionUtils.extractSingleton(partition).intValue());
518 
519         // second partition contains 1, 3, and 4
520         final Integer[] expected = { 1, 3, 4 };
521         partition = partitions.get(1);
522         assertArrayEquals(expected, partition.toArray());
523 
524         partitions = IterableUtils.partition((List<Integer>) null, EQUALS_TWO);
525         assertEquals(2, partitions.size());
526         assertTrue(partitions.get(0).isEmpty());
527         assertTrue(partitions.get(1).isEmpty());
528 
529         partitions = IterableUtils.partition(input);
530         assertEquals(1, partitions.size());
531         assertEquals(input, partitions.get(0));
532 
533         assertThrows(NullPointerException.class, () -> IterableUtils.partition(input, (Predicate<Integer>) null), "expecting NullPointerException");
534     }
535 
536     @SuppressWarnings("unchecked")
537     @Test
538     public void testPartitionMultiplePredicates() {
539         final List<Integer> input = new ArrayList<>();
540         input.add(1);
541         input.add(2);
542         input.add(3);
543         input.add(4);
544         final List<List<Integer>> partitions = IterableUtils.partition(input, EQUALS_TWO, EVEN);
545 
546         // first partition contains 2
547         Collection<Integer> partition = partitions.get(0);
548         assertEquals(1, partition.size());
549         assertEquals(2, partition.iterator().next().intValue());
550 
551         // second partition contains 4
552         partition = partitions.get(1);
553         assertEquals(1, partition.size());
554         assertEquals(4, partition.iterator().next().intValue());
555 
556         // third partition contains 1 and 3
557         final Integer[] expected = { 1, 3 };
558         partition = partitions.get(2);
559         assertArrayEquals(expected, partition.toArray());
560 
561         assertThrows(NullPointerException.class, () -> IterableUtils.partition(input, EQUALS_TWO, null));
562     }
563 
564     @Test
565     public void testSize() {
566         assertEquals(0, IterableUtils.size(null));
567     }
568 
569     @Test
570     public void testToString() {
571         String result = IterableUtils.toString(iterableA);
572         assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]", result);
573 
574         result = IterableUtils.toString(new ArrayList<>());
575         assertEquals("[]", result);
576 
577         result = IterableUtils.toString(null);
578         assertEquals("[]", result);
579 
580         result = IterableUtils.toString(iterableA, input -> Integer.toString(input * 2));
581         assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);
582 
583         result = IterableUtils.toString(new ArrayList<>(), input -> {
584             fail("not supposed to reach here");
585             return StringUtils.EMPTY;
586         });
587         assertEquals("[]", result);
588 
589         result = IterableUtils.toString(null, input -> {
590             fail("not supposed to reach here");
591             return StringUtils.EMPTY;
592         });
593         assertEquals("[]", result);
594     }
595 
596     @Test
597     public void testToStringDelimiter() {
598 
599         final Transformer<Integer, String> transformer = input -> Integer.toString(input * 2);
600 
601         String result = IterableUtils.toString(iterableA, transformer, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY);
602         assertEquals("2446668888", result);
603 
604         result = IterableUtils.toString(iterableA, transformer, ",", StringUtils.EMPTY, StringUtils.EMPTY);
605         assertEquals("2,4,4,6,6,6,8,8,8,8", result);
606 
607         result = IterableUtils.toString(iterableA, transformer, StringUtils.EMPTY, "[", "]");
608         assertEquals("[2446668888]", result);
609 
610         result = IterableUtils.toString(iterableA, transformer, ",", "[", "]");
611         assertEquals("[2,4,4,6,6,6,8,8,8,8]", result);
612 
613         result = IterableUtils.toString(iterableA, transformer, ",", "[[", "]]");
614         assertEquals("[[2,4,4,6,6,6,8,8,8,8]]", result);
615 
616         result = IterableUtils.toString(iterableA, transformer, ",,", "[", "]");
617         assertEquals("[2,,4,,4,,6,,6,,6,,8,,8,,8,,8]", result);
618 
619         result = IterableUtils.toString(iterableA, transformer, ",,", "((", "))");
620         assertEquals("((2,,4,,4,,6,,6,,6,,8,,8,,8,,8))", result);
621 
622         result = IterableUtils.toString(new ArrayList<>(), transformer, StringUtils.EMPTY, "(", ")");
623         assertEquals("()", result);
624 
625         result = IterableUtils.toString(new ArrayList<>(), transformer, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY);
626         assertEquals(StringUtils.EMPTY, result);
627     }
628 
629     @Test
630     public void testToStringWithNullArguments() {
631         final String result = IterableUtils.toString(null, input -> {
632             fail("not supposed to reach here");
633             return StringUtils.EMPTY;
634         }, StringUtils.EMPTY, "(", ")");
635         assertEquals("()", result);
636         assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), null, StringUtils.EMPTY, "(", ")"));
637         assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> {
638             fail("not supposed to reach here");
639             return StringUtils.EMPTY;
640         }, null, "(", ")"));
641         assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> {
642             fail("not supposed to reach here");
643             return StringUtils.EMPTY;
644         }, StringUtils.EMPTY, null, ")"));
645         assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> {
646             fail("not supposed to reach here");
647             return StringUtils.EMPTY;
648         }, StringUtils.EMPTY, "(", null));
649     }
650 
651 }