pax exam 3.0.0 migration ...
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1532119 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test2/pom.xml b/dependencymanager/test2/pom.xml
index d410b2a..ac07fc2 100644
--- a/dependencymanager/test2/pom.xml
+++ b/dependencymanager/test2/pom.xml
@@ -205,7 +205,7 @@
you can specify a given test below: for example:
<include>**/integration/**/AspectBaseTest.java</include>
-->
- <include>**/integration/**/*.java</include>
+ <include>**/integration/**/ResourceAdapterTest.java</include>
</includes>
</configuration>
</execution>
diff --git a/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAdapterServiceTestWithPublisher.java b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAdapterServiceTestWithPublisher.java
index c62af10..58a35dc 100644
--- a/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAdapterServiceTestWithPublisher.java
+++ b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAdapterServiceTestWithPublisher.java
@@ -81,6 +81,9 @@
@Component
public static class ResourceProvider {
+ @ServiceDependency(filter = "(name=" + ENSURE + ")")
+ volatile Ensure m_sequencer;
+
@Inject
private volatile BundleContext m_context;
private final Map m_handlers = new HashMap();
diff --git a/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAnnotation.java b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAnnotation.java
new file mode 100644
index 0000000..de69562
--- /dev/null
+++ b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ResourceAnnotation.java
@@ -0,0 +1,287 @@
+/*
+* 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.dependencymanager.test2.components;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.ResourceHandler;
+import org.apache.felix.dm.ResourceUtil;
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.Inject;
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.ResourceAdapterService;
+import org.apache.felix.dm.annotation.api.ResourceDependency;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.annotation.api.Stop;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+
+public class ResourceAnnotation {
+ public final static String ENSURE_RESOURCE = "ResourceAnnotation.resource";
+ public final static String ENSURE_FIELD = "ResourceAnnotation.field";
+ public final static String ENSURE_ADAPTER = "ResourceAnnotation.adapter";
+ public final static String ENSURE_PROVIDER = "ResourceAnnotation.provider";
+
+ /**
+ * A Service provided the ServiceProvider, which is a ResourceAdapter.
+ */
+ public interface ServiceInterface extends Runnable {
+ }
+
+ /**
+ * A Component which has a resource dependency.
+ */
+ @Component
+ public static class ResourceConsumer {
+ @ServiceDependency(required = true, filter = "(name=" + ENSURE_RESOURCE + ")")
+ volatile Ensure m_sequencer;
+
+ private volatile int m_resourcesSeen;
+
+ @Start
+ void start() {
+ System.out.println("ResourceConsumer.start: sequencer=" + m_sequencer);
+ }
+
+ @ResourceDependency(required = false, filter = "(&(path=/path/to/*.txt)(host=localhost))")
+ public void add(URL resource) {
+ System.out.println("ResourceConsumer.add: resource=" + resource + ", m_sequencer=" + m_sequencer);
+ if (match(resource, "file://localhost/path/to/test1.txt")) {
+ m_resourcesSeen++;
+ return;
+ }
+
+ if (match(resource, "file://localhost/path/to/test2.txt")) {
+ m_resourcesSeen++;
+ return;
+ }
+
+ Assert.fail("Got unexpected resource: " + resource);
+ }
+
+ private boolean match(URL resource, String url) {
+ return url.equals(resource.toString());
+ }
+
+ @Stop
+ void stop() {
+ System.out.println("ResourceConsumer.stop: m_sequencer=" + m_sequencer);
+ Assert.assertEquals(2, m_resourcesSeen);
+ m_sequencer.step(1);
+ }
+ }
+
+ /**
+ * A Component which as a resource dependency, using a class field.
+ */
+ @Component
+ public static class ResourceConsumerField {
+ @ServiceDependency(required = true, filter = "(name=" + ENSURE_FIELD + ")")
+ volatile Ensure m_sequencer;
+
+ @ResourceDependency(filter = "(&(path=*/test1.txt)(host=localhost))")
+ URL m_resource;
+
+ @Init
+ void init() {
+ if (m_resource != null) {
+ Assert.assertTrue("file://localhost/path/to/test1.txt".equals(m_resource.toString()));
+ m_sequencer.step(1);
+ }
+ }
+ }
+
+ /**
+ * Provides some simple resources.
+ */
+ @Component
+ public static class ResourceProvider {
+ @ServiceDependency(required = true, filter = "(name=" + ENSURE_PROVIDER + ")")
+ volatile Ensure m_sequencer;
+
+ @Inject
+ private volatile BundleContext m_context;
+ private final Map m_handlers = new HashMap();
+ private final URL[] m_resources;
+
+ public ResourceProvider() throws Exception {
+ m_resources = new URL[]{
+ new URL("file://localhost/path/to/test1.txt"),
+ new URL("file://localhost/path/to/test2.txt"),
+ new URL("file://localhost/path/to/README.doc")};
+ }
+
+ /**
+ * Handles a new Resource consumer
+ * @param serviceProperties
+ * @param handler
+ */
+ @ServiceDependency(removed = "remove", required = false)
+ public void add(Map serviceProperties, ResourceHandler handler) {
+ System.out.println("ResourceProvider.addResourceHandler " + handler);
+ String filterString = (String) serviceProperties.get("filter");
+ Filter filter = null;
+ if (filterString != null) {
+ try {
+ filter = m_context.createFilter(filterString);
+ } catch (InvalidSyntaxException e) {
+ Assert.fail("Could not create filter for resource handler: " + e);
+ return;
+ }
+ }
+ synchronized (m_handlers) {
+ m_handlers.put(handler, filter);
+ }
+ for (int i = 0; i < m_resources.length; i++) {
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i]))) {
+ System.out.println("ResourceProvider: calling handled.added(" + m_resources[i] + ")");
+ handler.added(m_resources[i], null);
+ }
+ }
+ }
+
+ /**
+ * Remove a Resource consumer.
+ * @param handler
+ */
+ public void remove(ResourceHandler handler) {
+ System.out.println("ResourceProvider.removeResourceHandler " + handler);
+
+ Filter filter;
+ synchronized (m_handlers) {
+ filter = (Filter) m_handlers.remove(handler);
+ }
+ removeResources(handler, filter);
+ }
+
+ private void removeResources(ResourceHandler handler, Filter filter) {
+ for (int i = 0; i < m_resources.length; i++) {
+ if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i]))) {
+ handler.removed(m_resources[i], null);
+ }
+ }
+ }
+
+ /**
+ * Our component is being destroyed: notify all our registered Resource consumers that we don't
+ * provide our Resources anymore.
+ */
+ @Destroy
+ public void destroy() {
+ Entry[] handlers;
+ synchronized (m_handlers) {
+ handlers = (Entry[]) m_handlers.entrySet().toArray(new Entry[m_handlers.size()]);
+ }
+ for (int i = 0; i < handlers.length; i++) {
+ removeResources((ResourceHandler) handlers[i].getKey(), (Filter) handlers[i].getValue());
+ }
+ }
+ }
+
+ /**
+ * Our ServiceInterface provider, which service is activated by a ResourceAdapter.
+ */
+ @ResourceAdapterService(filter = "(&(path=/path/to/test1.txt)(host=localhost))", properties = {@Property(name = "foo", value = "bar")}, propagate = true)
+ public static class ServiceProvider implements ServiceInterface {
+ // Injected by reflection
+ URL m_resource;
+
+ @ServiceDependency(filter = "(name=" + ENSURE_ADAPTER + ")")
+ Ensure m_sequencer;
+
+ // Check auto config injections
+ @Inject
+ BundleContext m_bc;
+ BundleContext m_bcNotInjected;
+
+ @Inject
+ DependencyManager m_dm;
+ DependencyManager m_dmNotInjected;
+
+ @Inject
+ org.apache.felix.dm.Component m_component;
+ org.apache.felix.dm.Component m_componentNotInjected;
+
+ public void run() {
+ checkInjectedFields();
+ Assert.assertNotNull("Resource has not been injected in the adapter", m_resource);
+ Assert.assertEquals("ServiceProvider did not get expected resource", "file://localhost/path/to/test1.txt",
+ m_resource.toString());
+ m_sequencer.step(2);
+ }
+
+ private void checkInjectedFields() {
+ if (m_bc == null) {
+ m_sequencer.throwable(new Exception("Bundle Context not injected"));
+ return;
+ }
+ if (m_bcNotInjected != null) {
+ m_sequencer.throwable(new Exception("Bundle Context must not be injected"));
+ return;
+ }
+
+ if (m_dm == null) {
+ m_sequencer.throwable(new Exception("DependencyManager not injected"));
+ return;
+ }
+ if (m_dmNotInjected != null) {
+ m_sequencer.throwable(new Exception("DependencyManager must not be injected"));
+ return;
+ }
+
+ if (m_component == null) {
+ m_sequencer.throwable(new Exception("Component not injected"));
+ return;
+ }
+ if (m_componentNotInjected != null) {
+ m_sequencer.throwable(new Exception("Component must not be injected"));
+ return;
+ }
+ }
+ }
+
+ /**
+ * A Component with a dependency over the ServiceInterface, which is actually provided
+ * by a ResourceAdapter.
+ */
+ @Component
+ public static class ServiceConsumer {
+ @ServiceDependency
+ ServiceInterface m_serviceInterface;
+
+ @ServiceDependency(filter = "(name=" + ENSURE_ADAPTER + ")")
+ Ensure m_sequencer;
+
+ @Start
+ void start() {
+ m_sequencer.step(1);
+ m_serviceInterface.run();
+ }
+ }
+}
diff --git a/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ServiceFactoryAnnotation.java b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ServiceFactoryAnnotation.java
new file mode 100644
index 0000000..cbd2bc2
--- /dev/null
+++ b/dependencymanager/test2/src/main/java/org/apache/felix/dependencymanager/test2/components/ServiceFactoryAnnotation.java
@@ -0,0 +1,176 @@
+/*
+* 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.dependencymanager.test2.components;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.annotation.api.Stop;
+
+public class ServiceFactoryAnnotation {
+ public final static String FACTORY = "ServiceFactoryAnnotation.Factory";
+ public final static String ENSURE = "ServiceFactoryAnnotation.Ensure";
+
+ public interface MyServiceInterface {
+ public void added(String instanceId);
+
+ public void changed(String modified);
+
+ public void removed();
+ }
+
+ @Component(properties = @Property(name = "foo", value = "bar"))
+ public static class ExtraDependency1 implements Runnable {
+ public void run() {
+ }
+ }
+
+ @Component(properties = @Property(name = "foo", value = "bar2"))
+ public static class ExtraDependency2 implements Runnable {
+ public void run() {
+ System.out.println("ExtraDependency2.run()");
+ }
+ }
+
+ /**
+ * This service is instantiated using a "factory set" from the
+ * ServiceFactoryAnnotationTest class.
+ *
+ * @see org.apache.felix.dm.test.annotation.ServiceFactoryAnnotationTest
+ */
+ @Component(factorySet = FACTORY, factoryConfigure = "configure", properties = {@Property(name = "foo", value = "bar")})
+ public static class MyService implements MyServiceInterface {
+ /**
+ * The configuration provided by MyServiceFactory
+ */
+ @SuppressWarnings("unchecked")
+ volatile Dictionary m_configuration;
+
+ /**
+ * Our sequencer.
+ */
+ @ServiceDependency(filter = "(name=" + ENSURE + ")")
+ volatile Ensure m_sequencer;
+
+ /**
+ * An extra dependency (we'll dynamically configure the filter from our
+ * init() method).
+ */
+ @ServiceDependency(name = "extra")
+ Runnable m_extra;
+
+ /**
+ * This is the first method called: we are provided with the
+ * MyServiceFactory configuration.
+ */
+ public void configure(Dictionary<?, ?> configuration) {
+ if (m_configuration == null) {
+ m_configuration = configuration;
+ } else {
+ m_sequencer.step(5);
+ }
+ }
+
+ /**
+ * Initialize our Service: we'll dynamically configure our dependency whose
+ * name is "extra".
+ */
+ @Init
+ Map init() {
+ return new HashMap() {
+ {
+ put("extra.filter", "(foo=bar2)");
+ put("extra.required", "true");
+ }
+ };
+ }
+
+ /**
+ * our Service is starting: at this point, all required dependencies have
+ * been injected.
+ */
+ @Start
+ public void start() {
+ Assert.assertNotNull("Extra dependency not injected", m_extra);
+ m_extra.run();
+ m_sequencer.step(2);
+ }
+
+ /**
+ * Our service is stopping.
+ */
+ @Stop
+ public void stop() {
+ m_sequencer.step(10);
+ }
+
+ public void added(String instanceId) {
+ if (instanceId.equals(m_configuration.get("instance.id"))) {
+ m_sequencer.step(4);
+ }
+ }
+
+ public void changed(String modified) {
+ if (modified.equals(m_configuration.get("instance.modified"))) {
+ m_sequencer.step(7);
+ }
+ }
+
+ public void removed() {
+ m_sequencer.step(9);
+ }
+ }
+
+ @Component
+ public static class MyServiceClient {
+ @ServiceDependency(filter = "(name=" + ENSURE + ")")
+ volatile Ensure m_sequencer;
+
+ @Start
+ void start() {
+ m_sequencer.step(1);
+ }
+
+ @ServiceDependency(required = false, changed = "update", removed = "removed")
+ void bind(Map serviceProperties, MyServiceInterface service) {
+ m_sequencer.step(3);
+ Assert.assertEquals("bar", serviceProperties.get("foo"));
+ Assert.assertNull(serviceProperties.get(".private.param"));
+ service.added((String) serviceProperties.get("instance.id"));
+ }
+
+ void update(Map serviceProperties, MyServiceInterface service) {
+ m_sequencer.step(6);
+ service.changed((String) serviceProperties.get("instance.modified"));
+ }
+
+ void removed(MyServiceInterface service) {
+ m_sequencer.step(8);
+ service.removed();
+ }
+ }
+}
diff --git a/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ResourceAnnotationTest.java b/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ResourceAnnotationTest.java
new file mode 100644
index 0000000..e8c80d8
--- /dev/null
+++ b/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ResourceAnnotationTest.java
@@ -0,0 +1,78 @@
+/*
+* 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.dependencymanager.test2.integration.annotations;
+
+import org.apache.felix.dependencymanager.test2.components.Ensure;
+import org.apache.felix.dependencymanager.test2.components.ResourceAnnotation;
+import org.apache.felix.dependencymanager.test2.integration.common.TestBase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * Use case: Verify Bundle Dependency annotations usage.
+ */
+@RunWith(PaxExam.class)
+public class ResourceAnnotationTest extends TestBase {
+ /**
+ * Tests a simple ResourceConsumer
+ * @param context
+ */
+ @Test
+ public void testResourceAnnotation() {
+ Ensure e = new Ensure();
+ ServiceRegistration sr = register(e, ResourceAnnotation.ENSURE_RESOURCE);
+ ServiceRegistration sr2 = register(e, ResourceAnnotation.ENSURE_PROVIDER);
+ stopTestComponentsBundle();
+ e.waitForStep(1, 10000);
+ sr.unregister();
+ sr2.unregister();
+ }
+
+ /**
+ * Tests a simple ResourceConsumer using a class field for resource injection
+ */
+ @Test
+ public void testResourceAnnotationAutoConfig() {
+ Ensure e = new Ensure();
+ ServiceRegistration sr = register(e, ResourceAnnotation.ENSURE_FIELD);
+ ServiceRegistration sr2 = register(e, ResourceAnnotation.ENSURE_PROVIDER);
+ stopTestComponentsBundle();
+ e.waitForStep(1, 10000);
+ sr.unregister();
+ sr2.unregister();
+ }
+
+ /**
+ * Tests a ResourceAdapter
+ * @param context
+ */
+ @Test
+ public void testResourceAdapterAnnotation() throws Throwable {
+ Ensure e = new Ensure();
+ ServiceRegistration sr = register(e, ResourceAnnotation.ENSURE_ADAPTER);
+ ServiceRegistration sr2 = register(e, ResourceAnnotation.ENSURE_PROVIDER);
+ stopTestComponentsBundle();
+ e.waitForStep(2, 10000);
+ e.ensure();
+ sr.unregister();
+ sr2.unregister();
+ }
+}
diff --git a/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ServiceFactoryAnnotationTest.java b/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ServiceFactoryAnnotationTest.java
new file mode 100644
index 0000000..62a0ac2
--- /dev/null
+++ b/dependencymanager/test2/src/test/java/org/apache/felix/dependencymanager/test2/integration/annotations/ServiceFactoryAnnotationTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.dependencymanager.test2.integration.annotations;
+
+import java.util.Hashtable;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dependencymanager.test2.components.Ensure;
+import org.apache.felix.dependencymanager.test2.components.ServiceFactoryAnnotation;
+import org.apache.felix.dependencymanager.test2.integration.common.TestBase;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.annotation.api.Component;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.ServiceRegistration;
+
+@RunWith(PaxExam.class)
+public class ServiceFactoryAnnotationTest extends TestBase {
+ private final Ensure m_ensure = new Ensure();
+ @Test
+ public void testServiceFactory() {
+ ServiceRegistration sr = register(m_ensure, ServiceFactoryAnnotation.ENSURE);
+
+ DependencyManager m = new DependencyManager(context);
+ // Wait for the factory.
+ m.add(m.createComponent()
+ .setImplementation(this)
+ .add(m.createServiceDependency()
+ .setService(Set.class, "(" + Component.FACTORY_NAME + "=" + ServiceFactoryAnnotation.FACTORY + ")")
+ .setRequired(true).setCallbacks("bindFactory", null)));
+
+ // Check if the test.annotation components have been initialized orderly
+ m_ensure.waitForStep(10, 5000);
+ m.clear();
+ sr.unregister();
+ }
+
+ void bindFactory(Set factory) {
+ // create a service instance with this configuration
+ Hashtable conf = new Hashtable();
+ conf.put("instance.id", "instance");
+ conf.put(".private.param", "private");
+ Assert.assertTrue(factory.add(conf));
+ m_ensure.waitForStep(4, 5000);
+
+ // update the service instance
+ conf.put("instance.modified", "true");
+ Assert.assertFalse(factory.add(conf));
+ m_ensure.waitForStep(7, 5000);
+
+ // remove instance
+ Assert.assertTrue(factory.remove(conf));
+ }
+}
\ No newline at end of file