/** * Return a new, populated map. The mappings in the map should match the * keys and values returned from {@link #getSampleKeys()} and * {@link #getSampleValues()}. The default implementation uses makeEmptyMap() * and calls {@link #addSampleMappings} to add all the mappings to the * map. * * @return the map to be tested */ public Map makeFullMap() { Map m = makeEmptyMap(); addSampleMappings(m); return m; }
@Test public void testEntrySetRemove2() { if (!isRemoveSupported()) { return; } resetFull(); int size = map.size(); Set entrySet = map.entrySet(); Map.Entry entry = (Map.Entry) entrySet.iterator().next(); Object key = entry.getKey(); Map.Entry test = cloneMapEntry(entry); assertEquals(true, entrySet.remove(test)); assertEquals(false, map.containsKey(key)); assertEquals(size - 1, map.size()); }
if (!isRemoveSupported()) { try { resetFull(); map.remove(map.keySet().iterator().next()); fail("Expected UnsupportedOperationException on remove"); resetEmpty(); Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for (int i = 0; i < keys.length; i++) { Object o = map.remove(keys[i]); assertTrue("First map.remove should return null", o == null); verifyAll(); resetFull(); verifyAll(); Object[] other = getOtherKeys(); resetFull(); int size = map.size(); for (int i = 0; i < other.length; i++) { verifyAll();
public Object[] getOtherElements() { Object[] k = getOtherKeys(); Object[] v = getOtherValues(); return makeEntryArray(k, v); }
public Object[] getFullElements() { Object[] k = getSampleKeys(); Object[] v = getSampleValues(); return makeEntryArray(k, v); }
/** * Tests Map.size() */ @Test public void testMapSize() { resetEmpty(); assertEquals("Map.size() should be 0 with an empty map", 0, map.size()); verifyAll(); resetFull(); assertEquals("Map.size() should equal the number of entries in the map", getSampleKeys().length, map.size()); verifyAll(); }
resetEmpty(); Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); Object[] newValues = getNewSampleValues(); if (isPutAddSupported()) { for (int i = 0; i < keys.length; i++) { Object o = map.put(keys[i], values[i]); confirmed.put(keys[i], values[i]); verifyAll(); assertTrue("First map.put should return null", o == null); assertTrue("Map should contain key after put", if (isPutChangeSupported()) { for (int i = 0; i < keys.length; i++) { Object o = map.put(keys[i], newValues[i]); confirmed.put(keys[i], newValues[i]); verifyAll(); assertEquals("Map.put should return previous value when changed", values[i], o); if (!isAllowDuplicateValues()) { assertTrue("Map should not contain old value after put when changed", !map.containsValue(values[i])); else if (isPutChangeSupported()) { resetEmpty(); try { map.put(keys[0], values[0]);
if (!isPutAddSupported()) { if (!isPutChangeSupported()) { Map temp = makeFullMap(); resetEmpty(); try { map.putAll(temp); resetEmpty(); Map m2 = makeFullMap(); verifyAll(); resetEmpty(); m2 = makeConfirmedMap(); Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for (int i = 0; i < keys.length; i++) { m2.put(keys[i], values[i]); verifyAll();
/** * Helper method to add all the mappings described by * {@link #getSampleKeys()} and {@link #getSampleValues()}. */ public void addSampleMappings(Map m) { Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for (int i = 0; i < keys.length; i++) { try { m.put(keys[i], values[i]); } catch (NullPointerException exception) { assertTrue("NullPointerException only allowed to be thrown if either the key or value is null.", keys[i] == null || values[i] == null); assertTrue("NullPointerException on null key, but isAllowNullKey is not overridden to return false.", keys[i] == null || !isAllowNullKey()); assertTrue("NullPointerException on null value, but isAllowNullValue is not overridden to return false.", values[i] == null || !isAllowNullValue()); assertTrue("Unknown reason for NullPointer.", false); } } assertEquals("size must reflect number of mappings added.", keys.length, m.size()); }
/** * Resets the {@link #map}, {@link #entrySet}, {@link #keySet}, * {@link #values} and {@link #confirmed} fields to full. */ public void resetFull() { this.map = makeFullMap(); views(); this.confirmed = makeConfirmedMap(); Object[] k = getSampleKeys(); Object[] v = getSampleValues(); for (int i = 0; i < k.length; i++) { confirmed.put(k[i], v[i]); } }
/** * Tests Map.put(null, value) */ @Test public void testMapPutNullValue() { resetFull(); Object[] keys = getSampleKeys(); if (isPutAddSupported()) { if (isAllowNullValue()) { map.put(keys[0], null); } else { try { map.put(keys[0], null); fail("put(key, null) should throw NPE/IAE"); } catch (NullPointerException ex) { } catch (IllegalArgumentException ex) { } } } }
/** * Tests Map.put(null, value) */ @Test public void testMapPutNullKey() { resetFull(); Object[] values = getSampleValues(); if (isPutAddSupported()) { if (isAllowNullKey()) { map.put(null, values[0]); } else { try { map.put(null, values[0]); fail("put(null, value) should throw NPE/IAE"); } catch (NullPointerException ex) { } catch (IllegalArgumentException ex) { } } } }
@Test public void testMapEntrySetIteratorEntrySetValue() { Object key1 = getSampleKeys()[0]; Object key2 = (getSampleKeys().length == 1 ? getSampleKeys()[0] : getSampleKeys()[1]); Object newValue1 = getNewSampleValues()[0]; Object newValue2 = (getNewSampleValues().length == 1 ? getNewSampleValues()[0] : getNewSampleValues()[1]); verifyAll(); if (isSetValueSupported() == false) { try { entry1.setValue(newValue1);
/** * Returns the set of values in the mappings used to test the map. This * method must return an array with the same length as * {@link #getSampleKeys()}. The default implementation constructs a set of * String values and includes a single null value if * {@link #isAllowNullValue()} returns {@code true}, and includes * two values that are the same if {@link #isAllowDuplicateValues()} returns * {@code true}. */ public Object[] getSampleValues() { Object[] result = new Object[] { "blahv", "foov", "barv", "bazv", "tmpv", "goshv", "gollyv", "geev", "hellov", "goodbyev", "we'llv", "seev", "youv", "allv", "againv", (isAllowNullValue() && !JDK12) ? null : "nonnullvalue", "value", (isAllowDuplicateValues()) ? "value" : "value2", }; return result; }
@Test public void testEntrySetContains2() { resetFull(); Set entrySet = map.entrySet(); Map.Entry entry = (Map.Entry) entrySet.iterator().next(); Map.Entry test = cloneMapEntry(entry); assertEquals(true, entrySet.contains(test)); }
/** * Tests that the {@link Map#keySet} set is backed by * the underlying map by removing from the keySet set * and testing if the key was removed from the map. */ @Test public void testKeySetRemoveChangesMap() { resetFull(); Object[] sampleKeys = getSampleKeys(); Set keys = map.keySet(); for (int i = 0; i < sampleKeys.length; i++) { try { keys.remove(sampleKeys[i]); } catch (UnsupportedOperationException e) { // if key.remove is unsupported, just skip this test return; } assertTrue( "Key should have been removed from the underlying map.", !map.containsKey(sampleKeys[i])); } }
resetFull(); Object[] sampleValues = getSampleValues(); Collection values = map.values(); for (int i = 0; i < sampleValues.length; i++) {
public boolean isNullSupported() { return MapAbstractTest.this.isAllowNullKey(); }
public Object[] getOtherKeys() { return getOtherNonNullStringElements(); }
public Object[] getOtherElements() { return getOtherKeys(); }