You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cs...@apache.org on 2021/06/14 14:42:50 UTC

[sling-org-apache-sling-testing-osgi-mock] branch SLING-10479 created (now 75122c2)

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

cschneider pushed a change to branch SLING-10479
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git.


      at 75122c2  SLING-10479 - throw IllegalStateException if registration is already unregistered

This branch includes the following new commits:

     new 75122c2  SLING-10479 - throw IllegalStateException if registration is already unregistered

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-testing-osgi-mock] 01/01: SLING-10479 - throw IllegalStateException if registration is already unregistered

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 75122c279d87278f8c93f2280eca0b7c9e592ae8
Author: Christian Schneider <cs...@adobe.com>
AuthorDate: Mon Jun 14 16:42:35 2021 +0200

    SLING-10479 - throw IllegalStateException if registration is already unregistered
---
 .../org/apache/sling/testing/mock/osgi/MockBundleContext.java  | 10 +++++++---
 .../apache/sling/testing/mock/osgi/MockBundleContextTest.java  |  8 ++++++++
 2 files changed, 15 insertions(+), 3 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 2b17c95..3949585 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
@@ -190,9 +190,13 @@ class MockBundleContext implements BundleContext {
     }
 
     void unregisterService(MockServiceRegistration<?> registration) {
-        this.registeredServices.remove(registration);
-        handleRefsUpdateOnUnregister(registration, this);
-        notifyServiceListeners(ServiceEvent.UNREGISTERING, registration.getReference());
+        boolean wasRemoved = this.registeredServices.remove(registration);
+        if (wasRemoved) {
+            handleRefsUpdateOnUnregister(registration, this);
+            notifyServiceListeners(ServiceEvent.UNREGISTERING, registration.getReference());
+        } else {
+            throw new IllegalStateException("Service was already unregistered");
+        }
     }
 
     @SuppressWarnings("null")
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
index 293cd8c..8ca1be7 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
@@ -37,6 +37,7 @@ import java.util.Hashtable;
 import java.util.List;
 
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -233,6 +234,13 @@ public class MockBundleContextTest {
         reg1.unregister();
 
         assertNull(bundleContext.getServiceReference(clazz1));
+        
+        try {
+            reg1.unregister();
+            Assert.fail("Unregistering a non existant service should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            assertEquals("Service was already unregistered", e.getMessage());
+        }
     }