rdonkin 2004/11/11 13:43:43 Modified: logging/optional/src/java/org/apache/commons/logging/impl WeakHashtable.java logging/optional/src/test/org/apache/commons/logging/impl WeakHashtableTest.java Log: Updated WeakHashtable so that the null handling symantics match the superclass. Unit test contributed by Brian Stansberry. Revision Changes Path 1.2 +10 -0 jakarta-commons/logging/optional/src/java/org/apache/commons/logging/impl/WeakHashtable.java Index: WeakHashtable.java =================================================================== RCS file: /home/cvs/jakarta-commons/logging/optional/src/java/org/apache/commons/logging/impl/WeakHashtable.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WeakHashtable.java 10 Nov 2004 22:59:24 -0000 1.1 +++ WeakHashtable.java 11 Nov 2004 21:43:43 -0000 1.2 @@ -23,6 +23,8 @@ /** * <p>Implementation of <code>Hashtable</code> that uses <code>WeakReference</code>'s * to hold it's keys thus allowing them to be reclaimed by the garbage collector. + * This class follows the symantics of <code>Hashtable</code> as closely as possible. + * It therefore does not accept null values or keys. * </p> * <p> * <strong>Usage:</strong> typical use case is as a drop-in replacement @@ -152,6 +154,14 @@ [EMAIL PROTECTED] Hashtable */ public Object put(Object key, Object value) { + // check for nulls, ensuring symantics match superclass + if (key == null) { + throw new NullPointerException("Null keys are not allowed"); + } + if (value == null) { + throw new NullPointerException("Null values are not allowed"); + } + Object result = null; Referenced lastValue = (Referenced) super.put(new Referenced(key), new Referenced(value)); if (lastValue != null) { 1.2 +18 -0 jakarta-commons/logging/optional/src/test/org/apache/commons/logging/impl/WeakHashtableTest.java Index: WeakHashtableTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/logging/optional/src/test/org/apache/commons/logging/impl/WeakHashtableTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WeakHashtableTest.java 10 Nov 2004 22:59:54 -0000 1.1 +++ WeakHashtableTest.java 11 Nov 2004 21:43:43 -0000 1.2 @@ -153,6 +153,24 @@ weakHashtable.put(anotherKey, new Long(1066)); assertEquals(new Long(1066), weakHashtable.get(anotherKey)); + + // Test compliance with the hashtable API re nulls + Exception caught = null; + try { + weakHashtable.put(null, new Object()); + } + catch (Exception e) { + caught = e; + } + assertNotNull("did not throw an exception adding a null key", caught); + caught = null; + try { + weakHashtable.put(new Object(), null); + } + catch (Exception e) { + caught = e; + } + assertNotNull("did not throw an exception adding a null value", caught); } /** Tests public void putAll(MapÊt) */
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]