/** Tests if equal and hashCode method still hold the contract. */ @Test public void testEqualsAndHashCode() { String failureReason; SortedMapWritable mapA = new SortedMapWritable(); SortedMapWritable mapB = new SortedMapWritable(); // Sanity checks failureReason = "SortedMapWritable couldn't be initialized. Got null reference"; assertNotNull(failureReason, mapA); assertNotNull(failureReason, mapB); // Basic null check assertFalse("equals method returns true when passed null", mapA.equals(null)); // When entry set is empty, they should be equal assertTrue("Two empty SortedMapWritables are no longer equal", mapA.equals(mapB)); // Setup Text[] keys = {new Text("key1"), new Text("key2")}; BytesWritable[] values = { new BytesWritable("value1".getBytes()), new BytesWritable("value2".getBytes()) }; mapA.put(keys[0], values[0]); mapB.put(keys[1], values[1]); // entrySets are different failureReason = "Two SortedMapWritables with different data are now equal"; assertTrue(failureReason, mapA.hashCode() != mapB.hashCode()); assertTrue(failureReason, !mapA.equals(mapB)); assertTrue(failureReason, !mapB.equals(mapA)); mapA.put(keys[1], values[1]); mapB.put(keys[0], values[0]); // entrySets are now same failureReason = "Two SortedMapWritables with same entry sets formed in different order are now different"; assertEquals(failureReason, mapA.hashCode(), mapB.hashCode()); assertTrue(failureReason, mapA.equals(mapB)); assertTrue(failureReason, mapB.equals(mapA)); // Let's check if entry sets of same keys but different values mapA.put(keys[0], values[1]); mapA.put(keys[1], values[0]); failureReason = "Two SortedMapWritables with different content are now equal"; assertTrue(failureReason, mapA.hashCode() != mapB.hashCode()); assertTrue(failureReason, !mapA.equals(mapB)); assertTrue(failureReason, !mapB.equals(mapA)); }