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.base.Preconditions.checkArgument;
20
21 import com.google.common.annotations.GwtCompatible;
22
23 import java.util.Map;
24
25
26
27
28
29
30
31 @GwtCompatible
32 public abstract class AbstractTableTest extends AbstractTableReadTest {
33
34 protected void populate(
35 Table<String, Integer, Character> table, Object... data) {
36 checkArgument(data.length % 3 == 0);
37 for (int i = 0; i < data.length; i+= 3) {
38 table.put(
39 (String) data[i], (Integer) data[i+1], (Character) data[i+2]);
40 }
41 }
42
43 protected boolean supportsRemove() {
44 return true;
45 }
46
47 protected boolean supportsNullValues() {
48 return false;
49 }
50
51 public void testClear() {
52 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
53 if (supportsRemove()) {
54 table.clear();
55 assertEquals(0, table.size());
56 assertFalse(table.containsRow("foo"));
57 } else {
58 try {
59 table.clear();
60 fail();
61 } catch (UnsupportedOperationException expected) {}
62 }
63 }
64
65 public void testPut() {
66 assertNull(table.put("foo", 1, 'a'));
67 assertNull(table.put("bar", 1, 'b'));
68 assertNull(table.put("foo", 3, 'c'));
69 assertEquals((Character) 'a', table.put("foo", 1, 'd'));
70 assertEquals((Character) 'd', table.get("foo", 1));
71 assertEquals((Character) 'b', table.get("bar", 1));
72 assertSize(3);
73 assertEquals((Character) 'd', table.put("foo", 1, 'd'));
74 assertEquals((Character) 'd', table.get("foo", 1));
75 assertSize(3);
76 }
77
78
79 public void testPutNull() {
80 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
81 assertSize(3);
82 try {
83 table.put(null, 2, 'd');
84 fail();
85 } catch (NullPointerException expected) {}
86 try {
87 table.put("cat", null, 'd');
88 fail();
89 } catch (NullPointerException expected) {}
90 if (supportsNullValues()) {
91 assertNull(table.put("cat", 2, null));
92 assertTrue(table.contains("cat", 2));
93 } else {
94 try {
95 table.put("cat", 2, null);
96 fail();
97 } catch (NullPointerException expected) {}
98 }
99 assertSize(3);
100 }
101
102 public void testPutNullReplace() {
103 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
104
105 if (supportsNullValues()) {
106 assertEquals((Character) 'b', table.put("bar", 1, null));
107 assertNull(table.get("bar", 1));
108 } else {
109 try {
110 table.put("bar", 1, null);
111 fail();
112 } catch (NullPointerException expected) {}
113 }
114 }
115
116 public void testPutAllTable() {
117 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
118 Table<String, Integer, Character> other = HashBasedTable.create();
119 other.put("foo", 1, 'd');
120 other.put("bar", 2, 'e');
121 other.put("cat", 2, 'f');
122 table.putAll(other);
123 assertEquals((Character) 'd', table.get("foo", 1));
124 assertEquals((Character) 'b', table.get("bar", 1));
125 assertEquals((Character) 'c', table.get("foo", 3));
126 assertEquals((Character) 'e', table.get("bar", 2));
127 assertEquals((Character) 'f', table.get("cat", 2));
128 assertSize(5);
129 }
130
131 public void testRemove() {
132 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
133 if (supportsRemove()) {
134 assertNull(table.remove("cat", 1));
135 assertNull(table.remove("bar", 3));
136 assertEquals(3, table.size());
137 assertEquals((Character) 'c', table.remove("foo", 3));
138 assertEquals(2, table.size());
139 assertEquals((Character) 'a', table.get("foo", 1));
140 assertEquals((Character) 'b', table.get("bar", 1));
141 assertNull(table.get("foo", 3));
142 assertNull(table.remove(null, 1));
143 assertNull(table.remove("foo", null));
144 assertNull(table.remove(null, null));
145 assertSize(2);
146 } else {
147 try {
148 table.remove("foo", 3);
149 fail();
150 } catch (UnsupportedOperationException expected) {}
151 assertEquals((Character) 'c', table.get("foo", 3));
152 }
153 }
154
155 public void testRowClearAndPut() {
156 if (supportsRemove()) {
157 table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
158 Map<Integer, Character> row = table.row("foo");
159 assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), row);
160 table.remove("foo", 3);
161 assertEquals(ImmutableMap.of(1, 'a'), row);
162 table.remove("foo", 1);
163 assertEquals(ImmutableMap.of(), row);
164 table.put("foo", 2, 'b');
165 assertEquals(ImmutableMap.of(2, 'b'), row);
166 row.clear();
167 assertEquals(ImmutableMap.of(), row);
168 table.put("foo", 5, 'x');
169 assertEquals(ImmutableMap.of(5, 'x'), row);
170 }
171 }
172 }