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

sseifert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 30956f8  SLING-8612 osgi-mock: Restarting service due to static 
dependency injection may lead to wrong service interface registration
30956f8 is described below

commit 30956f807efce7417d917058f9ae4fa0152c63b0
Author: sseifert <[email protected]>
AuthorDate: Mon Aug 5 18:15:09 2019 +0200

    SLING-8612 osgi-mock: Restarting service due to static dependency injection 
may lead to wrong service interface registration
---
 .../sling/testing/mock/osgi/MockBundleContext.java     |  4 +++-
 .../MockBundleContextStaticGreedyReferencesTest.java   |  9 +++++----
 .../sling/testing/mock/osgi/OsgiServiceUtilTest.java   | 18 ++++++++++++++++--
 ...che.sling.testing.mock.osgi.OsgiServiceUtilTest.xml |  4 ++--
 4 files changed, 26 insertions(+), 9 deletions(-)

diff --git 
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java 
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index a42a3c8..010c707 100644
--- 
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ 
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -214,7 +214,9 @@ class MockBundleContext implements BundleContext {
         }
         MockOsgi.injectServices(newService, this);
         MockOsgi.activate(newService, this, properties);
-        registerService(serviceClass.getName(), newService, 
MapUtil.toDictionary(properties));
+        
+        String[] serviceInterfaces = 
(String[])registration.getClasses().toArray(new 
String[registration.getClasses().size()]);
+        registerService(serviceInterfaces, newService, 
MapUtil.toDictionary(properties));
     }
 
     /**
diff --git 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
index 69b8765..117bb27 100644
--- 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
+++ 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 
 import 
org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.Service3StaticGreedy;
+import 
org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.Service3StaticGreedyImpl;
 import 
org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1;
 import 
org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1Optional;
 import 
org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface2;
@@ -71,7 +72,7 @@ public class MockBundleContextStaticGreedyReferencesTest {
         reg1a = 
bundleContext.registerService(ServiceInterface1.class.getName(), dependency1a, 
null);
         reg2a = 
bundleContext.registerService(ServiceInterface2.class.getName(), dependency2a, 
null);
         
-        Service3StaticGreedy service = new Service3StaticGreedy();
+        Service3StaticGreedy service = new Service3StaticGreedyImpl();
         MockOsgi.injectServices(service, bundleContext);
         MockOsgi.activate(service, bundleContext);
         bundleContext.registerService(Service3StaticGreedy.class.getName(), 
service, null);
@@ -148,7 +149,7 @@ public class MockBundleContextStaticGreedyReferencesTest {
     }
     
     private void assertDependency1(ServiceInterface1 instance) {
-        Service3StaticGreedy service =getService();
+        Service3StaticGreedy service = getService();
         if (instance == null) {
             assertNull(service.getReference1());
         }
@@ -158,7 +159,7 @@ public class MockBundleContextStaticGreedyReferencesTest {
     }
     
     private void assertDependency1Optional(ServiceInterface1Optional instance) 
{
-        Service3StaticGreedy service =getService();
+        Service3StaticGreedy service = getService();
         if (instance == null) {
             assertNull(service.getReference1Optional());
         }
@@ -168,7 +169,7 @@ public class MockBundleContextStaticGreedyReferencesTest {
     }
     
     private void assertDependencies2(ServiceInterface2... instances) {
-        Service3StaticGreedy service =getService();
+        Service3StaticGreedy service = getService();
         assertEquals(ImmutableSet.<ServiceInterface2>copyOf(instances), 
                 
ImmutableSet.<ServiceInterface2>copyOf(service.getReferences2()));
     }
diff --git 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
index f418cea..a962daa 100644
--- 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
+++ 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
@@ -402,9 +402,18 @@ public class OsgiServiceUtilTest {
 
     }
 
-    @Component(reference = { @Reference(name = "reference2", service = 
ServiceInterface2.class, cardinality = ReferenceCardinality.AT_LEAST_ONE,
+    public interface Service3StaticGreedy {
+        ServiceInterface1 getReference1();
+        ServiceInterface1Optional getReference1Optional();
+        List<ServiceInterface2> getReferences2();
+        List<ServiceSuperInterface3> getReferences3();
+        List<Map<String, Object>> getReference3Configs();
+    }
+
+    @Component(service= Service3StaticGreedy.class,
+            reference = { @Reference(name = "reference2", service = 
ServiceInterface2.class, cardinality = ReferenceCardinality.AT_LEAST_ONE,
             bind="bindReference2", unbind="unbindReference2") })
-    public static class Service3StaticGreedy {
+    public static class Service3StaticGreedyImpl implements 
Service3StaticGreedy {
 
         @Reference(bind="bindReference1", unbind="unbindReference1")
         private ServiceInterface1 reference1;
@@ -437,14 +446,17 @@ public class OsgiServiceUtilTest {
             this.config = newConfig;
         }
 
+        @Override
         public ServiceInterface1 getReference1() {
             return this.reference1;
         }
 
+        @Override
         public ServiceInterface1Optional getReference1Optional() {
             return this.reference1Optional;
         }
 
+        @Override
         public List<ServiceInterface2> getReferences2() {
             List<ServiceInterface2> services = new 
ArrayList<ServiceInterface2>();
             for (ServiceReference<?> serviceReference : references2) {
@@ -453,10 +465,12 @@ public class OsgiServiceUtilTest {
             return services;
         }
 
+        @Override
         public List<ServiceSuperInterface3> getReferences3() {
             return this.references3;
         }
 
+        @Override
         public List<Map<String, Object>> getReference3Configs() {
             return this.reference3Configs;
         }
diff --git 
a/core/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
 
b/core/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
index 7698b9e..b4a4266 100644
--- 
a/core/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
+++ 
b/core/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
@@ -54,8 +54,8 @@
     <reference name="reference3DynamicFiltered" 
interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3"
 cardinality="0..1" policy="dynamic" field="reference3DynamicFiltered" 
field-collection-type="service"/>
   </scr:component>
   <scr:component 
name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3StaticGreedy"
 activate="activate" deactivate="deactivate" modified="modified">
-    <implementation 
class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3StaticGreedy"/>
-    <property name="service.pid" 
value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3StaticGreedy"/>
+    <implementation 
class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3StaticGreedyImpl"/>
+    <property name="service.pid" 
value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3StaticGreedyImpl"/>
     <reference name="reference1" 
interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1"
 cardinality="1..1" policy="static" policy-option="greedy" 
bind="bindReference1" unbind="unbindReference1"/>
     <reference name="reference1Optional" 
interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1Optional"
 cardinality="0..1" policy="dynamic" policy-option="greedy" 
bind="bindReference1Optional" unbind="unbindReference1Optional"/>
     <reference name="reference2" 
interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2"
 cardinality="1..n" policy="static" policy-option="greedy" 
bind="bindReference2" unbind="unbindReference2"/>

Reply via email to