cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1333658 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
Date Thu, 03 May 2012 22:18:17 GMT
Author: sergeyb
Date: Thu May  3 22:18:17 2012
New Revision: 1333658

URL: http://svn.apache.org/viewvc?rev=1333658&view=rev
Log:
Merged revisions 1333656 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes

................
  r1333656 | sergeyb | 2012-05-03 23:14:35 +0100 (Thu, 03 May 2012) | 9 lines
  
  Merged revisions 1333653 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1333653 | sergeyb | 2012-05-03 23:04:18 +0100 (Thu, 03 May 2012) | 1 line
    
    [CXF-4280] Fixing MetadataMap case-insensitive checks
  ........
................

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/branches/2.5.x-fixes:r1333656
  Merged /cxf/trunk:r1333653

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java?rev=1333658&r1=1333657&r2=1333658&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
Thu May  3 22:18:17 2012
@@ -22,10 +22,12 @@ package org.apache.cxf.jaxrs.impl;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TreeSet;
 
 import javax.ws.rs.core.MultivaluedMap;
 
@@ -96,12 +98,7 @@ public class MetadataMap<K, V> implement
         if (!caseInsensitive) {
             return m.containsKey(key);
         }
-        for (K theKey : m.keySet()) {
-            if (theKey.toString().toLowerCase().equals(key.toString().toLowerCase())) {
-                return true;
-            }
-        }
-        return false;
+        return getMatchingKey(key) != null;
     }
 
     public boolean containsValue(Object value) {
@@ -116,9 +113,14 @@ public class MetadataMap<K, V> implement
         if (!caseInsensitive) {
             return m.get(key);
         }
-        for (Map.Entry<K, List<V>> entry : m.entrySet()) {
-            if (entry.getKey().toString().toLowerCase().equals(key.toString().toLowerCase()))
{
-                return entry.getValue();
+        K realKey = getMatchingKey(key);
+        return realKey == null ? null : m.get(realKey);
+    }
+    
+    private K getMatchingKey(Object key) {
+        for (K entry : m.keySet()) {
+            if (entry.toString().equalsIgnoreCase(key.toString())) {
+                return entry;
             }
         }
         return null;
@@ -129,19 +131,37 @@ public class MetadataMap<K, V> implement
     }
 
     public Set<K> keySet() {
-        return m.keySet();
+        if (!caseInsensitive) {
+            return m.keySet();
+        } else {
+            Set<K> set = new TreeSet<K>(new KeyComparator<K>());
+            set.addAll(m.keySet());
+            return set;
+        }
     }
 
     public List<V> put(K key, List<V> value) {
-        return m.put(key, value);
+        K realKey = !caseInsensitive ? key : getMatchingKey(key);
+        return m.put(realKey == null ? key : realKey, value);
     }
 
     public void putAll(Map<? extends K, ? extends List<V>> map) {
-        m.putAll(map);
+        if (!caseInsensitive) {
+            m.putAll(map);
+        } else {
+            for (Map.Entry<? extends K, ? extends List<V>> entry : map.entrySet())
{
+                this.put(entry.getKey(), entry.getValue());
+            }
+        }
     }
 
     public List<V> remove(Object key) {
-        return m.remove(key);
+        if (caseInsensitive) {
+            K realKey = getMatchingKey(key);
+            return m.remove(realKey == null ? key : realKey);
+        } else {
+            return m.remove(key);
+        }
     }
 
     public int size() {
@@ -166,5 +186,13 @@ public class MetadataMap<K, V> implement
         return m.toString();
     }
     
-    
+    private static class KeyComparator<K> implements Comparator<K> {
+
+        public int compare(K k1, K k2) {
+            String s1 = k1.toString();
+            String s2 = k2.toString();
+            return s1.compareToIgnoreCase(s2);
+        }
+        
+    }
 }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java?rev=1333658&r1=1333657&r2=1333658&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
Thu May  3 22:18:17 2012
@@ -41,6 +41,108 @@ public class MetadataMapTest extends Ass
         List<Object> value2 = m.get("baz");
         assertEquals("Only a single value should be in the list", 1, value2.size());
         assertEquals("Value is wrong", "clazz", value2.get(0));
+        assertNull(m.get("baZ"));
+    }
+    
+    @Test
+    public void testPutSingleCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        List<Object> value1 = new ArrayList<Object>();
+        value1.add("bar");
+        value1.add("foo");
+        m.put("baz", value1);
+        
+        m.putSingle("baz", "clazz");
+        assertEquals(1, m.size());
+        
+        List<Object> value2 = m.get("baz");
+        assertEquals("Only a single value should be in the list", 1, value2.size());
+        assertEquals("Value is wrong", "clazz", value2.get(0));
+        
+        m.putSingle("Baz", "clazz2");
+        assertEquals(1, m.size());
+        value2 = m.get("baz");
+        assertEquals("Only a single value should be in the list", 1, value2.size());
+        assertEquals("Value is wrong", "clazz2", value2.get(0));
+   
+        assertTrue(m.containsKey("Baz"));
+        assertTrue(m.containsKey("baz"));
+    }
+    
+    @Test
+    public void testContainsKeyCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        m.putSingle("a", "b");
+        assertTrue(m.containsKey("a"));
+        assertTrue(m.containsKey("A"));
+    }
+    
+    @Test
+    public void testContainsKeyCaseSensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.putSingle("a", "b");
+        assertTrue(m.containsKey("a"));
+        assertFalse(m.containsKey("A"));
+    }
+    
+    
+    @Test
+    public void testKeySetCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        m.putSingle("a", "b");
+        assertTrue(m.keySet().contains("a"));
+        assertTrue(m.keySet().contains("A"));
+    }
+    
+    @Test
+    public void testKeySetCaseSensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.putSingle("a", "b");
+        assertTrue(m.keySet().contains("a"));
+        assertFalse(m.keySet().contains("A"));
+    }
+    
+    @Test
+    public void testPutAllCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        List<Object> value1 = new ArrayList<Object>();
+        value1.add("bar");
+        value1.add("foo");
+        m.put("baz", value1);
+        assertEquals(1, m.size());
+        List<Object> values = m.get("baz");
+        assertEquals(2, values.size());
+        assertEquals("bar", values.get(0));
+        assertEquals("foo", values.get(1));
+        
+        MetadataMap<String, Object> m2 = new MetadataMap<String, Object>(false,
true);
+        List<Object> value2 = new ArrayList<Object>();
+        value2.add("bar2");
+        value2.add("foo2");
+        m2.put("BaZ", value2);
+        
+        m.putAll(m2);
+        
+        assertEquals(1, m.size());
+        values = m.get("Baz");
+        assertEquals(2, values.size());
+        assertEquals("bar2", values.get(0));
+        assertEquals("foo2", values.get(1));
+    }
+    
+    @Test
+    public void testRemoveCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        List<Object> value1 = new ArrayList<Object>();
+        value1.add("bar");
+        value1.add("foo");
+        m.put("baz", value1);
+        
+        m.putSingle("baz", "clazz");
+        assertEquals(1, m.size());
+        
+        m.remove("Baz");
+        assertEquals(0, m.size());
     }
     
     @Test



Mime
View raw message