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  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.base.Equivalence;
23  import com.google.common.collect.ImmutableList;
24  import com.google.common.collect.Lists;
25  import java.util.List;
26  import junit.framework.AssertionFailedError;
27  
28  /**
29   * Implementation helper for {@link EqualsTester} and {@link EquivalenceTester} that tests for
30   * equivalence classes.
31   *
32   * @author Gregory Kick
33   */
34  @GwtCompatible
35  final class RelationshipTester<T> {
36  
37    static class ItemReporter {
38      String reportItem(Item<?> item) {
39        return item.toString();
40      }
41    }
42  
43    /**
44     * A word about using {@link Equivalence}, which automatically checks for {@code null} and
45     * identical inputs: This sounds like it ought to be a problem here, since the goals of this class
46     * include testing that {@code equals()} is reflexive and is tolerant of {@code null}. However,
47     * there's no problem. The reason: {@link EqualsTester} tests {@code null} and identical inputs
48     * directly against {@code equals()} rather than through the {@code Equivalence}.
49     */
50    private final Equivalence<? super T> equivalence;
51    private final String relationshipName;
52    private final String hashName;
53    private final ItemReporter itemReporter;
54    private final List<ImmutableList<T>> groups = Lists.newArrayList();
55  
56    RelationshipTester(Equivalence<? super T> equivalence, String relationshipName, String hashName,
57        ItemReporter itemReporter) {
58      this.equivalence = checkNotNull(equivalence);
59      this.relationshipName = checkNotNull(relationshipName);
60      this.hashName = checkNotNull(hashName);
61      this.itemReporter = checkNotNull(itemReporter);
62    }
63  
64    // TODO(cpovirk): should we reject null items, since the tests already check null automatically?
65    public RelationshipTester<T> addRelatedGroup(Iterable<? extends T> group) {
66      groups.add(ImmutableList.copyOf(group));
67      return this;
68    }
69  
70    public void test() {
71      for (int groupNumber = 0; groupNumber < groups.size(); groupNumber++) {
72        ImmutableList<T> group = groups.get(groupNumber);
73        for (int itemNumber = 0; itemNumber < group.size(); itemNumber++) {
74          // check related items in same group
75          for (int relatedItemNumber = 0; relatedItemNumber < group.size(); relatedItemNumber++) {
76            if (itemNumber != relatedItemNumber) {
77              assertRelated(groupNumber, itemNumber, relatedItemNumber);
78            }
79          }
80          // check unrelated items in all other groups
81          for (int unrelatedGroupNumber = 0; unrelatedGroupNumber < groups.size();
82              unrelatedGroupNumber++) {
83            if (groupNumber != unrelatedGroupNumber) {
84              ImmutableList<T> unrelatedGroup = groups.get(unrelatedGroupNumber);
85              for (int unrelatedItemNumber = 0; unrelatedItemNumber < unrelatedGroup.size();
86                  unrelatedItemNumber++) {
87                assertUnrelated(groupNumber, itemNumber, unrelatedGroupNumber, unrelatedItemNumber);
88              }
89            }
90          }
91        }
92      }
93    }
94  
95    private void assertRelated(int groupNumber, int itemNumber, int relatedItemNumber) {
96      Item<T> itemInfo = getItem(groupNumber, itemNumber);
97      Item<T> relatedInfo = getItem(groupNumber, relatedItemNumber);
98  
99      T item = itemInfo.value;
100     T related = relatedInfo.value;
101     assertWithTemplate("$ITEM must be $RELATIONSHIP to $OTHER", itemInfo, relatedInfo,
102         equivalence.equivalent(item, related));
103 
104     int itemHash = equivalence.hash(item);
105     int relatedHash = equivalence.hash(related);
106     assertWithTemplate("the $HASH (" + itemHash + ") of $ITEM must be equal to the $HASH ("
107         + relatedHash + ") of $OTHER", itemInfo, relatedInfo, itemHash == relatedHash);
108   }
109 
110   private void assertUnrelated(int groupNumber, int itemNumber, int unrelatedGroupNumber,
111       int unrelatedItemNumber) {
112     Item<T> itemInfo = getItem(groupNumber, itemNumber);
113     Item<T> unrelatedInfo = getItem(unrelatedGroupNumber, unrelatedItemNumber);
114 
115     assertWithTemplate("$ITEM must not be $RELATIONSHIP to $OTHER", itemInfo, unrelatedInfo,
116         !equivalence.equivalent(itemInfo.value, unrelatedInfo.value));
117   }
118 
119   private void assertWithTemplate(String template, Item<T> item, Item<T> other, boolean condition) {
120     if (!condition) {
121       throw new AssertionFailedError(template
122           .replace("$RELATIONSHIP", relationshipName)
123           .replace("$HASH", hashName)
124           .replace("$ITEM", itemReporter.reportItem(item))
125           .replace("$OTHER", itemReporter.reportItem(other)));
126     }
127   }
128 
129   private Item<T> getItem(int groupNumber, int itemNumber) {
130     return new Item<T>(groups.get(groupNumber).get(itemNumber), groupNumber, itemNumber);
131   }
132 
133   static final class Item<T> {
134     final T value;
135     final int groupNumber;
136     final int itemNumber;
137 
138     Item(T value, int groupNumber, int itemNumber) {
139       this.value = value;
140       this.groupNumber = groupNumber;
141       this.itemNumber = itemNumber;
142     }
143 
144     @Override public String toString() {
145       return value + " [group " + (groupNumber + 1) + ", item " + (itemNumber + 1) + ']';
146     }
147   }
148 }