• 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 java.util.List;
29 
30 /**
31  * Tester for {@link Equivalence} relationships between groups of objects.
32  *
33  * <p>To use, create a new {@link EquivalenceTester} and add equivalence groups where each group
34  * contains objects that are supposed to be equal to each other. Objects of different groups are
35  * expected to be unequal. For example:
36  *
37  * <pre>{@code
38  * EquivalenceTester.of(someStringEquivalence)
39  *     .addEquivalenceGroup("hello", "h" + "ello")
40  *     .addEquivalenceGroup("world", "wor" + "ld")
41  *     .test();
42  * }</pre>
43  *
44  * <p>Note that testing {@link Object#equals(Object)} is more simply done using the {@link
45  * EqualsTester}. It includes an extra test against an instance of an arbitrary class without having
46  * to explicitly add another equivalence group.
47  *
48  * @author Gregory Kick
49  * @since 10.0
50  */
51 @GwtCompatible
52 public final class EquivalenceTester<T> {
53   private static final int REPETITIONS = 3;
54 
55   private final Equivalence<? super T> equivalence;
56   private final RelationshipTester<T> delegate;
57   private final List<T> items = Lists.newArrayList();
58 
EquivalenceTester(Equivalence<? super T> equivalence)59   private EquivalenceTester(Equivalence<? super T> equivalence) {
60     this.equivalence = checkNotNull(equivalence);
61     this.delegate = new RelationshipTester<>(equivalence, "equivalent", "hash", new ItemReporter());
62   }
63 
of(Equivalence<? super T> equivalence)64   public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
65     return new EquivalenceTester<>(equivalence);
66   }
67 
68   /**
69    * Adds a group of objects that are supposed to be equivalent to each other and not equivalent to
70    * objects in any other equivalence group added to this tester.
71    */
addEquivalenceGroup(T first, T... rest)72   public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
73     addEquivalenceGroup(Lists.asList(first, rest));
74     return this;
75   }
76 
addEquivalenceGroup(Iterable<T> group)77   public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
78     delegate.addRelatedGroup(group);
79     items.addAll(ImmutableList.copyOf(group));
80     return this;
81   }
82 
83   /** Run tests on equivalence methods, throwing a failure on an invalid test */
test()84   public EquivalenceTester<T> test() {
85     for (int run = 0; run < REPETITIONS; run++) {
86       testItems();
87       delegate.test();
88     }
89     return this;
90   }
91 
testItems()92   private void testItems() {
93     for (T item : items) {
94       /*
95        * TODO(cpovirk): consider no longer running these equivalent() tests on every Equivalence,
96        * since the Equivalence base type now implements this logic itself
97        */
98       assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null));
99       assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item));
100       assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item));
101       assertEquals(
102           "the hash of " + item + " must be consistent",
103           equivalence.hash(item),
104           equivalence.hash(item));
105     }
106   }
107 }
108