1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.truth.Truth.assertThat;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.base.Objects;
24 import com.google.common.testing.EqualsTester;
25 import com.google.common.testing.NullPointerTester;
26
27 import junit.framework.TestCase;
28
29
30
31
32
33
34 @GwtCompatible(emulated = true)
35 public abstract class AbstractTableReadTest extends TestCase {
36 protected Table<String, Integer, Character> table;
37
38
39
40
41
42
43
44
45
46
47 protected abstract Table<String, Integer, Character>
48 create(Object... data);
49
50 protected void assertSize(int expectedSize) {
51 assertEquals(expectedSize, table.size());
52 }
53
54 @Override public void setUp() throws Exception {
55 super.setUp();
56 table = create();
57 }
58
59 public void testContains() {
60 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
61 assertTrue(table.contains("foo", 1));
62 assertTrue(table.contains("bar", 1));
63 assertTrue(table.contains("foo", 3));
64 assertFalse(table.contains("foo", 2));
65 assertFalse(table.contains("bar", 3));
66 assertFalse(table.contains("cat", 1));
67 assertFalse(table.contains("foo", null));
68 assertFalse(table.contains(null, 1));
69 assertFalse(table.contains(null, null));
70 }
71
72 public void testContainsRow() {
73 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
74 assertTrue(table.containsRow("foo"));
75 assertTrue(table.containsRow("bar"));
76 assertFalse(table.containsRow("cat"));
77 assertFalse(table.containsRow(null));
78 }
79
80 public void testContainsColumn() {
81 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
82 assertTrue(table.containsColumn(1));
83 assertTrue(table.containsColumn(3));
84 assertFalse(table.containsColumn(2));
85 assertFalse(table.containsColumn(null));
86 }
87
88 public void testContainsValue() {
89 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
90 assertTrue(table.containsValue('a'));
91 assertTrue(table.containsValue('b'));
92 assertTrue(table.containsValue('c'));
93 assertFalse(table.containsValue('x'));
94 assertFalse(table.containsValue(null));
95 }
96
97 public void testGet() {
98 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
99 assertEquals((Character) 'a', table.get("foo", 1));
100 assertEquals((Character) 'b', table.get("bar", 1));
101 assertEquals((Character) 'c', table.get("foo", 3));
102 assertNull(table.get("foo", 2));
103 assertNull(table.get("bar", 3));
104 assertNull(table.get("cat", 1));
105 assertNull(table.get("foo", null));
106 assertNull(table.get(null, 1));
107 assertNull(table.get(null, null));
108 }
109
110 public void testIsEmpty() {
111 assertTrue(table.isEmpty());
112 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
113 assertFalse(table.isEmpty());
114 }
115
116 public void testSize() {
117 assertSize(0);
118 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
119 assertSize(3);
120 }
121
122 public void testEquals() {
123 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
124 Table<String, Integer, Character> hashCopy = HashBasedTable.create(table);
125 Table<String, Integer, Character> reordered
126 = create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b');
127 Table<String, Integer, Character> smaller
128 = create("foo", 1, 'a', "bar", 1, 'b');
129 Table<String, Integer, Character> swapOuter
130 = create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c');
131 Table<String, Integer, Character> swapValues
132 = create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a');
133
134 new EqualsTester()
135 .addEqualityGroup(table, hashCopy, reordered)
136 .addEqualityGroup(smaller)
137 .addEqualityGroup(swapOuter)
138 .addEqualityGroup(swapValues)
139 .testEquals();
140 }
141
142 public void testHashCode() {
143 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
144 int expected = Objects.hashCode("foo", 1, 'a')
145 + Objects.hashCode("bar", 1, 'b')
146 + Objects.hashCode("foo", 3, 'c');
147 assertEquals(expected, table.hashCode());
148 }
149
150 public void testToStringSize1() {
151 table = create("foo", 1, 'a');
152 assertEquals("{foo={1=a}}", table.toString());
153 }
154
155 public void testRow() {
156 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
157 assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), table.row("foo"));
158 }
159
160
161 public void testRowNull() {
162 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
163 try {
164 table.row(null);
165 fail();
166 } catch (NullPointerException expected) {}
167 }
168
169 public void testColumn() {
170 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
171 assertEquals(ImmutableMap.of("foo", 'a', "bar", 'b'), table.column(1));
172 }
173
174
175 public void testColumnNull() {
176 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
177 try {
178 table.column(null);
179 fail();
180 } catch (NullPointerException expected) {}
181 }
182
183 public void testColumnSetPartialOverlap() {
184 table = create(
185 "foo", 1, 'a', "bar", 1, 'b', "foo", 2, 'c', "bar", 3, 'd');
186 assertThat(table.columnKeySet()).has().exactly(1, 2, 3);
187 }
188
189 @GwtIncompatible("NullPointerTester")
190 public void testNullPointerInstance() {
191 table = create(
192 "foo", 1, 'a', "bar", 1, 'b', "foo", 2, 'c', "bar", 3, 'd');
193 new NullPointerTester().testAllPublicInstanceMethods(table);
194 }
195 }