incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r780768 - in /incubator/sling/trunk/bundles/api/src: main/java/org/apache/sling/api/resource/ResourceUtil.java test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Date Mon, 01 Jun 2009 18:32:58 GMT
Author: fmeschbe
Date: Mon Jun  1 18:32:58 2009
New Revision: 780768

URL: http://svn.apache.org/viewvc?rev=780768&view=rev
Log:
SLING-988 return a modifiable empty value map if the resource is
null or does not adapt to either ValueMap or Map and added more
unit tests

Modified:
    incubator/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    incubator/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java

Modified: incubator/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=780768&r1=780767&r2=780768&view=diff
==============================================================================
--- incubator/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
(original)
+++ incubator/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
Mon Jun  1 18:32:58 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.api.resource;
 
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -287,17 +288,24 @@
      */
     @SuppressWarnings("unchecked")
     public static ValueMap getValueMap(final Resource res) {
-        ValueMap map = (res == null)
-                ? ValueMap.EMPTY
-                : res.adaptTo(ValueMap.class);
-        if (map == null) {
-            Map m = res.adaptTo(Map.class);
-            if (m != null) {
-                map = new ValueMapDecorator(m);
-            } else {
-                map = ValueMap.EMPTY;
+        // adapt to ValueMap if resource is not null
+        ValueMap valueMap = (res != null)?
+            res.adaptTo(ValueMap.class) : null;
+        
+        // if no resource or no ValueMap adapter, check Map
+        if (valueMap == null) {
+            
+            Map map = (res != null) ? res.adaptTo(Map.class) : null;
+
+            // if not even adapting to map, assume an empty map
+            if (map == null) {
+                map = new HashMap<String, Object>();
             }
+            
+            // .. and decorate the plain map
+            valueMap = new ValueMapDecorator(map);
         }
-        return map;
+        
+        return valueMap;
     }
 }

Modified: incubator/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=780768&r1=780767&r2=780768&view=diff
==============================================================================
--- incubator/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
(original)
+++ incubator/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Mon Jun  1 18:32:58 2009
@@ -18,8 +18,13 @@
  */
 package org.apache.sling.api.resource;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import junit.framework.TestCase;
 
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+
 public class ResourceUtilTest extends TestCase {
 
     public void testResolveRelativeSegments() {
@@ -147,11 +152,81 @@
         assertEquals("b", ResourceUtil.getName("/b/c/.."));
         assertEquals("", ResourceUtil.getName("/b/c/../.."));
     }
+    
+    public void test_getValueMap_null_resource() {
+        final ValueMap valueMap = ResourceUtil.getValueMap(null);
+        assertNotNull(valueMap);
+        assertEquals(0, valueMap.size());
+        
+        final Object replaced = valueMap.put("sample", 1);
+        assertNull(replaced);
+        
+        assertEquals(1, valueMap.size());
+        assertEquals(1, valueMap.get("sample"));
+        assertEquals(Integer.valueOf(1), valueMap.get("sample", Integer.class));
+        assertEquals("1", valueMap.get("sample", String.class));
+    }
+    
+    public void test_getValueMap_direct() {
+        final ValueMap valueMap = new ValueMapDecorator(new HashMap<String, Object>());
+        valueMap.put("sample", true);
+        final Resource resource = new SyntheticResource(null, "/", "sample") {
+            @Override
+            @SuppressWarnings("unchecked")
+            public <Type> Type adaptTo(Class<Type> type) {
+                if (type == ValueMap.class) {
+                    return (Type) valueMap;
+                }
+                
+                return super.adaptTo(type);
+            }
+        };
+        
+        final ValueMap adapted = ResourceUtil.getValueMap(resource);
+        assertEquals(valueMap, adapted);
+        assertNotNull(adapted);
+        assertEquals(1, adapted.size());
+
+        assertEquals(true, adapted.get("sample"));
+        assertEquals(Boolean.valueOf(true), adapted.get("sample", Boolean.class));
+        assertEquals(Boolean.TRUE.toString(), adapted.get("sample", String.class));
+    }
+    
+    public void test_getValueMap_decorated_map() {
+        final Map<String, Object> map = new HashMap<String, Object>();
+        map.put("sample", true);
+        final Resource resource = new SyntheticResource(null, "/", "sample") {
+            @Override
+            @SuppressWarnings("unchecked")
+            public <Type> Type adaptTo(Class<Type> type) {
+                if (type == Map.class) {
+                    return (Type) map;
+                }
+                
+                return super.adaptTo(type);
+            }
+        };
+        
+        final ValueMap adapted = ResourceUtil.getValueMap(resource);
+        assertNotNull(adapted);
+        assertEquals(1, adapted.size());
+
+        assertEquals(true, adapted.get("sample"));
+        assertEquals(Boolean.valueOf(true), adapted.get("sample", Boolean.class));
+        assertEquals(Boolean.TRUE.toString(), adapted.get("sample", String.class));
+    }
 
-    public void testGetValueMap() {
-        // expect an empty ValueMap 
-        ValueMap valueMap = ResourceUtil.getValueMap(null);
+    public void test_getValueMap_no_adapter() {
+        final ValueMap valueMap = ResourceUtil.getValueMap(null);
         assertNotNull(valueMap);
         assertEquals(0, valueMap.size());
+        
+        final Object replaced = valueMap.put("sample", 1);
+        assertNull(replaced);
+        
+        assertEquals(1, valueMap.size());
+        assertEquals(1, valueMap.get("sample"));
+        assertEquals(Integer.valueOf(1), valueMap.get("sample", Integer.class));
+        assertEquals("1", valueMap.get("sample", String.class));
     }
 }



Mime
View raw message