You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2015/05/28 20:52:35 UTC

svn commit: r1682309 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java

Author: davidb
Date: Thu May 28 18:52:35 2015
New Revision: 1682309

URL: http://svn.apache.org/r1682309
Log:
FELIX-4883 ServiceComponentRuntime.getComponentConfigurationDTOs NullPointerException

Added defensive null check on serviceReference.getBundle().
Also added 2 unit test that focus on the code changed.

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java?rev=1682309&r1=1682308&r2=1682309&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java Thu May 28 18:52:35 2015
@@ -200,7 +200,12 @@ public class ServiceComponentRuntimeImpl
 	        return null;
 
 		ServiceReferenceDTO dto = new ServiceReferenceDTO();
-		dto.bundle = serviceRef.getBundle().getBundleId();
+		Bundle bundle = serviceRef.getBundle();
+		if (bundle != null)
+		    dto.bundle = bundle.getBundleId();
+		else
+		    dto.bundle = -1; // No bundle ever has -1 as ID, so this indicates no bundle.
+
 		dto.id = (Long) serviceRef.getProperty(Constants.SERVICE_ID);
 		dto.properties = deepCopy( serviceRef );
 		Bundle[] usingBundles = serviceRef.getUsingBundles();

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java?rev=1682309&r1=1682308&r2=1682309&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java Thu May 28 18:52:35 2015
@@ -18,18 +18,22 @@
  */
 package org.apache.felix.scr.impl.runtime;
 
-import java.lang.reflect.Array;
+import java.lang.reflect.Method;
 import java.util.Arrays;
 
+import junit.framework.TestCase;
+
+import org.mockito.Mockito;
 import org.osgi.dto.DTO;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.dto.BundleDTO;
 import org.osgi.framework.dto.ServiceReferenceDTO;
 
-import junit.framework.TestCase;
-
 public class ServiceComponentRuntimeImplTest extends TestCase
 {
- 
+
     /* in case behavior is supposed to actually involve copying
     public void testCopy()
     {
@@ -41,7 +45,7 @@ public class ServiceComponentRuntimeImpl
         equalCopy(new String[] {"1", "2", "3"}, scr);
         equalCopy(new Long[] {1l, 2l, 3l}, scr);
     }
-    
+
     private void equalCopy(Object o1, ServiceComponentRuntimeImpl scr)
     {
         Object o2 = scr.convert(o1);
@@ -51,10 +55,40 @@ public class ServiceComponentRuntimeImpl
         {
             assertEquals("expected same value at " + i, Array.get(o1, i), Array.get(o2, i));
         }
-        
+
     }
     */
-    
+
+    public void testNullBundleServiceReferenceDTO() throws Exception
+    {
+        ServiceReference<?> sr = Mockito.mock(ServiceReference.class);
+        Mockito.when(sr.getProperty(Constants.SERVICE_ID)).thenReturn(327L);
+        Mockito.when(sr.getPropertyKeys()).thenReturn(new String[] {});
+
+        ServiceComponentRuntimeImpl scr = new ServiceComponentRuntimeImpl(null, null);
+        Method m = scr.getClass().getDeclaredMethod("serviceReferenceToDTO", ServiceReference.class);
+        m.setAccessible(true);
+        ServiceReferenceDTO dto = (ServiceReferenceDTO) m.invoke(scr, sr);
+        assertEquals(-1, dto.bundle);
+    }
+
+    public void testNullBundleServiceReferenceDTO2() throws Exception
+    {
+        Bundle b = Mockito.mock(Bundle.class);
+        Mockito.when(b.getBundleId()).thenReturn(42L);
+
+        ServiceReference<?> sr = Mockito.mock(ServiceReference.class);
+        Mockito.when(sr.getProperty(Constants.SERVICE_ID)).thenReturn(327L);
+        Mockito.when(sr.getPropertyKeys()).thenReturn(new String[] {});
+        Mockito.when(sr.getBundle()).thenReturn(b);
+
+        ServiceComponentRuntimeImpl scr = new ServiceComponentRuntimeImpl(null, null);
+        Method m = scr.getClass().getDeclaredMethod("serviceReferenceToDTO", ServiceReference.class);
+        m.setAccessible(true);
+        ServiceReferenceDTO dto = (ServiceReferenceDTO) m.invoke(scr, sr);
+        assertEquals(42, dto.bundle);
+    }
+
     public void testConvert()
     {
         ServiceComponentRuntimeImpl scr = new ServiceComponentRuntimeImpl(null, null);
@@ -71,10 +105,10 @@ public class ServiceComponentRuntimeImpl
         equalsToString(Arrays.asList(new int[] {1, 2}), scr);
         equalsToString(Arrays.asList(new String[] {"foo", "bar"}), scr);
     }
-    
+
     private void equalsToString(Object o, ServiceComponentRuntimeImpl scr)
     {
-        assertEquals(String.valueOf(o), scr.convert(o));        
+        assertEquals(String.valueOf(o), scr.convert(o));
     }
 
     private void same(Object o, ServiceComponentRuntimeImpl scr)