Coverage Summary for Class: EquivalenceTester (com.google.common.testing)

Class Class, % Method, % Line, %
EquivalenceTester 100% (1/1) 100% (6/6) 100% (24/24)


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>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 @Beta 53 @GwtCompatible 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  61  private EquivalenceTester(Equivalence<? super T> equivalence) { 62  this.equivalence = checkNotNull(equivalence); 63  this.delegate = 64  new RelationshipTester<T>(equivalence, "equivalent", "hash", new ItemReporter()); 65  } 66  67  public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) { 68  return new EquivalenceTester<T>(equivalence); 69  } 70  71  /** 72  * Adds a group of objects that are supposed to be equivalent to each other and not equivalent to 73  * objects in any other equivalence group added to this tester. 74  */ 75  public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) { 76  addEquivalenceGroup(Lists.asList(first, rest)); 77  return this; 78  } 79  80  public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) { 81  delegate.addRelatedGroup(group); 82  items.addAll(ImmutableList.copyOf(group)); 83  return this; 84  } 85  86  /** Run tests on equivalence methods, throwing a failure on an invalid test */ 87  public EquivalenceTester<T> test() { 88  for (int run = 0; run < REPETITIONS; run++) { 89  testItems(); 90  delegate.test(); 91  } 92  return this; 93  } 94  95  private void testItems() { 96  for (T item : items) { 97  /* 98  * TODO(cpovirk): consider no longer running these equivalent() tests on every Equivalence, 99  * since the Equivalence base type now implements this logic itself 100  */ 101  assertTrue(item + " must be inequivalent to null", !equivalence.equivalent(item, null)); 102  assertTrue("null must be inequivalent to " + item, !equivalence.equivalent(null, item)); 103  assertTrue(item + " must be equivalent to itself", equivalence.equivalent(item, item)); 104  assertEquals( 105  "the hash of " + item + " must be consistent", 106  equivalence.hash(item), 107  equivalence.hash(item)); 108  } 109  } 110 }