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