1 54 55 package junitx.extensions; 56 57 import junit.framework.AssertionFailedError; 58 import junit.framework.TestCase; 59 import junitx.framework.Assert; 60 61 81 public abstract class EqualsHashCodeTestCase 82 extends TestCase { 83 84 private Object eq1; 85 private Object eq2; 86 private Object eq3; 87 private Object neq; 88 private static final int NUM_ITERATIONS = 20; 89 90 95 public EqualsHashCodeTestCase(String name) { 96 super(name); 97 } 98 99 106 protected abstract Object createInstance() throws Exception ; 107 108 116 protected abstract Object createNotEqualInstance() throws Exception ; 117 118 123 protected void setUp() throws Exception { 124 super.setUp(); 125 126 eq1 = createInstance(); 127 eq2 = createInstance(); 128 eq3 = createInstance(); 129 neq = createNotEqualInstance(); 130 131 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 (ex.getMessage()); 159 } 160 } 161 162 166 public final void testEqualsAgainstNewObject() { 167 Object o = new Object (); 168 169 Assert.assertNotEquals(o, eq1); 170 Assert.assertNotEquals(o, eq2); 171 Assert.assertNotEquals(o, eq3); 172 Assert.assertNotEquals(o, neq); 173 } 174 175 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 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 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 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 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 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 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 |