You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/11/24 15:13:10 UTC
[sling-org-apache-sling-testing-osgi-mock] branch master updated: SLING-10930 prevent multiple deactivate() calls on the same DS component (#14)
This is an automated email from the ASF dual-hosted git repository.
kwin 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 ce4aad9 SLING-10930 prevent multiple deactivate() calls on the same DS component (#14)
ce4aad9 is described below
commit ce4aad991cba7869ad57b41bf96a6ce69df20f9b
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Nov 24 16:13:03 2021 +0100
SLING-10930 prevent multiple deactivate() calls on the same DS component (#14)
Co-authored-by: Stefan Seifert <st...@users.noreply.github.com>
---
.../sling/testing/mock/osgi/MockBundleContext.java | 8 +++-
.../mock/osgi/context/OsgiContextImplTest.java | 6 +++
.../activatedeactivate/Service8.java | 47 ++++++++++++++++++++++
3 files changed, 60 insertions(+), 1 deletion(-)
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 3d4a2fc..b4f6dd2 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
@@ -455,13 +455,19 @@ class MockBundleContext implements BundleContext {
public void shutdown() {
List<MockServiceRegistration> reversedRegisteredServices = new ArrayList<>(registeredServices);
Collections.reverse(reversedRegisteredServices);
+ Set<Object> deactivatedComponents = new HashSet<>();
for (MockServiceRegistration<?> serviceRegistration : reversedRegisteredServices) {
+ Object componentInstance = serviceRegistration.getService();
+ if (deactivatedComponents.contains(componentInstance)) {
+ continue;
+ }
try {
- MockOsgi.deactivate(serviceRegistration.getService(), this, serviceRegistration.getProperties());
+ MockOsgi.deactivate(componentInstance, this, serviceRegistration.getProperties());
}
catch (NoScrMetadataException ex) {
// ignore, no deactivate method is available then
}
+ deactivatedComponents.add(componentInstance);
}
if (dataFileBaseDir != null) {
try {
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
index b8ad838..008a54f 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
@@ -37,6 +37,7 @@ import org.apache.sling.testing.mock.osgi.testsvc.osgicontextimpl.MyService;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service3;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface1;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface2;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service8;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -195,6 +196,11 @@ public class OsgiContextImplTest {
assertNotNull(service.getConfig().get("component.id"));
}
+ @Test
+ public void testRegisterInjectActivateWithAdditionalManualServiceRegistration() {
+ context.registerInjectActivateService(new Service8());
+ }
+
@Test(expected=RuntimeException.class)
public void testRegisterInjectActivate_RefrenceMissing_Instance() {
context.registerInjectActivateService(new Service3());
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/Service8.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/Service8.java
new file mode 100644
index 0000000..499230d
--- /dev/null
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/Service8.java
@@ -0,0 +1,47 @@
+/*
+ * 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.activatedeactivate;
+
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface1;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface2;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface3;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+
+@Component(service = { ServiceInterface1.class, ServiceInterface2.class })
+public class Service8 implements ServiceInterface1, ServiceInterface2, ServiceInterface3 {
+
+ private ServiceRegistration<ServiceInterface3> serviceRegistration;
+
+ @Activate
+ private void activate(BundleContext bundleContext) {
+ serviceRegistration = bundleContext.registerService(ServiceInterface3.class, this, null);
+ }
+
+ @Deactivate
+ private void deactivate(BundleContext bundleContext) {
+ serviceRegistration.unregister();
+ // explicitly set to null to ensure deactivate is never called twice
+ serviceRegistration = null;
+ }
+
+}