aboutsummaryrefslogtreecommitdiffstats
path: root/guava-testlib/src/com/google/common/testing/EquivalenceTester.java
blob: 40e5a27f33363f4589ff2175b54fc0b1a3e705dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2011 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.testing;

import static com.google.common.base.Preconditions.checkNotNull;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Equivalence;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.testing.RelationshipTester.RelationshipAssertion;

import java.util.List;

/**
 * Tester for {@link Equivalence} relationships between groups of objects.
 *
 * <p>
 * To use, create a new {@link EquivalenceTester} and add equivalence groups
 * where each group contains objects that are supposed to be equal to each
 * other. Objects of different groups are expected to be unequal. For example:
 *
 * <pre>
 * {@code
 * EquivalenceTester.of(someStringEquivalence)
 *     .addEquivalenceGroup("hello", "h" + "ello")
 *     .addEquivalenceGroup("world", "wor" + "ld")
 *     .test();
 * }
 * </pre>
 *
 * <p>
 * Note that testing {@link Objects#equals(Object)} is more simply done using
 * the {@link EqualsTester}. It includes an extra test against an instance of an
 * arbitrary class without having to explicitly add another equivalence group.
 *
 * @author Gregory Kick
 * @since 10.0
 *
 * TODO(gak): turn this into a test suite so that each test can fail
 * independently
 */
@Beta
@GwtCompatible public final class EquivalenceTester<T> {
  private static final int REPETITIONS = 3;

  private final Equivalence<? super T> equivalence;
  private final RelationshipTester<T> delegate;
  private final List<T> items = Lists.newArrayList();

  EquivalenceTester(final Equivalence<? super T> equivalence) {
    this.equivalence = checkNotNull(equivalence);
    this.delegate = new RelationshipTester<T>(new RelationshipAssertion<T>() {
      @Override public void assertRelated(T item, T related) {
        assertTrue("$ITEM must be equivalent to $RELATED", equivalence.equivalent(item, related));
        int itemHash = equivalence.hash(item);
        int relatedHash = equivalence.hash(related);
        assertEquals("the hash (" + itemHash + ") of $ITEM must be equal to the hash ("
            + relatedHash + ") of $RELATED", itemHash, relatedHash);
      }

      @Override public void assertUnrelated(T item, T unrelated) {
        assertTrue("$ITEM must be inequivalent to $UNRELATED",
            !equivalence.equivalent(item, unrelated));
      }
    });
  }

  public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
    return new EquivalenceTester<T>(equivalence);
  }

  /**
   * Adds a group of objects that are supposed to be equivalent to each other
   * and not equivalent to objects in any other equivalence group added to this
   * tester.
   */
  public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
    addEquivalenceGroup(Lists.asList(first, rest));
    return this;
  }

  public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
    delegate.addRelatedGroup(group);
    items.addAll(ImmutableList.copyOf(group));
    return this;
  }

  /** Run tests on equivalence methods, throwing a failure on an invalid test */
  public EquivalenceTester<T> test() {
    for (int run = 0; run < REPETITIONS; run++) {
      testItems();
      delegate.test();
    }
    return this;
  }

  private void testItems() {
    for (T item : items) {
      assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));
      assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));
      assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));
      assertEquals("the hash of " + item + " must be consistent", equivalence.hash(item),
          equivalence.hash(item));
    }
  }
}