1 /* 2 * Copyright (C) 2007 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.Iterables; 27 import com.google.common.collect.Lists; 28 import java.util.List; 29 30 /** 31 * Tester for equals() and hashCode() methods of a class. 32 * 33 * <p>The simplest use case is: 34 * 35 * <pre> 36 * new EqualsTester().addEqualityGroup(foo).testEquals(); 37 * </pre> 38 * 39 * <p>This tests {@code foo.equals(foo)}, {@code foo.equals(null)}, and a few other operations. 40 * 41 * <p>For more extensive testing, add multiple equality groups. Each group should contain objects 42 * that are equal to each other but unequal to the objects in any other group. For example: 43 * 44 * <pre> 45 * new EqualsTester() 46 * .addEqualityGroup(new User("page"), new User("page")) 47 * .addEqualityGroup(new User("sergey")) 48 * .testEquals(); 49 * </pre> 50 * 51 * <p>This tests: 52 * 53 * <ul> 54 * <li>comparing each object against itself returns true 55 * <li>comparing each object against null returns false 56 * <li>comparing each object against an instance of an incompatible class returns false 57 * <li>comparing each pair of objects within the same equality group returns true 58 * <li>comparing each pair of objects from different equality groups returns false 59 * <li>the hash codes of any two equal objects are equal 60 * </ul> 61 * 62 * <p>When a test fails, the error message labels the objects involved in the failed comparison as 63 * follows: 64 * 65 * <ul> 66 * <li>"{@code [group }<i>i</i>{@code , item }<i>j</i>{@code ]}" refers to the 67 * <i>j</i><sup>th</sup> item in the <i>i</i><sup>th</sup> equality group, where both equality 68 * groups and the items within equality groups are numbered starting from 1. When either a 69 * constructor argument or an equal object is provided, that becomes group 1. 70 * </ul> 71 * 72 * @author Jim McMaster 73 * @author Jige Yu 74 * @since 10.0 75 */ 76 @GwtCompatible 77 public final class EqualsTester { 78 private static final int REPETITIONS = 3; 79 80 private final List<List<Object>> equalityGroups = Lists.newArrayList(); 81 private final RelationshipTester.ItemReporter itemReporter; 82 83 /** Constructs an empty EqualsTester instance */ EqualsTester()84 public EqualsTester() { 85 this(new RelationshipTester.ItemReporter()); 86 } 87 EqualsTester(RelationshipTester.ItemReporter itemReporter)88 EqualsTester(RelationshipTester.ItemReporter itemReporter) { 89 this.itemReporter = checkNotNull(itemReporter); 90 } 91 92 /** 93 * Adds {@code equalityGroup} with objects that are supposed to be equal to each other and not 94 * equal to any other equality groups added to this tester. 95 */ addEqualityGroup(Object... equalityGroup)96 public EqualsTester addEqualityGroup(Object... equalityGroup) { 97 checkNotNull(equalityGroup); 98 equalityGroups.add(ImmutableList.copyOf(equalityGroup)); 99 return this; 100 } 101 102 /** Run tests on equals method, throwing a failure on an invalid test */ testEquals()103 public EqualsTester testEquals() { 104 RelationshipTester<Object> delegate = 105 new RelationshipTester<>( 106 Equivalence.equals(), "Object#equals", "Object#hashCode", itemReporter); 107 for (List<Object> group : equalityGroups) { 108 delegate.addRelatedGroup(group); 109 } 110 for (int run = 0; run < REPETITIONS; run++) { 111 testItems(); 112 delegate.test(); 113 } 114 return this; 115 } 116 testItems()117 private void testItems() { 118 for (Object item : Iterables.concat(equalityGroups)) { 119 assertTrue(item + " must not be Object#equals to null", !item.equals(null)); 120 assertTrue( 121 item + " must not be Object#equals to an arbitrary object of another class", 122 !item.equals(NotAnInstance.EQUAL_TO_NOTHING)); 123 assertTrue(item + " must be Object#equals to itself", item.equals(item)); 124 assertEquals( 125 "the Object#hashCode of " + item + " must be consistent", 126 item.hashCode(), 127 item.hashCode()); 128 if (!(item instanceof String)) { 129 assertTrue( 130 item + " must not be Object#equals to its Object#toString representation", 131 !item.equals(item.toString())); 132 } 133 } 134 } 135 136 /** 137 * Class used to test whether equals() correctly handles an instance of an incompatible class. 138 * Since it is a private inner class, the invoker can never pass in an instance to the tester 139 */ 140 private enum NotAnInstance { 141 EQUAL_TO_NOTHING; 142 } 143 } 144