• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.Table.Cell;
22 import com.google.common.testing.CollectorTester;
23 import com.google.common.testing.EqualsTester;
24 import com.google.common.testing.SerializableTester;
25 import java.util.stream.Collector;
26 import junit.framework.TestCase;
27 
28 /**
29  * Tests for {@link Tables}.
30  *
31  * @author Jared Levy
32  */
33 @GwtCompatible(emulated = true)
34 public class TablesTest extends TestCase {
35 
36   // The bulk of the toTable tests can be found in TableCollectorsTest.
37   // This gives minimal coverage to the forwarding functions
testToTableSanityTest()38   public void testToTableSanityTest() {
39     Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
40         Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create);
41     HashBasedTable<String, String, Integer> expected = HashBasedTable.create();
42     expected.put("one", "uno", 1);
43     CollectorTester.of(collector)
44         .expectCollects(HashBasedTable.create())
45         .expectCollects(expected, Tables.immutableCell("one", "uno", 1));
46   }
47 
testToTableMergingSanityTest()48   public void testToTableMergingSanityTest() {
49     Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
50         Tables.toTable(
51             Cell::getRowKey,
52             Cell::getColumnKey,
53             Cell::getValue,
54             Integer::sum,
55             HashBasedTable::create);
56     HashBasedTable<String, String, Integer> expected = HashBasedTable.create();
57     expected.put("one", "uno", 3);
58     CollectorTester.of(collector)
59         .expectCollects(HashBasedTable.create())
60         .expectCollects(
61             expected, Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2));
62   }
63 
64   @GwtIncompatible // SerializableTester
testImmutableEntrySerialization()65   public void testImmutableEntrySerialization() {
66     Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a');
67     SerializableTester.reserializeAndAssert(entry);
68   }
69 
testImmutableEntryToString()70   public void testImmutableEntryToString() {
71     Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a');
72     assertEquals("(foo,1)=a", entry.toString());
73 
74     Cell<String, Integer, Character> nullEntry = Tables.immutableCell(null, null, null);
75     assertEquals("(null,null)=null", nullEntry.toString());
76   }
77 
testEntryEquals()78   public void testEntryEquals() {
79     Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a');
80 
81     new EqualsTester()
82         .addEqualityGroup(entry, Tables.immutableCell("foo", 1, 'a'))
83         .addEqualityGroup(Tables.immutableCell("bar", 1, 'a'))
84         .addEqualityGroup(Tables.immutableCell("foo", 2, 'a'))
85         .addEqualityGroup(Tables.immutableCell("foo", 1, 'b'))
86         .addEqualityGroup(Tables.immutableCell(null, null, null))
87         .testEquals();
88   }
89 
testEntryEqualsNull()90   public void testEntryEqualsNull() {
91     Cell<String, Integer, Character> entry = Tables.immutableCell(null, null, null);
92 
93     new EqualsTester()
94         .addEqualityGroup(entry, Tables.immutableCell(null, null, null))
95         .addEqualityGroup(Tables.immutableCell("bar", null, null))
96         .addEqualityGroup(Tables.immutableCell(null, 2, null))
97         .addEqualityGroup(Tables.immutableCell(null, null, 'b'))
98         .addEqualityGroup(Tables.immutableCell("foo", 1, 'a'))
99         .testEquals();
100   }
101 }
102