Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2011 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.testing;
     18 
     19 import static com.google.common.base.Preconditions.checkState;
     20 
     21 import com.google.common.annotations.GwtCompatible;
     22 import com.google.common.base.Equivalence;
     23 import com.google.common.base.Objects;
     24 import com.google.common.collect.ImmutableMap;
     25 import com.google.common.collect.ImmutableTable;
     26 
     27 import junit.framework.AssertionFailedError;
     28 import junit.framework.TestCase;
     29 
     30 /**
     31  * Tests for {@link EquivalenceTester}.
     32  *
     33  * @author Gregory Kick
     34  */
     35 @GwtCompatible
     36 public class EquivalenceTesterTest extends TestCase {
     37   private EquivalenceTester<Object> tester;
     38   private MockEquivalence equivalenceMock;
     39 
     40   @Override public void setUp() throws Exception {
     41     super.setUp();
     42     this.equivalenceMock = new MockEquivalence();
     43     this.tester = EquivalenceTester.of(equivalenceMock);
     44   }
     45 
     46   /** Test null reference yields error */
     47   public void testOf_NullPointerException() {
     48     try {
     49       EquivalenceTester.of(null);
     50       fail("Should fail on null reference");
     51     } catch (NullPointerException expected) {}
     52   }
     53 
     54   public void testTest_NoData() {
     55     tester.test();
     56   }
     57 
     58   public void testTest() {
     59     Object group1Item1 = new TestObject(1, 1);
     60     Object group1Item2 = new TestObject(1, 2);
     61     Object group2Item1 = new TestObject(2, 1);
     62     Object group2Item2 = new TestObject(2, 2);
     63 
     64     equivalenceMock.expectEquivalent(group1Item1, group1Item2);
     65     equivalenceMock.expectDistinct(group1Item1, group2Item1);
     66     equivalenceMock.expectDistinct(group1Item1, group2Item2);
     67     equivalenceMock.expectEquivalent(group1Item2, group1Item1);
     68     equivalenceMock.expectDistinct(group1Item2, group2Item1);
     69     equivalenceMock.expectDistinct(group1Item2, group2Item2);
     70     equivalenceMock.expectDistinct(group2Item1, group1Item1);
     71     equivalenceMock.expectDistinct(group2Item1, group1Item2);
     72     equivalenceMock.expectEquivalent(group2Item1, group2Item2);
     73     equivalenceMock.expectDistinct(group2Item2, group1Item1);
     74     equivalenceMock.expectDistinct(group2Item2, group1Item2);
     75     equivalenceMock.expectEquivalent(group2Item2, group2Item1);
     76 
     77     equivalenceMock.expectHash(group1Item1, 1);
     78     equivalenceMock.expectHash(group1Item2, 1);
     79     equivalenceMock.expectHash(group2Item1, 2);
     80     equivalenceMock.expectHash(group2Item2, 2);
     81 
     82     equivalenceMock.replay();
     83 
     84     tester.addEquivalenceGroup(group1Item1, group1Item2)
     85         .addEquivalenceGroup(group2Item1, group2Item2)
     86         .test();
     87   }
     88 
     89   public void testTest_symmetric() {
     90     Object group1Item1 = new TestObject(1, 1);
     91     Object group1Item2 = new TestObject(1, 2);
     92 
     93     equivalenceMock.expectEquivalent(group1Item1, group1Item2);
     94     equivalenceMock.expectDistinct(group1Item2, group1Item1);
     95 
     96     equivalenceMock.expectHash(group1Item1, 1);
     97     equivalenceMock.expectHash(group1Item2, 1);
     98 
     99     equivalenceMock.replay();
    100 
    101     try {
    102       tester.addEquivalenceGroup(group1Item1, group1Item2).test();
    103     } catch (AssertionFailedError expected) {
    104       assertEquals("TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
    105           + "TestObject{group=1, item=1} [group 1, item 1]", expected.getMessage());
    106       return;
    107     }
    108     fail();
    109   }
    110 
    111   public void testTest_trasitive() {
    112     Object group1Item1 = new TestObject(1, 1);
    113     Object group1Item2 = new TestObject(1, 2);
    114     Object group1Item3 = new TestObject(1, 3);
    115 
    116     equivalenceMock.expectEquivalent(group1Item1, group1Item2);
    117     equivalenceMock.expectEquivalent(group1Item1, group1Item3);
    118     equivalenceMock.expectEquivalent(group1Item2, group1Item1);
    119     equivalenceMock.expectDistinct(group1Item2, group1Item3);
    120     equivalenceMock.expectEquivalent(group1Item3, group1Item1);
    121     equivalenceMock.expectEquivalent(group1Item3, group1Item2);
    122 
    123     equivalenceMock.expectHash(group1Item1, 1);
    124     equivalenceMock.expectHash(group1Item2, 1);
    125     equivalenceMock.expectHash(group1Item3, 1);
    126 
    127     equivalenceMock.replay();
    128 
    129     try {
    130       tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test();
    131     } catch (AssertionFailedError expected) {
    132       assertEquals("TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
    133           + "TestObject{group=1, item=3} [group 1, item 3]", expected.getMessage());
    134       return;
    135     }
    136     fail();
    137   }
    138 
    139   public void testTest_inequivalence() {
    140     Object group1Item1 = new TestObject(1, 1);
    141     Object group2Item1 = new TestObject(2, 1);
    142 
    143     equivalenceMock.expectEquivalent(group1Item1, group2Item1);
    144     equivalenceMock.expectDistinct(group2Item1, group1Item1);
    145 
    146     equivalenceMock.expectHash(group1Item1, 1);
    147     equivalenceMock.expectHash(group2Item1, 2);
    148 
    149     equivalenceMock.replay();
    150 
    151     try {
    152       tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test();
    153     } catch (AssertionFailedError expected) {
    154       assertEquals("TestObject{group=1, item=1} [group 1, item 1] must be inequivalent to "
    155           + "TestObject{group=2, item=1} [group 2, item 1]", expected.getMessage());
    156       return;
    157     }
    158     fail();
    159   }
    160 
    161   public void testTest_hash() {
    162     Object group1Item1 = new TestObject(1, 1);
    163     Object group1Item2 = new TestObject(1, 2);
    164 
    165     equivalenceMock.expectEquivalent(group1Item1, group1Item2);
    166     equivalenceMock.expectEquivalent(group1Item2, group1Item1);
    167 
    168     equivalenceMock.expectHash(group1Item1, 1);
    169     equivalenceMock.expectHash(group1Item2, 2);
    170 
    171     equivalenceMock.replay();
    172 
    173     try {
    174       tester.addEquivalenceGroup(group1Item1, group1Item2).test();
    175     } catch (AssertionFailedError expected) {
    176       String expectedMessage =
    177           "the hash (1) of TestObject{group=1, item=1} [group 1, item 1] must be "
    178           + "equal to the hash (2) of TestObject{group=1, item=2} [group 1, item 2]";
    179       if (!expected.getMessage().contains(expectedMessage)) {
    180         fail("<" + expected.getMessage() + "> expected to contain <" + expectedMessage + ">");
    181       }
    182       return;
    183     }
    184     fail();
    185   }
    186 
    187   /** An object with a friendly {@link #toString()}. */
    188   private static final class TestObject {
    189     final int group;
    190     final int item;
    191 
    192     TestObject(int group , int item) {
    193       this.group = group;
    194       this.item = item;
    195     }
    196 
    197     @Override public String toString() {
    198       return Objects.toStringHelper("TestObject")
    199           .add("group", group)
    200           .add("item", item)
    201           .toString();
    202     }
    203   }
    204 
    205   private static final class MockEquivalence extends Equivalence<Object> {
    206     final ImmutableTable.Builder<Object, Object, Boolean> equivalentExpectationsBuilder =
    207         ImmutableTable.builder();
    208     final ImmutableMap.Builder<Object, Integer> hashExpectationsBuilder =
    209         ImmutableMap.builder();
    210     ImmutableTable<Object, Object, Boolean> equivalentExpectations;
    211     ImmutableMap<Object, Integer> hashExpectations;
    212 
    213     void expectEquivalent(Object a, Object b) {
    214       checkRecording();
    215       equivalentExpectationsBuilder.put(a, b, true);
    216     }
    217 
    218     void expectDistinct(Object a, Object b) {
    219       checkRecording();
    220       equivalentExpectationsBuilder.put(a, b, false);
    221     }
    222 
    223     void expectHash(Object object, int hash) {
    224       checkRecording();
    225       hashExpectationsBuilder.put(object, hash);
    226     }
    227 
    228     void replay() {
    229       checkRecording();
    230       equivalentExpectations = equivalentExpectationsBuilder.build();
    231       hashExpectations = hashExpectationsBuilder.build();
    232     }
    233 
    234     @Override protected boolean doEquivalent(Object a, Object b) {
    235       return equivalentExpectations.get(a, b);
    236     }
    237 
    238     @Override protected int doHash(Object object) {
    239       return hashExpectations.get(object);
    240     }
    241 
    242     void checkRecording() {
    243       checkState(equivalentExpectations == null && hashExpectations == null);
    244     }
    245   }
    246 }
    247