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