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