added ResourceAnnotation integration test (currently, the testResourceAdapterAnnotation fails because it sounds like there is a bug in ResourceAdapterImpl, which does not propagate the resource adapter filter.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@919865 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/pom.xml b/dependencymanager/test/pom.xml
index c7994b1..a2c2443 100644
--- a/dependencymanager/test/pom.xml
+++ b/dependencymanager/test/pom.xml
@@ -88,7 +88,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
- <scope>test</scope>
</dependency>
</dependencies>
<build>
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
new file mode 100644
index 0000000..9adf699
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
@@ -0,0 +1,69 @@
+/*
+* 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.bundle.annotation.resource;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.ResourceDependency;
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.resources.Resource;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@Service
+public class ResourceConsumer
+{
+ @ServiceDependency(filter = "(test=resource)")
+ Sequencer m_sequencer;
+
+ private int m_resourcesSeen;
+
+ @ResourceDependency(required = false, filter = "(&(path=/test)(name=*.txt)(repository=TestRepository))")
+ public void add(Resource resource)
+ {
+ if (match(resource, "test1.txt", "/test", "TestRepository"))
+ {
+ m_resourcesSeen++;
+ return;
+ }
+
+ if (match(resource, "test2.txt", "/test", "TestRepository"))
+ {
+ m_resourcesSeen++;
+ return;
+ }
+
+ Assert.fail("Got unexpected resource: " + resource.getName() + "/" + resource.getPath()
+ + "/" + resource.getRepository());
+ }
+
+ private boolean match(Resource resource, String name, String path, String repo)
+ {
+ return name.equals(resource.getName()) && path.equals(resource.getPath())
+ && repo.equals(resource.getRepository());
+ }
+
+ @Destroy
+ private void destroy()
+ {
+ Assert.assertEquals(2, m_resourcesSeen);
+ m_sequencer.step(1);
+ }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
new file mode 100644
index 0000000..1d702aa
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
@@ -0,0 +1,127 @@
+/*
+* 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.bundle.annotation.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.resources.Resource;
+import org.apache.felix.dm.resources.ResourceHandler;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+@Service
+public class ResourceProvider
+{
+ private volatile BundleContext m_context;
+ private final Map m_handlers = new HashMap();
+ private StaticResource[] m_resources = {
+ new StaticResource("test1.txt", "/test", "TestRepository")
+ {
+ public InputStream openStream() throws IOException
+ {
+ return null;
+ };
+ }, new StaticResource("test2.txt", "/test", "TestRepository")
+ {
+ public InputStream openStream() throws IOException
+ {
+ return null;
+ };
+ }, new StaticResource("README.doc", "/", "TestRepository")
+ {
+ public InputStream openStream() throws IOException
+ {
+ Assert.fail("resource should not have matched the filter");
+ return null;
+ };
+ } };
+
+ @ServiceDependency(removed = "remove", required=false)
+ public void add(ServiceReference ref, ResourceHandler handler)
+ {
+ String filterString = (String) ref.getProperty("filter");
+ Filter filter;
+ 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.match(m_resources[i].getProperties()))
+ {
+ handler.added(m_resources[i]);
+ }
+ }
+ }
+
+ public void remove(ServiceReference ref, ResourceHandler 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.match(m_resources[i].getProperties()))
+ {
+ handler.removed(m_resources[i]);
+ }
+ }
+ }
+
+ @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());
+ }
+ }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java
new file mode 100644
index 0000000..611d9fe
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java
@@ -0,0 +1,40 @@
+/*
+* 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.bundle.annotation.resource;
+
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@Service
+public class ServiceConsumer
+{
+ @ServiceDependency
+ ServiceInterface m_serviceInterface;
+
+ @ServiceDependency(filter = "(test=adapter)")
+ Sequencer m_sequencer;
+
+ @Start
+ void start() {
+ m_sequencer.step(1);
+ m_serviceInterface.run();
+ }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java
new file mode 100644
index 0000000..1d238da
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java
@@ -0,0 +1,24 @@
+/*
+* 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.bundle.annotation.resource;
+
+public interface ServiceInterface extends Runnable
+{
+
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
new file mode 100644
index 0000000..465a429
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
@@ -0,0 +1,49 @@
+/*
+* 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.bundle.annotation.resource;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.annotation.api.Param;
+import org.apache.felix.dm.annotation.api.ResourceAdapterService;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.resources.Resource;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@ResourceAdapterService(
+ filter = "(&(path=/test)(name=test1.txt)(repository=TestRepository))",
+ properties = {@Param(name="foo", value="bar")},
+ propagate = true)
+public class ServiceProvider implements ServiceInterface
+{
+ // Injected by reflection
+ Resource m_resource;
+
+ @ServiceDependency(filter="(test=adapter)")
+ Sequencer m_sequencer;
+
+ public void run()
+ {
+ Assert.assertNotNull("Resource has not been injected in the adapter", m_resource);
+ Assert.assertEquals("ServiceProvider did not get expected resource", "test1.txt", m_resource.getName());
+ Assert.assertEquals("ServiceProvider did not get expected resource", "/test", m_resource.getPath());
+ Assert.assertEquals("ServiceProvider did not get expected resource", "TestRepository", m_resource.getRepository());
+ m_sequencer.step(2);
+ }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
new file mode 100644
index 0000000..5fd9627
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
@@ -0,0 +1,70 @@
+/*
+* 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.bundle.annotation.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import org.apache.felix.dm.resources.Resource;
+
+public class StaticResource implements Resource
+{
+ private String m_id;
+ private String m_name;
+ private String m_path;
+ private String m_repository;
+
+ public StaticResource(String name, String path, String repository) {
+ m_id = repository + ":" + path + "/" + name;
+ m_name = name;
+ m_path = path;
+ m_repository = repository;
+ }
+
+ public String getID() {
+ return m_id;
+ }
+
+ public String getName() {
+ return m_name;
+ }
+
+ public String getPath() {
+ return m_path;
+ }
+
+ public String getRepository() {
+ return m_repository;
+ }
+
+ public Dictionary getProperties() {
+ return new Properties() {{
+ put(Resource.ID, getID());
+ put(Resource.NAME, getName());
+ put(Resource.PATH, getPath());
+ put(Resource.REPOSITORY, getRepository());
+ }};
+ }
+
+ public InputStream openStream() throws IOException {
+ return null;
+ }
+}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
new file mode 100644
index 0000000..e7fb948
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
@@ -0,0 +1,93 @@
+/*
+* 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.annotation;
+
+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 static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+import java.util.Properties;
+
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.test.BundleGenerator;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+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;
+import org.osgi.framework.Constants;
+
+/**
+ * Use case: Verify Bundle Dependency annotations usage.
+ */
+@RunWith(JUnit4TestRunner.class)
+public class ResourceAnnotationTest extends AnnotationBase
+{
+ @Configuration
+ public static Option[] configuration()
+ {
+ return options(
+ systemProperty("dm.log").value( "true" ),
+ provision(
+ mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.1.0"),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject(),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager.runtime").versionAsInProject()),
+ provision(
+ new BundleGenerator()
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ResourceTest")
+ .set("Export-Package", "org.apache.felix.dm.test.bundle.annotation.sequencer")
+ .set("Private-Package", "org.apache.felix.dm.test.bundle.annotation.resource")
+ .set("Import-Package", "*")
+ .set("-plugin", "org.apache.felix.dm.annotation.plugin.bnd.AnnotationPlugin")
+ .build()));
+ }
+
+ /**
+ * Tests a simple ResourceConsumer
+ * @param context
+ */
+ @Test
+ public void testResourceAnnotation(BundleContext context)
+ {
+ DependencyManager m = new DependencyManager(context);
+ Properties props = new Properties() {{ put("test", "resource"); }};
+ m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+ super.stopBundle("ResourceTest", context);
+ m_ensure.waitForStep(1, 10000);
+ }
+
+ /**
+ * Tests a ResourceAdapter
+ * @param context
+ */
+ @Test
+ public void testResourceAdapterAnnotation(BundleContext context)
+ {
+ // TODO currently, this test fails because it sounds like there is a bug in ResourceAdapterImpl, which
+ // does not propagate the resource adapter filter ...
+ DependencyManager m = new DependencyManager(context);
+ Properties props = new Properties() {{ put("test", "adapter"); }};
+ m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+ super.stopBundle("ResourceTest", context);
+ m_ensure.waitForStep(2, 10000);
+ }
+}
\ No newline at end of file