FELIX-2078 Added a test case that exposes the bug.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@910192 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
index deb2398..8a93edd 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
@@ -57,6 +57,17 @@
         }
         notifyAll();
     }
+    
+    /**
+     * Mark this point as the next step.
+     */
+    public synchronized void step() {
+        step++;
+        if (DEBUG) {
+            STREAM.println("[Ensure " + INSTANCE + "] next step " + step);
+        }
+        notifyAll();
+    }
 
     /**
      * Wait until we arrive at least at step <code>nr</code> in the process, or fail if that
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2078_ServiceDependencyTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2078_ServiceDependencyTest.java
new file mode 100644
index 0000000..362676a
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2078_ServiceDependencyTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.test;
+
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.service.Service;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+
+@RunWith(JUnit4TestRunner.class)
+public class FELIX2078_ServiceDependencyTest {
+    @Configuration
+    public static Option[] configuration() {
+        return options(
+            provision(
+                mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.1.0"),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+            )
+        );
+    }    
+
+    @Test
+    public void testRequiredServiceRegistrationAndConsumption(BundleContext context) {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a service provider and consumer
+        Service sp = m.createService().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
+        Service sp2 = m.createService().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
+        Service sc = m.createService().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", "remove"));
+        m.add(sp);
+        m.add(sp2);
+        m.add(sc);
+        // wait until both services have been added to our consumer
+        e.waitForStep(2, 5000);
+        m.remove(sc);
+        m.remove(sp2);
+        m.remove(sp);
+        // ensure we executed all steps inside the component instance
+    }
+    
+    @Test
+    public void testOptionalServiceRegistrationAndConsumption(BundleContext context) {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a service provider and consumer
+        Service sp = m.createService().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
+        Service sp2 = m.createService().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
+        Service sc = m.createService().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
+        m.add(sp);
+        m.add(sp2);
+        m.add(sc);
+        // wait until both services have been added to our consumer
+        e.waitForStep(2, 5000);
+        m.remove(sc);
+        m.remove(sp2);
+        m.remove(sp);
+        // ensure we executed all steps inside the component instance
+    }
+    
+    static interface ServiceInterface {
+        public void invoke();
+    }
+
+    static class ServiceProvider implements ServiceInterface {
+        private final Ensure m_ensure;
+        public ServiceProvider(Ensure e) {
+            m_ensure = e;
+        }
+        public void invoke() {
+        }
+    }
+
+    static class ServiceConsumer {
+        private final Ensure m_ensure;
+
+        public ServiceConsumer(Ensure e) {
+            m_ensure = e;
+        }
+        
+        public void add(ServiceInterface i) {
+            m_ensure.step();
+        }
+        
+        public void remove(ServiceInterface i) {
+            
+        }
+    }
+}