FELIX-4883 ServiceComponentRuntime.getComponentConfigurationDTOs NullPointerException
Added defensive null check on serviceReference.getBundle().
Also added 2 unit test that focus on the code changed.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1682309 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java b/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
index 4dd05c4..3d8c140 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
@@ -200,7 +200,12 @@
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();
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java b/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
index c52521f..2a53bd1 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
@@ -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 org.osgi.dto.DTO;
-import org.osgi.framework.dto.BundleDTO;
-import org.osgi.framework.dto.ServiceReferenceDTO;
-
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;
+
public class ServiceComponentRuntimeImplTest extends TestCase
{
-
+
/* in case behavior is supposed to actually involve copying
public void testCopy()
{
@@ -41,7 +45,7 @@
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 @@
{
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 @@
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)