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