/** * 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; }
/** * Returns a the set of values that can be used to replace the values * returned from {@link #getSampleValues()}. This method must return an * array with the same length as {@link #getSampleValues()}. The values * returned from this method should not be the same as those returned from * {@link #getSampleValues()}. 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[] getNewSampleValues() { Object[] result = new Object[] { (isAllowNullValue() && !JDK12 && isAllowDuplicateValues()) ? null : "newnonnullvalue", "newvalue", (isAllowDuplicateValues()) ? "newvalue" : "newvalue2", "newblahv", "newfoov", "newbarv", "newbazv", "newtmpv", "newgoshv", "newgollyv", "newgeev", "newhellov", "newgoodbyev", "newwe'llv", "newseev", "newyouv", "newallv", "newagainv", }; return result; }
/** * 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) { } } } }
/** * 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()); }
assertTrue("failure in test: found null value, but isNullValueSupported is false.", values[i] != null || isAllowNullValue()); assertTrue("failure in test: found null new value, but isNullValueSupported is false.", newValues[i] != null || isAllowNullValue()); assertTrue("failure in test: values should not be the same as new value", values[i] != newValues[i] && (values[i] == null || !values[i].equals(newValues[i])));
/** * 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; }
/** * Returns a the set of values that can be used to replace the values * returned from {@link #getSampleValues()}. This method must return an * array with the same length as {@link #getSampleValues()}. The values * returned from this method should not be the same as those returned from * {@link #getSampleValues()}. 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[] getNewSampleValues() { Object[] result = new Object[] { (isAllowNullValue() && !JDK12 && isAllowDuplicateValues()) ? null : "newnonnullvalue", "newvalue", (isAllowDuplicateValues()) ? "newvalue" : "newvalue2", "newblahv", "newfoov", "newbarv", "newbazv", "newtmpv", "newgoshv", "newgollyv", "newgeev", "newhellov", "newgoodbyev", "newwe'llv", "newseev", "newyouv", "newallv", "newagainv", }; return result; }
/** * 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) { } } } }
/** * 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()); }
assertTrue("failure in test: found null value, but isNullValueSupported is false.", values[i] != null || isAllowNullValue()); assertTrue("failure in test: found null new value, but isNullValueSupported is false.", newValues[i] != null || isAllowNullValue()); assertTrue("failure in test: values should not be the same as new value", values[i] != newValues[i] && (values[i] == null || !values[i].equals(newValues[i])));