guava
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.GwtCompatible;24import com.google.common.base.Equivalence;25import com.google.common.collect.ImmutableList;26import com.google.common.collect.Lists;27import com.google.common.testing.RelationshipTester.ItemReporter;28import com.google.errorprone.annotations.CanIgnoreReturnValue;29import java.util.List;30
31/**
32* Tester for {@link Equivalence} relationships between groups of objects.
33*
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:
37*
38* <pre>{@code
39* EquivalenceTester.of(someStringEquivalence)
40* .addEquivalenceGroup("hello", "h" + "ello")
41* .addEquivalenceGroup("world", "wor" + "ld")
42* .test();
43* }</pre>
44*
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.
48*
49* @author Gregory Kick
50* @since 10.0
51*/
52@GwtCompatible
53@ElementTypesAreNonnullByDefault
54public final class EquivalenceTester<T> {55private static final int REPETITIONS = 3;56
57private final Equivalence<? super T> equivalence;58private final RelationshipTester<T> delegate;59private final List<T> items = Lists.newArrayList();60
61private EquivalenceTester(Equivalence<? super T> equivalence) {62this.equivalence = checkNotNull(equivalence);63this.delegate = new RelationshipTester<>(equivalence, "equivalent", "hash", new ItemReporter());64}65
66public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {67return new EquivalenceTester<>(equivalence);68}69
70/**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.
73*/
74@CanIgnoreReturnValue75public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {76addEquivalenceGroup(Lists.asList(first, rest));77return this;78}79
80@CanIgnoreReturnValue81public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {82delegate.addRelatedGroup(group);83items.addAll(ImmutableList.copyOf(group));84return this;85}86
87/** Run tests on equivalence methods, throwing a failure on an invalid test */88@CanIgnoreReturnValue89public EquivalenceTester<T> test() {90for (int run = 0; run < REPETITIONS; run++) {91testItems();92delegate.test();93}94return this;95}96
97private void testItems() {98for (T item : items) {99/*100* TODO(cpovirk): consider no longer running these equivalent() tests on every Equivalence,
101* since the Equivalence base type now implements this logic itself
102*/
103assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));104assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));105assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));106assertEquals(107"the hash of " + item + " must be consistent",108equivalence.hash(item),109equivalence.hash(item));110}111}112}
113