/** * Makes a Map.Entry of a type that's known to work correctly. */ public Map.Entry makeKnownMapEntry() { return makeKnownMapEntry(null, null); }
/** * Make an instance of Map.Entry with the specified key and value. * Subclasses should override this method to return a Map.Entry * of the type being tested. */ public abstract Map.Entry makeMapEntry(Object key, Object value);
/** * Subclasses should override this method to test the * desired behaviour of the class with respect to * handling of self-references. * */ public void testSelfReferenceHandling() { // test that #setValue does not permit // the MapEntry to contain itself (and thus cause infinite recursion // in #hashCode and #toString) Map.Entry entry = makeMapEntry(); try { entry.setValue(entry); fail("Should throw an IllegalArgumentException"); } catch (IllegalArgumentException iae) { // expected to happen... // check that the KVP's state has not changed assertTrue(entry.getKey() == null && entry.getValue() == null); } }
public void testEqualsAndHashCode() { // 1. test with object data Map.Entry e1 = makeMapEntry(key, value); Map.Entry e2 = makeKnownMapEntry(key, value); assertTrue(e1.equals(e1)); assertTrue(e2.equals(e1)); assertTrue(e1.equals(e2)); assertTrue(e1.hashCode() == e2.hashCode()); // 2. test with nulls e1 = makeMapEntry(); e2 = makeKnownMapEntry(); assertTrue(e1.equals(e1)); assertTrue(e2.equals(e1)); assertTrue(e1.equals(e2)); assertTrue(e1.hashCode() == e2.hashCode()); }
public void testAccessorsAndMutators() { Map.Entry entry = makeMapEntry(key, value); assertTrue(entry.getKey() == key); entry.setValue(value); assertTrue(entry.getValue() == value); // check that null doesn't do anything funny entry = makeMapEntry(null, null); assertTrue(entry.getKey() == null); entry.setValue(null); assertTrue(entry.getValue() == null); }
public void testToString() { Map.Entry entry = makeMapEntry(key, value); assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue())); // test with nulls entry = makeMapEntry(); assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue())); }