View Javadoc
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 com.google.common.annotations.GwtCompatible;
20  import com.google.common.annotations.GwtIncompatible;
21  import com.google.common.testing.NullPointerTester;
22  import com.google.common.testing.SerializableTester;
23  
24  /**
25   * Test cases for {@link HashBasedTable}.
26   *
27   * @author Jared Levy
28   */
29  @GwtCompatible(emulated = true)
30  public class HashBasedTableTest extends AbstractTableTest {
31  
32    @Override protected Table<String, Integer, Character> create(
33        Object... data) {
34      Table<String, Integer, Character> table = HashBasedTable.create();
35      table.put("foo", 4, 'a');
36      table.put("cat", 1, 'b');
37      table.clear();
38      populate(table, data);
39      return table;
40    }
41  
42    public void testCreateWithValidSizes() {
43      Table<String, Integer, Character> table1 = HashBasedTable.create(100, 20);
44      table1.put("foo", 1, 'a');
45      assertEquals((Character) 'a', table1.get("foo", 1));
46  
47      Table<String, Integer, Character> table2 = HashBasedTable.create(100, 0);
48      table2.put("foo", 1, 'a');
49      assertEquals((Character) 'a', table2.get("foo", 1));
50  
51      Table<String, Integer, Character> table3 = HashBasedTable.create(0, 20);
52      table3.put("foo", 1, 'a');
53      assertEquals((Character) 'a', table3.get("foo", 1));
54  
55      Table<String, Integer, Character> table4 = HashBasedTable.create(0, 0);
56      table4.put("foo", 1, 'a');
57      assertEquals((Character) 'a', table4.get("foo", 1));
58    }
59  
60    public void testCreateWithInvalidSizes() {
61      try {
62        HashBasedTable.create(100, -5);
63        fail();
64      } catch (IllegalArgumentException expected) {}
65  
66      try {
67        HashBasedTable.create(-5, 20);
68        fail();
69      } catch (IllegalArgumentException expected) {}
70    }
71  
72    public void testCreateCopy() {
73      Table<String, Integer, Character> original
74          = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
75      Table<String, Integer, Character> copy = HashBasedTable.create(original);
76      assertEquals(original, copy);
77      assertEquals((Character) 'a', copy.get("foo", 1));
78    }
79  
80    @GwtIncompatible("SerializableTester")
81    public void testSerialization() {
82      table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
83      SerializableTester.reserializeAndAssert(table);
84    }
85  
86    @GwtIncompatible("NullPointerTester")
87    public void testNullPointerStatic() {
88      new NullPointerTester().testAllPublicStaticMethods(HashBasedTable.class);
89    }
90  }