Author: kwin
Date: Fri Apr 24 13:08:42 2015
New Revision: 1675829

URL: http://svn.apache.org/r1675829
Log:
SLING-4178 only return those elements in an array which can be converted to the 
requested type

Added:
    
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/wrappers/ValueMapDecoratorTest.java
Modified:
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java

Modified: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java?rev=1675829&r1=1675828&r2=1675829&view=diff
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
 (original)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
 Fri Apr 24 13:08:42 2015
@@ -19,7 +19,9 @@
 package org.apache.sling.api.wrappers;
 
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -95,17 +97,27 @@ public class ValueMapDecorator implement
     private <T> T[] convertToArray(Object obj, Class<T> type) {
         if (obj.getClass().isArray()) {
             final Object[] array = (Object[]) obj;
-                       @SuppressWarnings("unchecked")
-                       final T[] result = (T[]) Array.newInstance(type, 
array.length);
+            List<Object> resultList = new ArrayList<Object>();
             for (int i = 0; i < array.length; i++) {
-                result[i] = convert(array[i], type);
+                T singleValueResult = convert(array[i], type);
+                if (singleValueResult != null) {
+                    resultList.add(singleValueResult);
+                }
             }
-            return result;
+            if (resultList.isEmpty()) {
+                return null;
+            }
+            return resultList.toArray((T[]) Array.newInstance(type, 
resultList.size()));
         } else {
             @SuppressWarnings("unchecked")
-            final T[] result = (T[]) Array.newInstance(type, 1);
-            result[0] = convert(obj, type);
-            return result;
+            final T singleValueResult = convert(obj, type);
+            // return null for type conversion errors instead of single 
element array with value null
+            if (singleValueResult == null) {
+                return null;
+            }
+            final T[] arrayResult = (T[]) Array.newInstance(type, 1);
+            arrayResult[0] = singleValueResult;
+            return arrayResult;
         }
     }
 

Added: 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/wrappers/ValueMapDecoratorTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/wrappers/ValueMapDecoratorTest.java?rev=1675829&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/wrappers/ValueMapDecoratorTest.java
 (added)
+++ 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/wrappers/ValueMapDecoratorTest.java
 Fri Apr 24 13:08:42 2015
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ValueMapDecoratorTest {
+       
+       private Map<String, Object> map;
+       private ValueMap valueMap;
+       
+       @Before
+       public void setUp() {
+               map = new HashMap<String,Object>();
+               valueMap = new ValueMapDecorator(map);
+       }
+
+       // SLING-4178
+       @Test
+       public void testIncompatibleTypeInArray() {
+               map.put("prop1", new String[] {"test", "test2"});
+               map.put("prop2", "test");
+               Assert.assertNull("Not convertible type should return null", 
valueMap.get("prop1", Integer[].class));
+               Assert.assertNull("Not convertible type should return null", 
valueMap.get("prop2", Integer[].class));
+       }
+       
+       // SLING-662
+       @Test
+       public void testGettingArraysFromSingleValueEntries() {
+               map.put("prop1", "test");
+               map.put("prop2", "1");
+               Assert.assertArrayEquals("Even though the underlying entry is 
single-value if should be enclosed in a single element array", new String[] 
{"test"}, valueMap.get("prop1", String[].class));
+               Assert.assertArrayEquals("Even though the underlying entry is 
single-value if should be enclosed in a single element array", new Integer[] 
{1}, valueMap.get("prop2", Integer[].class));
+       }
+       
+       @Test
+       public void testGettingArraysFromMultiValueEntries() {
+               map.put("prop1", new String[] {"test", "test2"});
+               map.put("prop2", new String[] {"1", "2"});
+               Assert.assertArrayEquals("Could not get values from underlying 
array", new String[] {"test", "test2"}, valueMap.get("prop1", String[].class));
+               Assert.assertArrayEquals("Conversion to Integer was not 
possible", new Integer[] {1, 2}, valueMap.get("prop2", Integer[].class));
+       }
+       
+       @Test
+       public void testGettingInvalidEntryWithDefaultValue() {
+               Assert.assertEquals(Integer.valueOf(1), valueMap.get("prop1", 
1));
+               Assert.assertEquals("test", valueMap.get("prop1", "test"));
+       }
+}


Reply via email to