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