• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.checkNotNull;
20 import static junit.framework.Assert.assertEquals;
21 import static junit.framework.Assert.assertTrue;
22 
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;
29 import 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
54 public final class EquivalenceTester<T> {
55   private static final int REPETITIONS = 3;
56 
57   private final Equivalence<? super T> equivalence;
58   private final RelationshipTester<T> delegate;
59   private final List<T> items = Lists.newArrayList();
60 
EquivalenceTester(Equivalence<? super T> equivalence)61   private EquivalenceTester(Equivalence<? super T> equivalence) {
62     this.equivalence = checkNotNull(equivalence);
63     this.delegate = new RelationshipTester<>(equivalence, "equivalent", "hash", new ItemReporter());
64   }
65 
of(Equivalence<? super T> equivalence)66   public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
67     return 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   @CanIgnoreReturnValue
addEquivalenceGroup(T first, T... rest)75   public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
76     addEquivalenceGroup(Lists.asList(first, rest));
77     return this;
78   }
79 
80   @CanIgnoreReturnValue
addEquivalenceGroup(Iterable<T> group)81   public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
82     delegate.addRelatedGroup(group);
83     items.addAll(ImmutableList.copyOf(group));
84     return this;
85   }
86 
87   /** Run tests on equivalence methods, throwing a failure on an invalid test */
88   @CanIgnoreReturnValue
test()89   public EquivalenceTester<T> test() {
90     for (int run = 0; run < REPETITIONS; run++) {
91       testItems();
92       delegate.test();
93     }
94     return this;
95   }
96 
testItems()97   private void testItems() {
98     for (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        */
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));
106       assertEquals(
107           "the hash of " + item + " must be consistent",
108           equivalence.hash(item),
109           equivalence.hash(item));
110     }
111   }
112 }
113