1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.atlassian.fugue;
17
18 import org.junit.Test;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.stream.Collector;
23
24 import static io.atlassian.fugue.Iterables.collect;
25 import static java.util.Arrays.asList;
26 import static java.util.Collections.emptyList;
27 import static java.util.Collections.singletonList;
28 import static java.util.stream.Collectors.*;
29 import static org.junit.Assert.*;
30
31 public class IterablesJavaCollectorTest {
32
33 @Test public void collectStringIterableAsIntegerList() {
34 final Iterable<String> from = asList("3", "2", "1", "4", "5");
35
36 final List<Integer> collectedAsIntList = collect(from, mapping(Integer::parseInt, toList()));
37
38 assertNotNull(collectedAsIntList);
39 assertEquals(5, collectedAsIntList.size());
40 assertEquals((Integer) 3, collectedAsIntList.get(0));
41 assertEquals((Integer) 2, collectedAsIntList.get(1));
42 assertEquals((Integer) 1, collectedAsIntList.get(2));
43 assertEquals((Integer) 4, collectedAsIntList.get(3));
44 assertEquals((Integer) 5, collectedAsIntList.get(4));
45 }
46
47 @Test public void collectStringIterableAsIntegerAverage() {
48 final Iterable<String> from = asList("3", "2", "1", "4", "5");
49
50 final Double average = collect(from, averagingInt(Integer::parseInt));
51
52 assertEquals((Double) 3.0, average);
53 }
54
55 @Test public void groupStringIterableIntoIntegerLists() {
56 final Iterable<String> from = asList("3", "2", "1", "4", "5");
57
58 final Map<Boolean, List<Integer>> grouped = collect(from, mapping(Integer::valueOf, groupingBy(integer -> integer <= 3)));
59
60 assertNotNull(grouped);
61 assertEquals(2, grouped.size());
62 assertTrue(grouped.containsKey(true));
63 assertNotNull(grouped.get(true));
64 assertEquals(3, grouped.get(true).size());
65 assertTrue(grouped.get(true).contains(1));
66 assertTrue(grouped.get(true).contains(2));
67 assertTrue(grouped.get(true).contains(3));
68 assertTrue(grouped.containsKey(false));
69 assertNotNull(grouped.get(false));
70 assertEquals(2, grouped.get(false).size());
71 assertTrue(grouped.get(false).contains(4));
72 assertTrue(grouped.get(false).contains(5));
73 }
74
75 @Test public void emptyStringIterableToIntegerListMustResultInEmptyList() {
76 final Iterable<String> from = emptyList();
77
78 final List<Integer> collectedAsIntList = collect(from, mapping(Integer::parseInt, toList()));
79
80 assertNotNull(collectedAsIntList);
81 assertEquals(0, collectedAsIntList.size());
82 }
83
84 @Test public void collectEmptyStringAsIntegerAverageMustResultInZero() {
85 final Iterable<String> from = emptyList();
86
87 final Double average = collect(from, averagingInt(Integer::parseInt));
88
89 assertEquals((double) 0, average, 0);
90 }
91
92 @Test public void groupEmptyStringIterableIntoIntegerListsMustResultInEmptyList() {
93 final Iterable<String> from = emptyList();
94
95 final Map<Boolean, List<Integer>> grouped = collect(from, mapping(Integer::valueOf, groupingBy(integer -> integer <= 3)));
96
97 assertNotNull(grouped);
98 assertEquals(0, grouped.size());
99 }
100
101 @Test public void stringIterableWithSizeOneToIntegerList() {
102 final Iterable<String> from = singletonList("123");
103
104 final List<Integer> collectedAsIntList = collect(from, mapping(Integer::parseInt, toList()));
105
106 assertNotNull(collectedAsIntList);
107 assertEquals(1, collectedAsIntList.size());
108 }
109
110 @Test public void stringIterableWithSizeOneToIntegerAverage() {
111 final Iterable<String> from = singletonList("123");
112
113 final Double average = collect(from, averagingInt(Integer::parseInt));
114
115 assertEquals((double) 123, average, 0);
116 }
117
118 @Test public void groupStringIterableWithSizeOneToIntegerLists() {
119 final Iterable<String> from = singletonList("123");
120
121 final Map<Boolean, List<Integer>> grouped = collect(from, mapping(Integer::valueOf, groupingBy(integer -> integer <= 3)));
122
123 assertNotNull(grouped);
124 assertEquals(1, grouped.size());
125 assertTrue(grouped.containsKey(false));
126 assertNotNull(grouped.get(false));
127 assertEquals(1, grouped.get(false).size());
128 assertTrue(grouped.get(false).contains(123));
129 }
130
131 @Test(expected = JavaCollectorRuntimeException.class) public void collectorThrowsRuntimeException() {
132 final Iterable<String> from = asList("3", "2", "1", "4", "5");
133
134 collect(from, mapping(s -> {
135 throw new JavaCollectorRuntimeException();
136 }, toList()));
137 }
138
139 @Test(expected = NullPointerException.class) @SuppressWarnings("unchecked") public void nullCollectorMustResultInNPE() {
140 final Iterable<String> from = asList("3", "2", "1", "4", "5");
141
142 collect(from, (Collector<String, Iterable<String>, Iterable<String>>) null);
143 }
144
145 @Test(expected = NullPointerException.class) public void nullIterableMustResultInNPE() {
146 collect(null, toList());
147 }
148
149 private static class JavaCollectorRuntimeException extends RuntimeException {}
150 }