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

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 473e581  FELIX-6429: ServiceObjects should not throw IAE if the 
service is unr… (#102)
473e581 is described below

commit 473e58112f0ea9fd91dc30556e5d8fa8e2524c9b
Author: Karl Pauls <[email protected]>
AuthorDate: Thu Sep 23 22:45:06 2021 +0200

    FELIX-6429: ServiceObjects should not throw IAE if the service is unr… 
(#102)
    
    * FELIX-6429: ServiceObjects should not throw IAE if the service is 
unregistered.
---
 .../apache/felix/framework/BundleContextImpl.java  |  2 +-
 .../apache/felix/framework/ServiceObjectsTest.java | 90 ++++++++++++++++++++++
 2 files changed, 91 insertions(+), 1 deletion(-)

diff --git 
a/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java 
b/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
index 3b79f1a..5386a3f 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
@@ -559,7 +559,7 @@ class BundleContextImpl implements BundleContext
             checkValidity();
 
             // Unget the specified service.
-            if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) )
+            if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) && 
m_ref.getBundle() != null )
             {
                throw new IllegalArgumentException();
             }
diff --git 
a/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java 
b/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java
new file mode 100644
index 0000000..1abeac3
--- /dev/null
+++ b/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.felix.framework;
+
+import junit.framework.TestCase;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceObjects;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.launch.Framework;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ServiceObjectsTest extends TestCase
+{
+    public void testServiceObjects() throws Exception
+    {
+        Map params = new HashMap();
+        File cacheDir = File.createTempFile("felix-cache", ".dir");
+        cacheDir.delete();
+        cacheDir.mkdirs();
+        String cache = cacheDir.getPath();
+        params.put("felix.cache.profiledir", cache);
+        params.put("felix.cache.dir", cache);
+        params.put(Constants.FRAMEWORK_STORAGE, cache);
+        Framework f = new Felix(params);
+        f.init();
+        f.start();
+
+        try
+        {
+            BundleContext context = f.getBundleContext();
+            ServiceRegistration<Object> registration =
+                    context.registerService(Object.class, new Object(), null);
+
+            ServiceReference<Object> reference = registration.getReference();
+
+            ServiceObjects<Object> serviceObjects = 
context.getServiceObjects(reference);
+
+            Object service = serviceObjects.getService();
+
+            serviceObjects.ungetService(service);
+
+            assertEquals(service, serviceObjects.getService());
+            service = serviceObjects.getService();
+
+            registration.unregister();
+
+            serviceObjects.ungetService(service);
+        }
+        finally
+        {
+            f.stop();
+            Thread.sleep(1000);
+            deleteDir(cacheDir);
+        }
+    }
+
+    private static void deleteDir(File root) throws IOException
+    {
+        if (root.isDirectory())
+        {
+            for (File file : root.listFiles())
+            {
+                deleteDir(file);
+            }
+        }
+        assertTrue(root.delete());
+    }
+}

Reply via email to