You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2010/03/06 23:36:32 UTC

svn commit: r919865 - in /felix/trunk/dependencymanager/test: ./ src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ src/test/java/org/apache/felix/dm/test/annotation/

Author: pderop
Date: Sat Mar  6 22:36:32 2010
New Revision: 919865

URL: http://svn.apache.org/viewvc?rev=919865&view=rev
Log:
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.

Added:
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
    felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
Modified:
    felix/trunk/dependencymanager/test/pom.xml

Modified: felix/trunk/dependencymanager/test/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/pom.xml?rev=919865&r1=919864&r2=919865&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/pom.xml (original)
+++ felix/trunk/dependencymanager/test/pom.xml Sat Mar  6 22:36:32 2010
@@ -88,7 +88,6 @@
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.7</version>
-      <scope>test</scope>
     </dependency>
   </dependencies>
   <build>

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumer.java Sat Mar  6 22:36:32 2010
@@ -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);
+    }
+}

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceProvider.java Sat Mar  6 22:36:32 2010
@@ -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());
+        }
+    }
+}

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceConsumer.java Sat Mar  6 22:36:32 2010
@@ -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();
+    }
+}

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceInterface.java Sat Mar  6 22:36:32 2010
@@ -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
+{
+    
+}

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ServiceProvider.java Sat Mar  6 22:36:32 2010
@@ -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);
+    }
+}

Added: felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java (added)
+++ felix/trunk/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/StaticResource.java Sat Mar  6 22:36:32 2010
@@ -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;
+    }
+}

Added: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java?rev=919865&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java (added)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java Sat Mar  6 22:36:32 2010
@@ -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