001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static junit.framework.Assert.assertEquals;
021import static junit.framework.Assert.assertTrue;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.base.Equivalence;
025import com.google.common.collect.ImmutableList;
026import com.google.common.collect.Lists;
027import com.google.common.testing.RelationshipTester.ItemReporter;
028import com.google.errorprone.annotations.CanIgnoreReturnValue;
029import java.util.List;
030
031/**
032 * Tester for {@link Equivalence} relationships between groups of objects.
033 *
034 * <p>To use, create a new {@link EquivalenceTester} and add equivalence groups where each group
035 * contains objects that are supposed to be equal to each other. Objects of different groups are
036 * expected to be unequal. For example:
037 *
038 * <pre>{@code
039 * EquivalenceTester.of(someStringEquivalence)
040 *     .addEquivalenceGroup("hello", "h" + "ello")
041 *     .addEquivalenceGroup("world", "wor" + "ld")
042 *     .test();
043 * }</pre>
044 *
045 * <p>Note that testing {@link Object#equals(Object)} is more simply done using the {@link
046 * EqualsTester}. It includes an extra test against an instance of an arbitrary class without having
047 * to explicitly add another equivalence group.
048 *
049 * @author Gregory Kick
050 * @since 10.0
051 */
052@GwtCompatible
053@ElementTypesAreNonnullByDefault
054public final class EquivalenceTester<T> {
055  private static final int REPETITIONS = 3;
056
057  private final Equivalence<? super T> equivalence;
058  private final RelationshipTester<T> delegate;
059  private final List<T> items = Lists.newArrayList();
060
061  private EquivalenceTester(Equivalence<? super T> equivalence) {
062    this.equivalence = checkNotNull(equivalence);
063    this.delegate = new RelationshipTester<>(equivalence, "equivalent", "hash", new ItemReporter());
064  }
065
066  public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
067    return new EquivalenceTester<>(equivalence);
068  }
069
070  /**
071   * Adds a group of objects that are supposed to be equivalent to each other and not equivalent to
072   * objects in any other equivalence group added to this tester.
073   */
074  @CanIgnoreReturnValue
075  public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
076    addEquivalenceGroup(Lists.asList(first, rest));
077    return this;
078  }
079
080  @CanIgnoreReturnValue
081  public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
082    delegate.addRelatedGroup(group);
083    items.addAll(ImmutableList.copyOf(group));
084    return this;
085  }
086
087  /** Run tests on equivalence methods, throwing a failure on an invalid test */
088  @CanIgnoreReturnValue
089  public EquivalenceTester<T> test() {
090    for (int run = 0; run < REPETITIONS; run++) {
091      testItems();
092      delegate.test();
093    }
094    return this;
095  }
096
097  private void testItems() {
098    for (T item : items) {
099      /*
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}