• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Guava Authors
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 
17 package com.google.common.collect;
18 
19 import static com.google.common.collect.Tables.immutableCell;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.base.Equivalence;
23 import com.google.common.base.Function;
24 import com.google.common.base.MoreObjects;
25 import com.google.common.collect.Table.Cell;
26 import com.google.common.testing.CollectorTester;
27 import java.util.function.BiPredicate;
28 import java.util.stream.Collector;
29 import java.util.stream.Stream;
30 import junit.framework.TestCase;
31 
32 /** Unit tests for {@link TableCollectors}. */
33 @GwtCompatible(emulated = true)
34 public class TableCollectorsTest {
35 
36   public static final class ToImmutableTableTest extends TestCase {
testToImmutableTable()37     public void testToImmutableTable() {
38       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
39           collector =
40               TableCollectors.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue);
41       BiPredicate<ImmutableTable<String, String, Integer>, ImmutableTable<String, String, Integer>>
42           equivalence = pairwiseOnResultOf(ImmutableTable::cellSet);
43       CollectorTester.of(collector, equivalence)
44           .expectCollects(
45               new ImmutableTable.Builder<String, String, Integer>()
46                   .put("one", "uno", 1)
47                   .put("two", "dos", 2)
48                   .put("three", "tres", 3)
49                   .build(),
50               immutableCell("one", "uno", 1),
51               immutableCell("two", "dos", 2),
52               immutableCell("three", "tres", 3));
53     }
54 
testToImmutableTableConflict()55     public void testToImmutableTableConflict() {
56       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
57           collector =
58               TableCollectors.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue);
59       try {
60         Stream.of(immutableCell("one", "uno", 1), immutableCell("one", "uno", 2))
61             .collect(collector);
62         fail("Expected IllegalArgumentException");
63       } catch (IllegalArgumentException expected) {
64       }
65     }
66 
testToImmutableTableNullRowKey()67     public void testToImmutableTableNullRowKey() {
68       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
69           collector =
70               TableCollectors.toImmutableTable(t -> null, Cell::getColumnKey, Cell::getValue);
71       try {
72         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
73         fail("Expected NullPointerException");
74       } catch (NullPointerException expected) {
75       }
76     }
77 
testToImmutableTableNullColumnKey()78     public void testToImmutableTableNullColumnKey() {
79       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
80           collector = TableCollectors.toImmutableTable(Cell::getRowKey, t -> null, Cell::getValue);
81       try {
82         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
83         fail("Expected NullPointerException");
84       } catch (NullPointerException expected) {
85       }
86     }
87 
testToImmutableTableNullValue()88     public void testToImmutableTableNullValue() {
89       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
90           collector =
91               TableCollectors.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, t -> null);
92       try {
93         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
94         fail("Expected NullPointerException");
95       } catch (NullPointerException expected) {
96       }
97       collector =
98           TableCollectors.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue);
99       try {
100         Stream.of(immutableCell("one", "uno", 1), immutableCell("one", "uno", (Integer) null))
101             .collect(collector);
102         fail("Expected NullPointerException");
103       } catch (NullPointerException expected) {
104       }
105     }
106 
testToImmutableTableMerging()107     public void testToImmutableTableMerging() {
108       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
109           collector =
110               TableCollectors.toImmutableTable(
111                   Cell::getRowKey, Cell::getColumnKey, Cell::getValue, Integer::sum);
112       BiPredicate<ImmutableTable<String, String, Integer>, ImmutableTable<String, String, Integer>>
113           equivalence = pairwiseOnResultOf(ImmutableTable::cellSet);
114       CollectorTester.of(collector, equivalence)
115           .expectCollects(
116               new ImmutableTable.Builder<String, String, Integer>()
117                   .put("one", "uno", 1)
118                   .put("two", "dos", 6)
119                   .put("three", "tres", 3)
120                   .build(),
121               immutableCell("one", "uno", 1),
122               immutableCell("two", "dos", 2),
123               immutableCell("three", "tres", 3),
124               immutableCell("two", "dos", 4));
125     }
126 
testToImmutableTableMergingNullRowKey()127     public void testToImmutableTableMergingNullRowKey() {
128       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
129           collector =
130               TableCollectors.toImmutableTable(
131                   t -> null, Cell::getColumnKey, Cell::getValue, Integer::sum);
132       try {
133         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
134         fail("Expected NullPointerException");
135       } catch (NullPointerException expected) {
136       }
137     }
138 
testToImmutableTableMergingNullColumnKey()139     public void testToImmutableTableMergingNullColumnKey() {
140       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
141           collector =
142               TableCollectors.toImmutableTable(
143                   Cell::getRowKey, t -> null, Cell::getValue, Integer::sum);
144       try {
145         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
146         fail("Expected NullPointerException");
147       } catch (NullPointerException expected) {
148       }
149     }
150 
testToImmutableTableMergingNullValue()151     public void testToImmutableTableMergingNullValue() {
152       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
153           collector =
154               TableCollectors.toImmutableTable(
155                   Cell::getRowKey, Cell::getColumnKey, t -> null, Integer::sum);
156       try {
157         Stream.of(immutableCell("one", "uno", 1)).collect(collector);
158         fail("Expected NullPointerException");
159       } catch (NullPointerException expected) {
160       }
161       collector =
162           TableCollectors.toImmutableTable(
163               Cell::getRowKey,
164               Cell::getColumnKey,
165               Cell::getValue,
166               (i, j) -> MoreObjects.firstNonNull(i, 0) + MoreObjects.firstNonNull(j, 0));
167       try {
168         Stream.of(immutableCell("one", "uno", 1), immutableCell("one", "uno", (Integer) null))
169             .collect(collector);
170         fail("Expected NullPointerException");
171       } catch (NullPointerException expected) {
172       }
173     }
174 
testToImmutableTableMergingNullMerge()175     public void testToImmutableTableMergingNullMerge() {
176       Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>>
177           collector =
178               TableCollectors.toImmutableTable(
179                   Cell::getRowKey, Cell::getColumnKey, Cell::getValue, (v1, v2) -> null);
180       try {
181         Stream.of(immutableCell("one", "uno", 1), immutableCell("one", "uno", 2))
182             .collect(collector);
183         fail("Expected NullPointerException");
184       } catch (NullPointerException expected) {
185       }
186     }
187   }
188 
189   public static final class ToTableTest extends TestCase {
190 
testToTable()191     public void testToTable() {
192       Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
193           TableCollectors.toTable(
194               Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create);
195       BiPredicate<Table<String, String, Integer>, Table<String, String, Integer>> equivalence =
196           pairwiseOnResultOf(Table::cellSet);
197       CollectorTester.of(collector, equivalence)
198           .expectCollects(
199               new ImmutableTable.Builder<String, String, Integer>()
200                   .put("one", "uno", 1)
201                   .put("two", "dos", 2)
202                   .put("three", "tres", 3)
203                   .build(),
204               immutableCell("one", "uno", 1),
205               immutableCell("two", "dos", 2),
206               immutableCell("three", "tres", 3));
207     }
208 
testToTableNullMerge()209     public void testToTableNullMerge() {
210       Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
211           TableCollectors.toTable(
212               Cell::getRowKey,
213               Cell::getColumnKey,
214               Cell::getValue,
215               (Integer v1, Integer v2) -> null,
216               HashBasedTable::create);
217       BiPredicate<Table<String, String, Integer>, Table<String, String, Integer>> equivalence =
218           pairwiseOnResultOf(Table::cellSet);
219       CollectorTester.of(collector, equivalence)
220           .expectCollects(
221               ImmutableTable.of(), immutableCell("one", "uno", 1), immutableCell("one", "uno", 2));
222     }
223 
testToTableNullValues()224     public void testToTableNullValues() {
225       Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
226           TableCollectors.toTable(
227               Cell::getRowKey,
228               Cell::getColumnKey,
229               Cell::getValue,
230               () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno")));
231       try {
232         Stream.of(immutableCell("one", "uno", (Integer) null)).collect(collector);
233         fail("Expected NullPointerException");
234       } catch (NullPointerException expected) {
235       }
236     }
237 
testToTableConflict()238     public void testToTableConflict() {
239       Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
240           TableCollectors.toTable(
241               Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create);
242       try {
243         Stream.of(immutableCell("one", "uno", 1), immutableCell("one", "uno", 2))
244             .collect(collector);
245         fail("Expected IllegalStateException");
246       } catch (IllegalStateException expected) {
247       }
248     }
249 
testToTableMerging()250     public void testToTableMerging() {
251       Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
252           TableCollectors.toTable(
253               Cell::getRowKey,
254               Cell::getColumnKey,
255               Cell::getValue,
256               Integer::sum,
257               HashBasedTable::create);
258       BiPredicate<Table<String, String, Integer>, Table<String, String, Integer>> equivalence =
259           pairwiseOnResultOf(Table::cellSet);
260       CollectorTester.of(collector, equivalence)
261           .expectCollects(
262               new ImmutableTable.Builder<String, String, Integer>()
263                   .put("one", "uno", 1)
264                   .put("two", "dos", 6)
265                   .put("three", "tres", 3)
266                   .build(),
267               immutableCell("one", "uno", 1),
268               immutableCell("two", "dos", 2),
269               immutableCell("three", "tres", 3),
270               immutableCell("two", "dos", 4));
271     }
272   }
273 
274   // This function specifically returns a BiPredicate, because Guava7’s Equivalence class does not
275   // actually implement BiPredicate, and CollectorTests expects a BiPredicate.
pairwiseOnResultOf(Function<C, R> arg)276   static <C, E, R extends Iterable<E>> BiPredicate<C, C> pairwiseOnResultOf(Function<C, R> arg) {
277     Equivalence<C> equivalence = Equivalence.equals().<E>pairwise().onResultOf(arg);
278     return equivalence::equivalent;
279   }
280 
TableCollectorsTest()281   private TableCollectorsTest() {}
282 }
283