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