2
* Copyright (C) 2011 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.testing;
19
import static com.google.common.base.Preconditions.checkNotNull;
20
import static junit.framework.Assert.assertEquals;
21
import static junit.framework.Assert.assertTrue;
23
import com.google.common.annotations.GwtCompatible;
24
import com.google.common.base.Equivalence;
25
import com.google.common.collect.ImmutableList;
26
import com.google.common.collect.Lists;
27
import com.google.common.testing.RelationshipTester.ItemReporter;
28
import com.google.errorprone.annotations.CanIgnoreReturnValue;
32
* Tester for {@link Equivalence} relationships between groups of objects.
34
* <p>To use, create a new {@link EquivalenceTester} and add equivalence groups where each group
35
* contains objects that are supposed to be equal to each other. Objects of different groups are
36
* expected to be unequal. For example:
39
* EquivalenceTester.of(someStringEquivalence)
40
* .addEquivalenceGroup("hello", "h" + "ello")
41
* .addEquivalenceGroup("world", "wor" + "ld")
45
* <p>Note that testing {@link Object#equals(Object)} is more simply done using the {@link
46
* EqualsTester}. It includes an extra test against an instance of an arbitrary class without having
47
* to explicitly add another equivalence group.
49
* @author Gregory Kick
53
@ElementTypesAreNonnullByDefault
54
public final class EquivalenceTester<T> {
55
private static final int REPETITIONS = 3;
57
private final Equivalence<? super T> equivalence;
58
private final RelationshipTester<T> delegate;
59
private final List<T> items = Lists.newArrayList();
61
private EquivalenceTester(Equivalence<? super T> equivalence) {
62
this.equivalence = checkNotNull(equivalence);
63
this.delegate = new RelationshipTester<>(equivalence, "equivalent", "hash", new ItemReporter());
66
public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
67
return new EquivalenceTester<>(equivalence);
71
* Adds a group of objects that are supposed to be equivalent to each other and not equivalent to
72
* objects in any other equivalence group added to this tester.
75
public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
76
addEquivalenceGroup(Lists.asList(first, rest));
81
public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
82
delegate.addRelatedGroup(group);
83
items.addAll(ImmutableList.copyOf(group));
87
/** Run tests on equivalence methods, throwing a failure on an invalid test */
89
public EquivalenceTester<T> test() {
90
for (int run = 0; run < REPETITIONS; run++) {
97
private void testItems() {
98
for (T item : items) {
100
* TODO(cpovirk): consider no longer running these equivalent() tests on every Equivalence,
101
* since the Equivalence base type now implements this logic itself
103
assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));
104
assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));
105
assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));
107
"the hash of " + item + " must be consistent",
108
equivalence.hash(item),
109
equivalence.hash(item));