• 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.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 import junit.framework.TestCase;
27 
28 /**
29  * Test cases for {@link Table} read operations.
30  *
31  * @author Jared Levy
32  */
33 @GwtCompatible(emulated = true)
34 public abstract class AbstractTableReadTest extends TestCase {
35   protected Table<String, Integer, Character> table;
36 
37   /**
38    * Creates a table with the specified data.
39    *
40    * @param data the table data, repeating the sequence row key, column key, value once per mapping
41    * @throws IllegalArgumentException if the size of {@code data} isn't a multiple of 3
42    * @throws ClassCastException if a data element has the wrong type
43    */
create(Object... data)44   protected abstract Table<String, Integer, Character> create(Object... data);
45 
assertSize(int expectedSize)46   protected void assertSize(int expectedSize) {
47     assertEquals(expectedSize, table.size());
48   }
49 
50   @Override
setUp()51   public void setUp() throws Exception {
52     super.setUp();
53     table = create();
54   }
55 
testContains()56   public void testContains() {
57     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
58     assertTrue(table.contains("foo", 1));
59     assertTrue(table.contains("bar", 1));
60     assertTrue(table.contains("foo", 3));
61     assertFalse(table.contains("foo", 2));
62     assertFalse(table.contains("bar", 3));
63     assertFalse(table.contains("cat", 1));
64     assertFalse(table.contains("foo", null));
65     assertFalse(table.contains(null, 1));
66     assertFalse(table.contains(null, null));
67   }
68 
testContainsRow()69   public void testContainsRow() {
70     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
71     assertTrue(table.containsRow("foo"));
72     assertTrue(table.containsRow("bar"));
73     assertFalse(table.containsRow("cat"));
74     assertFalse(table.containsRow(null));
75   }
76 
testContainsColumn()77   public void testContainsColumn() {
78     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
79     assertTrue(table.containsColumn(1));
80     assertTrue(table.containsColumn(3));
81     assertFalse(table.containsColumn(2));
82     assertFalse(table.containsColumn(null));
83   }
84 
testContainsValue()85   public void testContainsValue() {
86     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
87     assertTrue(table.containsValue('a'));
88     assertTrue(table.containsValue('b'));
89     assertTrue(table.containsValue('c'));
90     assertFalse(table.containsValue('x'));
91     assertFalse(table.containsValue(null));
92   }
93 
testGet()94   public void testGet() {
95     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
96     assertEquals((Character) 'a', table.get("foo", 1));
97     assertEquals((Character) 'b', table.get("bar", 1));
98     assertEquals((Character) 'c', table.get("foo", 3));
99     assertNull(table.get("foo", 2));
100     assertNull(table.get("bar", 3));
101     assertNull(table.get("cat", 1));
102     assertNull(table.get("foo", null));
103     assertNull(table.get(null, 1));
104     assertNull(table.get(null, null));
105   }
106 
testIsEmpty()107   public void testIsEmpty() {
108     assertTrue(table.isEmpty());
109     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
110     assertFalse(table.isEmpty());
111   }
112 
testSize()113   public void testSize() {
114     assertSize(0);
115     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
116     assertSize(3);
117   }
118 
testEquals()119   public void testEquals() {
120     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
121     Table<String, Integer, Character> hashCopy = HashBasedTable.create(table);
122     Table<String, Integer, Character> reordered =
123         create("foo", 3, 'c', "foo", 1, 'a', "bar", 1, 'b');
124     Table<String, Integer, Character> smaller = create("foo", 1, 'a', "bar", 1, 'b');
125     Table<String, Integer, Character> swapOuter =
126         create("bar", 1, 'a', "foo", 1, 'b', "bar", 3, 'c');
127     Table<String, Integer, Character> swapValues =
128         create("foo", 1, 'c', "bar", 1, 'b', "foo", 3, 'a');
129 
130     new EqualsTester()
131         .addEqualityGroup(table, hashCopy, reordered)
132         .addEqualityGroup(smaller)
133         .addEqualityGroup(swapOuter)
134         .addEqualityGroup(swapValues)
135         .testEquals();
136   }
137 
testHashCode()138   public void testHashCode() {
139     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
140     int expected =
141         Objects.hashCode("foo", 1, 'a')
142             + Objects.hashCode("bar", 1, 'b')
143             + Objects.hashCode("foo", 3, 'c');
144     assertEquals(expected, table.hashCode());
145   }
146 
testToStringSize1()147   public void testToStringSize1() {
148     table = create("foo", 1, 'a');
149     assertEquals("{foo={1=a}}", table.toString());
150   }
151 
testRow()152   public void testRow() {
153     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
154     assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), table.row("foo"));
155   }
156 
157   // This test assumes that the implementation does not support null keys.
testRowNull()158   public void testRowNull() {
159     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
160     try {
161       table.row(null);
162       fail();
163     } catch (NullPointerException expected) {
164     }
165   }
166 
testColumn()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   // This test assumes that the implementation does not support null keys.
testColumnNull()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 
testColumnSetPartialOverlap()182   public void testColumnSetPartialOverlap() {
183     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 2, 'c', "bar", 3, 'd');
184     assertThat(table.columnKeySet()).containsExactly(1, 2, 3);
185   }
186 
187   @GwtIncompatible // NullPointerTester
testNullPointerInstance()188   public void testNullPointerInstance() {
189     table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 2, 'c', "bar", 3, 'd');
190     new NullPointerTester().testAllPublicInstanceMethods(table);
191   }
192 }
193