FELIX-5153: Added test for 5153 issue.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1722772 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/DiagnosticsTest.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/DiagnosticsTest.java
index 5a459d5..954c07d 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/DiagnosticsTest.java
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/DiagnosticsTest.java
@@ -35,10 +35,11 @@
 
 public class DiagnosticsTest extends TestBase {
 	
-	// there is one TestComponent created by org.apache.felix.dm.itest.bundle
-	// this component is always registered, so we only need to take it into 
+	// there are two components (TestComponent and HelloWorldServiceFactory) created by 
+    // org.apache.felix.dm.itest.bundle
+	// These component is always registered, so we only need to take them into 
 	// account when we build the graph with ALL components
-	private static final int InitialComponentCount = 1;
+	private static final int InitialComponentCount = 2;
 	
 	private boolean checkComponentCount(int expected, int count) {
 		return count == expected + InitialComponentCount;
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FELIX5153_StopBeforeUngetServiceTest.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FELIX5153_StopBeforeUngetServiceTest.java
new file mode 100644
index 0000000..3773a9d
--- /dev/null
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FELIX5153_StopBeforeUngetServiceTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.dm.itest.api;
+
+import org.apache.felix.dm.itest.bundle.HelloWorld;
+import org.apache.felix.dm.itest.util.TestBase;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class FELIX5153_StopBeforeUngetServiceTest extends TestBase {
+    public void testCallStopAfterUngetService() throws Throwable {
+        Bundle testBundle = null;
+        
+        try {
+    	    ServiceReference[] refs = context.getServiceReferences(HelloWorld.class.getName(), "(dm=dm4)");
+    	    HelloWorld service = (HelloWorld) context.getService(refs[0]);
+    	    System.out.println(service.sayIt("DM4"));
+
+    		for (Bundle b : context.getBundles()) {
+    			if (b.getSymbolicName().equals("org.apache.felix.dependencymanager.itest.bundle")) {
+    				testBundle = b;
+    				// Stop the test bundle. In this test bundle, we have a service factory component, and its unget method
+    				// is expected to be called *before* its stop() method.
+    				b.stop();
+    				break;
+    			}
+    		}
+    	} 
+    	
+    	catch (Throwable t) {
+    		error("test failed", t);
+    	}
+    	
+    	finally {
+    	    if (testBundle != null) {
+    	        testBundle.start(); // restart the test bundle
+    	    }
+    	}
+    }
+}
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/Activator.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/Activator.java
index a550e30..15fe6eb 100644
--- a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/Activator.java
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/Activator.java
@@ -18,6 +18,9 @@
  */
 package org.apache.felix.dm.itest.bundle;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
@@ -28,11 +31,15 @@
 public class Activator extends DependencyActivatorBase {
 
 	@Override
-	public void init(BundleContext ctx, DependencyManager m)
-			throws Exception {
+	public void init(BundleContext ctx, DependencyManager m) throws Exception {
 		m.add(createComponent()
-				.setImplementation(TestComponent.class)
-				.setInterface(TestService.class.getName(), null));
+		    .setImplementation(TestComponent.class)
+		    .setInterface(TestService.class.getName(), null));
+		
+        Dictionary<String, Object> props = new Hashtable<>();
+        props.put("dm", "dm4");
+        m.add(createComponent().setInterface(HelloWorld.class.getName(), props)
+         .setImplementation(new HelloWorldServiceFactory()));
 	}
 
 }
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorld.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorld.java
new file mode 100644
index 0000000..f31693f
--- /dev/null
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorld.java
@@ -0,0 +1,24 @@
+/*
+ * 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.dm.itest.bundle;
+
+public interface HelloWorld {
+
+	String sayIt(String msg);
+}
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldService.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldService.java
new file mode 100644
index 0000000..3c3aa6f
--- /dev/null
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldService.java
@@ -0,0 +1,27 @@
+/*
+ * 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.dm.itest.bundle;
+
+public class HelloWorldService implements HelloWorld {
+
+	@Override
+	public String sayIt(String arg) {
+		return "Hello " + arg;
+	}
+}
diff --git a/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldServiceFactory.java b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldServiceFactory.java
new file mode 100644
index 0000000..3ee6a49
--- /dev/null
+++ b/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/bundle/HelloWorldServiceFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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.dm.itest.bundle;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+public class HelloWorldServiceFactory implements ServiceFactory {
+	private final Map<ServiceRegistration, HelloWorldService> m_services = new HashMap<>();
+
+	public void stop() {
+		synchronized (m_services) {
+			if (!m_services.isEmpty()) {
+				throw new IllegalStateException("All services should be closed");
+			}
+		}
+	}
+
+	@Override
+	public HelloWorld getService(Bundle bundle, ServiceRegistration registration) {
+		HelloWorldService service = new HelloWorldService();
+		synchronized (m_services) {
+			HelloWorldService old = m_services.put(registration, service);
+			assert old == null;
+		}
+		return service;
+	}
+
+	@Override
+	public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
+		synchronized (m_services) {
+			HelloWorldService old = m_services.remove(registration);
+			HelloWorldService serv = (HelloWorldService) service;
+			assert serv == old;
+		}
+	}
+}