KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > extensions > EqualsHashCodeTestCase


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.extensions;
56
57 import junit.framework.AssertionFailedError;
58 import junit.framework.TestCase;
59 import junitx.framework.Assert;
60
61 /**
62  * Extend me in order to test a class's functional compliance with the
63  * <code>equals</code> and <code>hashCode</code> contract.
64  * <p>
65  * Override my {@link #createInstance() createInstance} and
66  * {@link #createNotEqualInstance() createNotEqualInstance} methods to
67  * provide me with objects to test against. Both methods should return
68  * objects that are of the same class.
69  * <p>
70  * <b>WARNING</b>: Extend me only if your class overrides
71  * <code>equals</code> to test for equivalence. If your class's
72  * <code>equals</code> tests for identity or preserves the behavior from
73  * <code>Object</code>, I'm not interested, because I expect
74  * <code>createInstance</code> to return equivalent but distinct objects.
75  *
76  * @see java.lang.Object#equals(Object)
77  * @see java.lang.Object#hashCode()
78  * @version $Revision: 1.2 $ $Date: 2003/02/06 20:43:52 $
79  * @author <a HREF="mailto:pholser@yahoo.com">Paul Holser</a>
80  */

81 public abstract class EqualsHashCodeTestCase
82         extends TestCase {
83
84     private Object JavaDoc eq1;
85     private Object JavaDoc eq2;
86     private Object JavaDoc eq3;
87     private Object JavaDoc neq;
88     private static final int NUM_ITERATIONS = 20;
89
90     /**
91      * Creates a new test.
92      *
93      * @param name name of the test
94      */

95     public EqualsHashCodeTestCase(String JavaDoc name) {
96         super(name);
97     }
98
99     /**
100      * Creates and returns an instance of the class under test.
101      *
102      * @return a new instance of the class under test; each object returned
103      * from this method should compare equal to each other.
104      * @throws Exception
105      */

106     protected abstract Object JavaDoc createInstance() throws Exception JavaDoc;
107
108     /**
109      * Creates and returns an instance of the class under test.
110      *
111      * @return a new instance of the class under test; each object returned
112      * from this method should compare equal to each other, but not to the
113      * objects returned from {@link #createInstance() createInstance}.
114      * @throws Exception
115      */

116     protected abstract Object JavaDoc createNotEqualInstance() throws Exception JavaDoc;
117
118     /**
119      * Sets up the test fixture.
120      *
121      * @throws Exception
122      */

123     protected void setUp() throws Exception JavaDoc {
124         super.setUp();
125
126         eq1 = createInstance();
127         eq2 = createInstance();
128         eq3 = createInstance();
129         neq = createNotEqualInstance();
130
131         // We want these assertions to yield errors, not failures.
132
try {
133             assertNotNull("createInstance() returned null", eq1);
134             assertNotNull("2nd createInstance() returned null", eq2);
135             assertNotNull("3rd createInstance() returned null", eq3);
136             assertNotNull("createNotEqualInstance() returned null", neq);
137
138             Assert.assertNotSame(eq1, eq2);
139             Assert.assertNotSame(eq1, eq3);
140             Assert.assertNotSame(eq1, neq);
141             Assert.assertNotSame(eq2, eq3);
142             Assert.assertNotSame(eq2, neq);
143             Assert.assertNotSame(eq3, neq);
144
145             assertEquals(
146                     "1st and 2nd equal instances of different classes",
147                     eq1.getClass(),
148                     eq2.getClass());
149             assertEquals(
150                     "1st and 3rd equal instances of different classes",
151                     eq1.getClass(),
152                     eq3.getClass());
153             assertEquals(
154                     "1st equal instance and not-equal instance of different classes",
155                     eq1.getClass(),
156                     neq.getClass());
157         } catch (AssertionFailedError ex) {
158             throw new IllegalArgumentException JavaDoc(ex.getMessage());
159         }
160     }
161
162     /**
163      * Tests whether <code>equals</code> holds up against a new
164      * <code>Object</code> (should always be <code>false</code>).
165      */

166     public final void testEqualsAgainstNewObject() {
167         Object JavaDoc o = new Object JavaDoc();
168
169         Assert.assertNotEquals(o, eq1);
170         Assert.assertNotEquals(o, eq2);
171         Assert.assertNotEquals(o, eq3);
172         Assert.assertNotEquals(o, neq);
173     }
174
175     /**
176      * Tests whether <code>equals</code> holds up against <code>null</code>.
177      */

178     public final void testEqualsAgainstNull() {
179         Assert.assertNotEquals("null vs. 1st", null, eq1);
180         Assert.assertNotEquals("null vs. 2nd", null, eq2);
181         Assert.assertNotEquals("null vs. 3rd", null, eq3);
182         Assert.assertNotEquals("null vs. not-equal", null, neq);
183     }
184
185     /**
186      * Tests whether <code>equals</code> holds up against objects that should
187      * not compare equal.
188      */

189     public final void testEqualsAgainstUnequalObjects() {
190         Assert.assertNotEquals("1st vs. not-equal", eq1, neq);
191         Assert.assertNotEquals("2nd vs. not-equal", eq2, neq);
192         Assert.assertNotEquals("3rd vs. not-equal", eq3, neq);
193
194         Assert.assertNotEquals("not-equal vs. 1st", neq, eq1);
195         Assert.assertNotEquals("not-equal vs. 2nd", neq, eq2);
196         Assert.assertNotEquals("not-equal vs. 3rd", neq, eq3);
197     }
198
199     /**
200      * Tests whether <code>equals</code> is <em>consistent</em>.
201      */

202     public final void testEqualsIsConsistentAcrossInvocations() {
203         for (int i = 0; i < NUM_ITERATIONS; ++i) {
204             testEqualsAgainstNewObject();
205             testEqualsAgainstNull();
206             testEqualsAgainstUnequalObjects();
207             testEqualsIsReflexive();
208             testEqualsIsSymmetricAndTransitive();
209         }
210     }
211
212     /**
213      * Tests whether <code>equals</code> is <em>reflexive</em>.
214      */

215     public final void testEqualsIsReflexive() {
216         assertEquals("1st equal instance", eq1, eq1);
217         assertEquals("2nd equal instance", eq2, eq2);
218         assertEquals("3rd equal instance", eq3, eq3);
219         assertEquals("not-equal instance", neq, neq);
220     }
221
222     /**
223      * Tests whether <code>equals</code> is <em>symmetric</em> and
224      * <em>transitive</em>.
225      */

226     public final void testEqualsIsSymmetricAndTransitive() {
227         assertEquals("1st vs. 2nd", eq1, eq2);
228         assertEquals("2nd vs. 1st", eq2, eq1);
229
230         assertEquals("1st vs. 3rd", eq1, eq3);
231         assertEquals("3rd vs. 1st", eq3, eq1);
232
233         assertEquals("2nd vs. 3rd", eq2, eq3);
234         assertEquals("3rd vs. 2nd", eq3, eq2);
235     }
236
237     /**
238      * Tests the <code>hashCode</code> contract.
239      */

240     public final void testHashCodeContract() {
241         assertEquals("1st vs. 2nd", eq1.hashCode(), eq2.hashCode());
242         assertEquals("1st vs. 3rd", eq1.hashCode(), eq3.hashCode());
243         assertEquals("2nd vs. 3rd", eq2.hashCode(), eq3.hashCode());
244     }
245
246     /**
247      * Tests the consistency of <code>hashCode</code>.
248      */

249     public final void testHashCodeIsConsistentAcrossInvocations() {
250         int eq1Hash = eq1.hashCode();
251         int eq2Hash = eq2.hashCode();
252         int eq3Hash = eq3.hashCode();
253         int neqHash = neq.hashCode();
254
255         for (int i = 0; i < NUM_ITERATIONS; ++i) {
256             assertEquals("1st equal instance", eq1Hash, eq1.hashCode());
257             assertEquals("2nd equal instance", eq2Hash, eq2.hashCode());
258             assertEquals("3rd equal instance", eq3Hash, eq3.hashCode());
259             assertEquals("not-equal instance", neqHash, neq.hashCode());
260         }
261     }
262
263 }
264
Popular Tags