This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag 
org.apache.sling.testing.osgi-mock-2.2.2
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit c8f77a9dd43965256fd5d2d25c1d36bf51ab1d0a
Author: Stefan Seifert <[email protected]>
AuthorDate: Fri Dec 9 21:09:56 2016 +0000

    SLING-6386 osgi-mock: Avoid NPE when providing servicer properties with 
null values
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1773474 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/osgi/MapUtil.java    | 14 ++++++++++--
 .../testing/mock/osgi/context/OsgiContextImpl.java |  6 +-----
 .../mock/osgi/context/OsgiContextImplTest.java     | 25 ++++++++++++++++++++++
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java 
b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
index 11891a0..44aef63 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
@@ -46,7 +46,13 @@ public final class MapUtil {
         if (map == null) {
             return null;
         }
-        return new Hashtable<T, U>(map);
+        Hashtable<T, U> hashtable = new Hashtable<>();
+        for (Map.Entry<T, U> entry : map.entrySet()) {
+            if (entry.getKey() != null && entry.getValue() != null) {
+                hashtable.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return hashtable;
     }
 
     /**
@@ -101,7 +107,11 @@ public final class MapUtil {
         }
         final Map<String, Object> result = new HashMap<>();
         for (int i=0 ; i < args.length; i+=2) {
-            result.put(args[i].toString(), args[i+1]);
+            Object key = args[i];
+            Object value = args[i+1];
+            if (key != null && value != null) {
+                result.put(key.toString(), value);
+            }
         }
         return result;
     }
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java 
b/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
index cf23354..acb96e7 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
@@ -20,7 +20,6 @@ package org.apache.sling.testing.mock.osgi.context;
 
 import java.lang.reflect.Array;
 import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.Map;
 
 import org.apache.sling.testing.mock.osgi.MapUtil;
@@ -114,10 +113,7 @@ public class OsgiContextImpl {
      * @return Registered service instance
      */
     public final <T> T registerService(final Class<T> serviceClass, final T 
service, final Map<String, Object> properties) {
-        Dictionary<String, Object> serviceProperties = null;
-        if (properties != null) {
-            serviceProperties = new Hashtable<String, Object>(properties);
-        }
+        Dictionary<String, Object> serviceProperties = 
MapUtil.toDictionary(properties);
         bundleContext().registerService(serviceClass != null ? 
serviceClass.getName() : null, service, serviceProperties);
         return service;
     }
diff --git 
a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
 
b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
index ab67824..36fd122 100644
--- 
a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
+++ 
b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
@@ -126,6 +126,31 @@ public class OsgiContextImplTest {
         assertEquals("value3", service.getConfig().get("prop1"));
     }
 
+    @Test
+    public void testRegisterInjectActivateWithPropertiesWithNulls() {
+        context.registerService(ServiceInterface1.class, 
mock(ServiceInterface1.class));
+        context.registerService(ServiceInterface2.class, 
mock(ServiceInterface2.class));
+        OsgiServiceUtilTest.Service3 service = 
context.registerInjectActivateService(new OsgiServiceUtilTest.Service3(),
+                "prop1", "value3",
+                "prop2", null,
+                null, "value4",
+                null, null);
+        assertEquals("value3", service.getConfig().get("prop1"));
+    }
+
+    @Test
+    public void testRegisterInjectActivateWithPropertyMapNulls() {
+        context.registerService(ServiceInterface1.class, 
mock(ServiceInterface1.class));
+        context.registerService(ServiceInterface2.class, 
mock(ServiceInterface2.class));
+        Map<String,Object> props = new HashMap<>();
+        props.put("prop1", "value3");
+        props.put("prop2", null);
+        props.put(null, "value4");
+        props.put(null, null);
+        OsgiServiceUtilTest.Service3 service = 
context.registerInjectActivateService(new OsgiServiceUtilTest.Service3(), 
props);
+        assertEquals("value3", service.getConfig().get("prop1"));
+    }
+
     @Test(expected=RuntimeException.class)
     public void testRegisterInjectActivate_RefrenceMissing() {
         context.registerInjectActivateService(new 
OsgiServiceUtilTest.Service3());

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to