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
17package com.google.common.testing;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20import static junit.framework.Assert.assertEquals;
21import static junit.framework.Assert.assertTrue;
22
23import com.google.common.annotations.Beta;
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.base.Equivalence;
26import com.google.common.collect.ImmutableList;
27import com.google.common.collect.Lists;
28import com.google.common.testing.RelationshipTester.RelationshipAssertion;
29
30import java.util.List;
31
32/**
33 * Tester for {@link Equivalence} relationships between groups of objects.
34 *
35 * <p>
36 * To use, create a new {@link EquivalenceTester} and add equivalence groups
37 * where each group contains objects that are supposed to be equal to each
38 * other. Objects of different groups are expected to be unequal. For example:
39 *
40 * <pre>
41 * {@code
42 * EquivalenceTester.of(someStringEquivalence)
43 *     .addEquivalenceGroup("hello", "h" + "ello")
44 *     .addEquivalenceGroup("world", "wor" + "ld")
45 *     .test();
46 * }
47 * </pre>
48 *
49 * <p>
50 * Note that testing {@link Objects#equals(Object)} is more simply done using
51 * the {@link EqualsTester}. It includes an extra test against an instance of an
52 * arbitrary class without having to explicitly add another equivalence group.
53 *
54 * @author Gregory Kick
55 * @since 10.0
56 *
57 * TODO(gak): turn this into a test suite so that each test can fail
58 * independently
59 */
60@Beta
61@GwtCompatible public final class EquivalenceTester<T> {
62  private static final int REPETITIONS = 3;
63
64  private final Equivalence<? super T> equivalence;
65  private final RelationshipTester<T> delegate;
66  private final List<T> items = Lists.newArrayList();
67
68  EquivalenceTester(final Equivalence<? super T> equivalence) {
69    this.equivalence = checkNotNull(equivalence);
70    this.delegate = new RelationshipTester<T>(new RelationshipAssertion<T>() {
71      @Override public void assertRelated(T item, T related) {
72        assertTrue("$ITEM must be equivalent to $RELATED", equivalence.equivalent(item, related));
73        int itemHash = equivalence.hash(item);
74        int relatedHash = equivalence.hash(related);
75        assertEquals("the hash (" + itemHash + ") of $ITEM must be equal to the hash ("
76            + relatedHash + ") of $RELATED", itemHash, relatedHash);
77      }
78
79      @Override public void assertUnrelated(T item, T unrelated) {
80        assertTrue("$ITEM must be inequivalent to $UNRELATED",
81            !equivalence.equivalent(item, unrelated));
82      }
83    });
84  }
85
86  public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
87    return new EquivalenceTester<T>(equivalence);
88  }
89
90  /**
91   * Adds a group of objects that are supposed to be equivalent to each other
92   * and not equivalent to objects in any other equivalence group added to this
93   * tester.
94   */
95  public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
96    addEquivalenceGroup(Lists.asList(first, rest));
97    return this;
98  }
99
100  public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
101    delegate.addRelatedGroup(group);
102    items.addAll(ImmutableList.copyOf(group));
103    return this;
104  }
105
106  /** Run tests on equivalence methods, throwing a failure on an invalid test */
107  public EquivalenceTester<T> test() {
108    for (int run = 0; run < REPETITIONS; run++) {
109      testItems();
110      delegate.test();
111    }
112    return this;
113  }
114
115  private void testItems() {
116    for (T item : items) {
117      assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));
118      assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));
119      assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));
120      assertEquals("the hash of " + item + " must be consistent", equivalence.hash(item),
121          equivalence.hash(item));
122    }
123  }
124}
125