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.base.Equivalence; 22 import com.google.common.collect.Table.Cell; 23 import com.google.common.testing.CollectorTester; 24 import com.google.common.testing.EqualsTester; 25 import com.google.common.testing.SerializableTester; 26 import java.util.stream.Collector; 27 import java.util.stream.Stream; 28 import junit.framework.TestCase; 29 30 /** 31 * Tests for {@link Tables}. 32 * 33 * @author Jared Levy 34 */ 35 @GwtCompatible(emulated = true) 36 public class TablesTest extends TestCase { 37 testToTable()38 public void testToTable() { 39 Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = 40 Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); 41 Equivalence<Table<String, String, Integer>> equivalence = 42 Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::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 Tables.immutableCell("one", "uno", 1), 51 Tables.immutableCell("two", "dos", 2), 52 Tables.immutableCell("three", "tres", 3)); 53 } 54 testToTableNullMerge()55 public void testToTableNullMerge() { 56 Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = 57 Tables.toTable( 58 Cell::getRowKey, 59 Cell::getColumnKey, 60 Cell::getValue, 61 (Integer v1, Integer v2) -> null, 62 HashBasedTable::create); 63 Equivalence<Table<String, String, Integer>> equivalence = 64 Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); 65 CollectorTester.of(collector, equivalence) 66 .expectCollects( 67 ImmutableTable.of(), 68 Tables.immutableCell("one", "uno", 1), 69 Tables.immutableCell("one", "uno", 2)); 70 } 71 testToTableNullValues()72 public void testToTableNullValues() { 73 Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = 74 Tables.toTable( 75 Cell::getRowKey, 76 Cell::getColumnKey, 77 Cell::getValue, 78 () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"))); 79 try { 80 Stream.of(Tables.immutableCell("one", "uno", (Integer) null)).collect(collector); 81 fail("Expected NullPointerException"); 82 } catch (NullPointerException expected) { 83 } 84 } 85 testToTableConflict()86 public void testToTableConflict() { 87 Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = 88 Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create); 89 try { 90 Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) 91 .collect(collector); 92 fail("Expected IllegalStateException"); 93 } catch (IllegalStateException expected) { 94 } 95 } 96 testToTableMerging()97 public void testToTableMerging() { 98 Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = 99 Tables.toTable( 100 Cell::getRowKey, 101 Cell::getColumnKey, 102 Cell::getValue, 103 Integer::sum, 104 HashBasedTable::create); 105 Equivalence<Table<String, String, Integer>> equivalence = 106 Equivalence.equals().<Cell<String, String, Integer>>pairwise().onResultOf(Table::cellSet); 107 CollectorTester.of(collector, equivalence) 108 .expectCollects( 109 new ImmutableTable.Builder<String, String, Integer>() 110 .put("one", "uno", 1) 111 .put("two", "dos", 6) 112 .put("three", "tres", 3) 113 .build(), 114 Tables.immutableCell("one", "uno", 1), 115 Tables.immutableCell("two", "dos", 2), 116 Tables.immutableCell("three", "tres", 3), 117 Tables.immutableCell("two", "dos", 4)); 118 } 119 120 @GwtIncompatible // SerializableTester testImmutableEntrySerialization()121 public void testImmutableEntrySerialization() { 122 Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); 123 SerializableTester.reserializeAndAssert(entry); 124 } 125 testImmutableEntryToString()126 public void testImmutableEntryToString() { 127 Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); 128 assertEquals("(foo,1)=a", entry.toString()); 129 130 Cell<String, Integer, Character> nullEntry = Tables.immutableCell(null, null, null); 131 assertEquals("(null,null)=null", nullEntry.toString()); 132 } 133 testEntryEquals()134 public void testEntryEquals() { 135 Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a'); 136 137 new EqualsTester() 138 .addEqualityGroup(entry, Tables.immutableCell("foo", 1, 'a')) 139 .addEqualityGroup(Tables.immutableCell("bar", 1, 'a')) 140 .addEqualityGroup(Tables.immutableCell("foo", 2, 'a')) 141 .addEqualityGroup(Tables.immutableCell("foo", 1, 'b')) 142 .addEqualityGroup(Tables.immutableCell(null, null, null)) 143 .testEquals(); 144 } 145 testEntryEqualsNull()146 public void testEntryEqualsNull() { 147 Cell<String, Integer, Character> entry = Tables.immutableCell(null, null, null); 148 149 new EqualsTester() 150 .addEqualityGroup(entry, Tables.immutableCell(null, null, null)) 151 .addEqualityGroup(Tables.immutableCell("bar", null, null)) 152 .addEqualityGroup(Tables.immutableCell(null, 2, null)) 153 .addEqualityGroup(Tables.immutableCell(null, null, 'b')) 154 .addEqualityGroup(Tables.immutableCell("foo", 1, 'a')) 155 .testEquals(); 156 } 157 } 158