You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2018/09/17 16:50:52 UTC

svn commit: r1841092 - 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: cziegeler
Date: Mon Sep 17 16:50:52 2018
New Revision: 1841092

URL: http://svn.apache.org/viewvc?rev=1841092&view=rev
Log:
FELIX-5930 : Service reference is missing in DTO

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=1841092&r1=1841091&r2=1841092&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 Mon Sep 17 16:50:52 2018
@@ -278,27 +278,20 @@ public class ServiceComponentRuntimeImpl
     {
         if (serviceRef == null)
             return null;
+        final long id = (Long) serviceRef.getProperty(Constants.SERVICE_ID);
 
-        ServiceReferenceDTO dto = new ServiceReferenceDTO();
-        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();
-        if (usingBundles != null && usingBundles.length > 0 )
+        final ServiceReferenceDTO[] dtos = serviceRef.getBundle().adapt(ServiceReferenceDTO[].class);
+        if ( dtos != null )
         {
-            long[] usingBundleIds = new long[usingBundles.length];
-            for (int i = 0; i < usingBundles.length; i++)
+            for(final ServiceReferenceDTO dto : dtos)
             {
-                usingBundleIds[i] = usingBundles[i].getBundleId();
+                if ( dto.id == id)
+                {
+                    return dto;
+                }
             }
-            dto.usingBundles = usingBundleIds;
         }
-        return dto;
+        return null;
     }
 
     /**

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=1841092&r1=1841091&r2=1841092&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 Mon Sep 17 16:50:52 2018
@@ -21,8 +21,6 @@ package org.apache.felix.scr.impl.runtim
 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;
@@ -31,48 +29,11 @@ import org.osgi.framework.ServiceReferen
 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()
-    {
-        ServiceComponentRuntimeImpl scr = new ServiceComponentRuntimeImpl(null, null);
-        assertEquals(1, scr.convert(1));
-        assertEquals("foo", scr.convert("foo"));
-        equalCopy(new int[] {1, 2, 3}, scr);
-        equalCopy(new byte[] {1, 2, 3}, scr);
-        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);
-        assertEquals("expected same length", Array.getLength(o1), Array.getLength(o2));
-        assertEquals("expceted same component type", o1.getClass().getComponentType(), o2.getClass().getComponentType());
-        for (int i = 0; i < Array.getLength(o1); i++)
-        {
-            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
+    public void testBundleServiceReferenceDTO() throws Exception
     {
         Bundle b = Mockito.mock(Bundle.class);
         Mockito.when(b.getBundleId()).thenReturn(42L);
@@ -82,11 +43,21 @@ public class ServiceComponentRuntimeImpl
         Mockito.when(sr.getPropertyKeys()).thenReturn(new String[] {});
         Mockito.when(sr.getBundle()).thenReturn(b);
 
+        ServiceReferenceDTO one = new ServiceReferenceDTO();
+        one.id = 5;
+        ServiceReferenceDTO two = new ServiceReferenceDTO();
+        two.id = 825;
+        ServiceReferenceDTO three = new ServiceReferenceDTO();
+        three.id = 19;
+        ServiceReferenceDTO real = new ServiceReferenceDTO();
+        real.id = 327;
+
+        Mockito.when(b.adapt(ServiceReferenceDTO[].class)).thenReturn(new ServiceReferenceDTO[] {one, two, real, three});
         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);
+        assertEquals(real, dto);
     }
 
     public void testConvert()