added support for bundle dependencies plus a basic test
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@884482 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
index 4cc085e..2f24f8e 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
@@ -20,6 +20,7 @@
import java.util.List;
+import org.apache.felix.dependencymanager.dependencies.BundleDependency;
import org.apache.felix.dependencymanager.dependencies.ConfigurationDependency;
import org.apache.felix.dependencymanager.dependencies.ServiceDependency;
import org.apache.felix.dependencymanager.dependencies.TemporalServiceDependency;
@@ -128,7 +129,16 @@
* @return the configuration dependency
*/
public ConfigurationDependency createConfigurationDependency() {
- return new ConfigurationDependency(m_context, m_logger);
+ return m_manager.createConfigurationDependency();
+ }
+
+ /**
+ * Creates a new bundle dependency.
+ *
+ * @return the bundle dependency
+ */
+ public BundleDependency createBundleDependency() {
+ return m_manager.createBundleDependency();
}
/**
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyManager.java b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyManager.java
index 784519c..26a9066 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyManager.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dependencymanager/DependencyManager.java
@@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;
+import org.apache.felix.dependencymanager.dependencies.BundleDependency;
import org.apache.felix.dependencymanager.dependencies.ConfigurationDependency;
import org.apache.felix.dependencymanager.dependencies.ServiceDependency;
import org.apache.felix.dependencymanager.dependencies.TemporalServiceDependency;
@@ -104,6 +105,10 @@
return new ConfigurationDependency(m_context, m_logger);
}
+ public BundleDependency createBundleDependency() {
+ return new BundleDependency(m_context, m_logger);
+ }
+
/**
* Returns a list of services.
*
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 8d05ab6..6d0f72e 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
@@ -41,7 +41,7 @@
private final Logger m_logger;
private boolean m_isStarted;
private BundleTracker m_tracker;
- private int m_stateMask;
+ private int m_stateMask = Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE;
private boolean m_isAvailable;
private boolean m_isRequired;
private DependencyService m_service;
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
new file mode 100644
index 0000000..dffffa1
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dependencymanager/test/BundleDependencyTest.java
@@ -0,0 +1,76 @@
+package org.apache.felix.dependencymanager.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 junit.framework.Assert;
+
+import org.apache.felix.dependencymanager.DependencyManager;
+import org.apache.felix.dependencymanager.Service;
+import org.apache.felix.dependencymanager.impl.Logger;
+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.Bundle;
+import org.osgi.framework.BundleContext;
+
+@RunWith(JUnit4TestRunner.class)
+public class BundleDependencyTest {
+ @Configuration
+ public static Option[] configuration() {
+ return options(
+ provision(
+ mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.2.0"),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+ )
+ );
+ }
+
+ @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);
+ 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
+ c.check();
+ // remove the consumer again
+ m.remove(consumer);
+ // check if all bundles were removed correctly
+ c.doubleCheck();
+ }
+
+ 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++;
+ }
+
+ public void check() {
+ Assert.assertTrue("we should have found at least one bundle", m_count > 0);
+ }
+
+ public void remove(Bundle b) {
+ m_count--;
+ }
+
+ public void doubleCheck() {
+ Assert.assertTrue("all bundles we found should have been removed again", m_count == 0);
+ }
+ }
+}