extended the bundle dependency test to check filtering, added debug output to the Ensure class, fixed the BundleDependency

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@884524 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/dependencies/BundleDependency.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/dependencies/BundleDependency.java
index 6d0f72e..24edcb1 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/dependencies/BundleDependency.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/dependencies/BundleDependency.java
@@ -122,7 +122,9 @@
 		Filter filter = m_filter;
 		if (filter != null) {
 			Dictionary headers = bundle.getHeaders();
+//			System.out.println("HEADERS: " + headers);
 			if (!m_filter.match(headers)) {
+			    System.out.println("NO MATCH: " + bundle);
 				return null;
 			}
 		}
@@ -133,13 +135,12 @@
 		System.out.println("ADDED " + bundle + " " + event);
         if (makeAvailable()) {
             m_service.dependencyAvailable(this);
+            if (!isRequired()) {
+                invokeAdded(bundle);
+            }
         }
         else {
             m_service.dependencyChanged(this);
-        }
-        // try to invoke callback, if specified, but only for optional dependencies
-        // because callbacks for required dependencies are handled differently
-        if (!isRequired()) {
             invokeAdded(bundle);
         }
 	}
@@ -148,7 +149,7 @@
 		System.out.println("MODIFIED " + bundle + " " + event);
         m_service.dependencyChanged(this);
         // only invoke the changed callback if the service itself is "active"
-        if (((ServiceImpl) m_service).isRegistered()) {
+        if (m_service.isRegistered()) {
             invokeChanged(bundle);
         }
 	}
@@ -157,10 +158,12 @@
 		System.out.println("REMOVED " + bundle + " " + event);
         if (makeUnavailable()) {
             m_service.dependencyUnavailable(this);
+            if (!isRequired()) {
+                invokeRemoved(bundle);
+            }
         }
-        // try to invoke callback, if specified, but only for optional dependencies
-        // because callbacks for required dependencies are handled differently
-        if (!isRequired()) {
+        else {
+            m_service.dependencyChanged(this);
             invokeRemoved(bundle);
         }
 	}
@@ -188,20 +191,16 @@
     public void invokeAdded(Bundle serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackAdded != null)) {
-                invokeCallbackMethod(callbackInstances, m_callbackAdded, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackAdded, serviceInstance);
         }
     }
 
     public void invokeChanged(Bundle serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackChanged != null)) {
-//                if (m_reference == null) {
-//                    Thread.dumpStack();
-//                }
-                invokeCallbackMethod(callbackInstances, m_callbackChanged, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackChanged, serviceInstance);
         }
     }
-
     
     public void invokeRemoved() {
         invokeRemoved(m_bundleInstance);
@@ -210,16 +209,10 @@
     public void invokeRemoved(Bundle serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackRemoved != null)) {
-//                if (m_reference == null) {
-//                    Thread.dumpStack();
-//                }
-                invokeCallbackMethod(callbackInstances, m_callbackRemoved, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackRemoved, serviceInstance);
         }
     }
     
-
-    
-    
     private void invokeCallbackMethod(Object[] instances, String methodName, Object service) {
         for (int i = 0; i < instances.length; i++) {
             try {
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/BundleDependencyTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/BundleDependencyTest.java
index dffffa1..923d990 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/BundleDependencyTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/BundleDependencyTest.java
@@ -31,13 +31,9 @@
     @Test
     public void testBundleDependencies(BundleContext context) {
         DependencyManager m = new DependencyManager(context, new Logger(context));
-        // helper class that ensures certain steps get executed in sequence
-        Ensure e = new Ensure();
         // create a service provider and consumer
-        Consumer c = new Consumer(e);
+        Consumer c = new Consumer();
         Service consumer = m.createService().setImplementation(c).add(m.createBundleDependency().setCallbacks("add", "remove"));
-//        Service consumerWithFilter = m.createService().setImplementation(new Consumer(e)).add(m.createBundleDependency().setFilter(""));
-//        Service consumerWithStateMask = m.createService().setImplementation(new Consumer(e)).add(m.createBundleDependency().setStateMask(0));
         // add the service consumer
         m.add(consumer);
         // check if at least one bundle was found
@@ -46,16 +42,21 @@
         m.remove(consumer);
         // check if all bundles were removed correctly
         c.doubleCheck();
+        
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        Service consumerWithFilter = m.createService().setImplementation(new FilteredConsumer(e)).add(m.createBundleDependency().setFilter("(Bundle-SymbolicName=org.apache.felix.dependencymanager)").setCallbacks("add", "remove"));
+        // add a consumer with a filter
+        m.add(consumerWithFilter);
+        e.step(2);
+        // remove the consumer again
+        m.remove(consumerWithFilter);
+        e.step(4);
     }
     
     static class Consumer {
-        private final Ensure m_ensure;
         private int m_count = 0;
 
-        public Consumer(Ensure e) {
-            m_ensure = e;
-        }
-        
         public void add(Bundle b) {
             Assert.assertNotNull("bundle instance must not be null", b);
             m_count++;
@@ -73,4 +74,20 @@
             Assert.assertTrue("all bundles we found should have been removed again", m_count == 0);
         }
     }
+    
+    static class FilteredConsumer {
+        private final Ensure m_ensure;
+
+        public FilteredConsumer(Ensure e) {
+            m_ensure = e;
+        }
+        
+        public void add(Bundle b) {
+            m_ensure.step(1);
+        }
+        
+        public void remove(Bundle b) {
+            m_ensure.step(3);
+        }
+    }
 }
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java b/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java
index 73e1e2a..bdac0bd 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java
@@ -26,8 +26,16 @@
  * can also have threads wait until you arrive at a certain step.
  */
 public class Ensure {
+    private static final boolean DEBUG = true;
+    private static long INSTANCE = 0;
     private static final int RESOLUTION = 100;
-    int step = 1;
+    int step = 0;
+    
+    public Ensure() {
+        if (DEBUG) {
+            INSTANCE++;
+        }
+    }
 
     /**
      * Mark this point as step <code>nr</code>.
@@ -35,8 +43,11 @@
      * @param nr the step we are in
      */
     public synchronized void step(int nr) {
-        Assert.assertEquals(nr, step);
         step++;
+        Assert.assertEquals(nr, step);
+        if (DEBUG) {
+            System.out.println("[Ensure " + INSTANCE + "] step " + step);
+        }
         notifyAll();
     }
 
@@ -51,15 +62,21 @@
      */
     public synchronized void waitForStep(int nr, int timeout) {
         final int initialTimeout = timeout;
-        while (step <= nr && timeout > 0) {
+        if (DEBUG) {
+            System.out.println("[Ensure " + INSTANCE + "] waiting for step " + nr);
+        }
+        while (step < nr && timeout > 0) {
             try {
                 wait(RESOLUTION);
                 timeout -= RESOLUTION;
             }
             catch (InterruptedException e) {}
         }
-        if (step <= nr) {
+        if (step < nr) {
             throw new IllegalStateException("Timed out waiting for " + initialTimeout + " ms for step " + nr + ", we are still at step " + step);
         }
+        if (DEBUG) {
+            System.out.println("[Ensure " + INSTANCE + "] arrived at step " + nr);
+        }
     }
 }