Author: markt Date: Fri Nov 28 18:27:38 2014 New Revision: 1642341 URL: http://svn.apache.org/r1642341 Log: kkolinko review Fix a typo. Simplify where values are known to be non-null Specify and test behaviour with null and non-String key values.
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/CaseInsensitiveKeyMap.java tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties tomcat/trunk/test/org/apache/tomcat/websocket/TestCaseInsensitiveKeyMap.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/CaseInsensitiveKeyMap.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/CaseInsensitiveKeyMap.java?rev=1642341&r1=1642340&r2=1642341&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/CaseInsensitiveKeyMap.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/CaseInsensitiveKeyMap.java Fri Nov 28 18:27:38 2014 @@ -24,9 +24,14 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import org.apache.tomcat.util.res.StringManager; + /** - * A Map implementation that uses case-insensitive (using {@link Locale#ENGLISH} - * strings as keys. + * A Map implementation that uses case-insensitive (using {@link + * Locale#ENGLISH}) strings as keys. + * <p> + * Keys must be instances of {@link String}. Note that this means that + * <code>null</code> keys are not permitted. * <p> * This implementation is not thread-safe. * @@ -34,6 +39,9 @@ import java.util.Set; */ public class CaseInsensitiveKeyMap<V> extends AbstractMap<String,V> { + private static final StringManager sm = + StringManager.getManager(CaseInsensitiveKeyMap.class); + private final Map<Key,V> map = new HashMap<>(); @@ -45,7 +53,11 @@ public class CaseInsensitiveKeyMap<V> ex @Override public V put(String key, V value) { - return map.put(Key.getInstance(key), value); + Key caseInsensitiveKey = Key.getInstance(key); + if (caseInsensitiveKey == null) { + throw new NullPointerException(sm.getString("caseInsensitiveKeyMap.nullKey")); + } + return map.put(caseInsensitiveKey, value); } @@ -155,9 +167,11 @@ public class CaseInsensitiveKeyMap<V> ex private static class Key { private final String key; + private final String lcKey; private Key(String key) { this.key = key; + this.lcKey = key.toLowerCase(Locale.ENGLISH); } public String getKey() { @@ -168,8 +182,7 @@ public class CaseInsensitiveKeyMap<V> ex public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + - ((key == null) ? 0 : key.toLowerCase(Locale.ENGLISH).hashCode()); + result = prime * result + lcKey.hashCode(); return result; } @@ -185,15 +198,7 @@ public class CaseInsensitiveKeyMap<V> ex return false; } Key other = (Key) obj; - if (key == null) { - if (other.key != null) { - return false; - } - } else if (!key.toLowerCase(Locale.ENGLISH).equals( - other.key.toLowerCase(Locale.ENGLISH))) { - return false; - } - return true; + return lcKey.equals(other.lcKey); } public static Key getInstance(Object o) { Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1642341&r1=1642340&r2=1642341&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties Fri Nov 28 18:27:38 2014 @@ -28,6 +28,8 @@ asyncChannelWrapperSecure.wrongStateWrit backgroundProcessManager.processFailed=A background process failed +caseInsensitiveKeyMap.nullKey=Null keys are not permitted + perMessageDeflate.deflateFailed=Failed to decompress a compressed WebSocket frame perMessageDeflate.duplicateParameter=Duplicate definition of the [{0}] extension parameter perMessageDeflate.invalidWindowSize=An invalid windows of [{1}] size was specified for [{0}]. Valid values are whole numbers from 8 to 15 inclusive. Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestCaseInsensitiveKeyMap.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestCaseInsensitiveKeyMap.java?rev=1642341&r1=1642340&r2=1642341&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/websocket/TestCaseInsensitiveKeyMap.java (original) +++ tomcat/trunk/test/org/apache/tomcat/websocket/TestCaseInsensitiveKeyMap.java Fri Nov 28 18:27:38 2014 @@ -43,6 +43,15 @@ public class TestCaseInsensitiveKeyMap { } + @Test(expected=NullPointerException.class) + public void testPutNullKey() { + Object o1 = new Object(); + + CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>(); + map.put(null, o1); + } + + @Test public void testGet() { Object o1 = new Object(); @@ -56,6 +65,17 @@ public class TestCaseInsensitiveKeyMap { @Test + public void testGetNullKey() { + Object o1 = new Object(); + + CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>(); + map.put("a", o1); + + Assert.assertNull(map.get(null)); + } + + + @Test public void testContainsKey() { Object o1 = new Object(); @@ -68,6 +88,28 @@ public class TestCaseInsensitiveKeyMap { @Test + public void testContainsKeyNonString() { + Object o1 = new Object(); + + CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>(); + map.put("a", o1); + + Assert.assertFalse(map.containsKey(o1)); + } + + + @Test + public void testContainsKeyNull() { + Object o1 = new Object(); + + CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>(); + map.put("a", o1); + + Assert.assertFalse(map.containsKey(null)); + } + + + @Test public void testContainsValue() { Object o1 = new Object(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org