FELIX-2344 After feedback from Pierre, added yet another test scenario and fixed it by reversing the order in which callbacks were invoked and the service was started to ensure callbacks of required dependencies will be invoked before the service is started.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@949019 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
index 14008f4..1f9dcfc 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
@@ -388,14 +388,13 @@
         for (int i = 0; i < services.length; i++) {
             DependencyService ds = (DependencyService) services[i];
             if (makeAvailable) {
-                ds.dependencyAvailable(this);
-                
                 // only if this service has already been instantiated should be attempt to
                 // invoke the callback method, if not, this callback will be deferred until
                 // the instance is created (see ServiceImpl.initService())
                 if (ds.getService() != null) {
                     invokeAdded(ds, ref, service);
                 }
+                ds.dependencyAvailable(this);
             }
             else {
                 ds.dependencyChanged(this);
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithCallbackTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithCallbackTest.java
index b9af272..ab45ba4 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithCallbackTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithCallbackTest.java
@@ -53,22 +53,35 @@
         Service sp = m.createService().setInterface(MyService.class.getName(), null).setImplementation(MyServiceImpl.class);
         Service sc = m.createService().setImplementation(new MyClient(e, false, 1));
         Service sc2 = m.createService().setImplementation(new MyClient(e, true, 5));
+        Service sc3 = m.createService().setImplementation(new MyClient(e, true, 9));
+        
+        // add the provider first, then add the consumer which initially will have no dependencies
+        // but via the init() method an optional dependency with a callback method will be added
         m.add(sp);
         m.add(sc);
+        // remove the consumer again
         m.remove(sc);
         e.waitForStep(4, 5000);
+
+        // next up, add a second consumer, identical to the first, but with a required dependency
+        // with a callback method which will be added in the init() method
         m.add(sc2);
+        // remove the consumer again
         m.remove(sc2);
         e.waitForStep(8, 5000);
-     }
+
+        // now remove the provider, add a third consumer, identical to the second, and after the
+        // consumer has started, add the provider again
+        m.remove(sp);
+        m.add(sc3);
+        m.add(sp);
+        e.waitForStep(12, 5000);
+    }
     
     public interface MyService {
     }
 
     public static class MyServiceImpl implements MyService {
-        public void start() {
-            System.out.println("MyServiceImpl started");
-        }
     }
 
     public static class MyClient {