You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by di...@apache.org on 2021/07/15 09:19:11 UTC

[sling-org-apache-sling-testing-osgi-mock] branch master updated: handle reference target filter for static+greedy referneces on ref update

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

diru 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 cdff15c  handle reference target filter for static+greedy referneces on ref update
cdff15c is described below

commit cdff15c9087f1e440b67a5158c012e61c0a09cd0
Author: Dirk Rudolph <dr...@adobe.com>
AuthorDate: Thu Jul 15 11:17:51 2021 +0200

    handle reference target filter for static+greedy referneces on ref update
---
 .../sling/testing/mock/osgi/MockBundleContext.java | 49 +++++++++------
 .../MockBundleContextStaticGreedyCyclicTest.java   | 73 ++++++++++++++++++++++
 .../osgi/testsvc/osgiserviceutil/Service8.java     | 40 ++++++++++++
 3 files changed, 142 insertions(+), 20 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 3949585..d9f3f62 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
@@ -174,17 +174,24 @@ class MockBundleContext implements BundleContext {
         List<ReferenceInfo<?>> affectedStaticGreedyReferences = OsgiServiceUtil.getMatchingStaticGreedyReferences(registeredServices, registration);
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-                // nothing to do - reference is already set
-                break;
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                restartService(referenceInfo.getServiceRegistration());
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            // Look for a target override
+            Object o = referenceInfo.getServiceRegistration().getProperties().get(reference.getName() + ComponentConstants.REFERENCE_TARGET_SUFFIX);
+            if (o instanceof String) {
+                reference = new DynamicReference(reference,(String)o);
+            }
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                    case MANDATORY_UNARY:
+                        // nothing to do - reference is already set
+                        break;
+                    case MANDATORY_MULTIPLE:
+                    case OPTIONAL_MULTIPLE:
+                    case OPTIONAL_UNARY:
+                        restartService(referenceInfo.getServiceRegistration());
+                        break;
+                    default:
+                        throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
     }
@@ -249,15 +256,17 @@ class MockBundleContext implements BundleContext {
         List<ReferenceInfo<?>> affectedStaticGreedyReferences = OsgiServiceUtil.getMatchingStaticGreedyReferences(registeredServices, registration);
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                restartService(referenceInfo.getServiceRegistration());
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                    case MANDATORY_UNARY:
+                    case MANDATORY_MULTIPLE:
+                    case OPTIONAL_MULTIPLE:
+                    case OPTIONAL_UNARY:
+                        restartService(referenceInfo.getServiceRegistration());
+                        break;
+                    default:
+                        throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
     }
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyCyclicTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyCyclicTest.java
new file mode 100644
index 0000000..02553d2
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyCyclicTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.testing.mock.osgi;
+
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service8;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class MockBundleContextStaticGreedyCyclicTest {
+
+    private BundleContext bundleContext;
+
+    @Before
+    public void setup() {
+        bundleContext = MockOsgi.newBundleContext();
+
+        Service8 impl = new Service8();
+        bundleContext.registerService(Service8.class, impl, MapUtil.toDictionary("service.ranking", 10));
+        assertNull(impl.getDefaultImpl());
+    }
+
+    @Test
+    public void testCyclicReferenceFiltered() {
+        DefaultService8 defaultImpl1 = new DefaultService8();
+        ServiceRegistration defaultService1 = bundleContext.registerService(new String[] {
+            Service8.class.getName(),
+            DefaultService8.class.getName()
+        }, defaultImpl1, null);
+
+        Service8 impl = getService(Service8.class);
+        assertEquals(defaultImpl1, impl.getDefaultImpl());
+
+        // no default service expected
+        defaultService1.unregister();
+        impl = getService(Service8.class);
+        assertNull(impl.getDefaultImpl());
+    }
+
+    private <T> T getService(Class<T> clazz) {
+        ServiceReference<T> ref = bundleContext.getServiceReference(clazz);
+        assertNotNull(ref);
+        T srv = bundleContext.getService(ref);
+        assertNotNull(srv);
+        return srv;
+    }
+
+    private static class DefaultService8 extends Service8 {
+        // no Reference to Service8
+    }
+}
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8.java
new file mode 100644
index 0000000..e2ccb4a
--- /dev/null
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil;
+
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+
+@Component(service = Service8.class)
+public class Service8 {
+
+    @Reference(
+        cardinality = ReferenceCardinality.OPTIONAL,
+        policy = ReferencePolicy.STATIC,
+        policyOption = ReferencePolicyOption.GREEDY,
+        target = "(objectClass=*DefaultService8)")
+    private Service8 defaultImpl;
+
+    public Service8 getDefaultImpl() {
+        return defaultImpl;
+    }
+}