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