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/10/16 21:29:19 UTC

svn commit: r1023359 - /felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java

Author: pderop
Date: Sat Oct 16 19:29:19 2010
New Revision: 1023359

URL: http://svn.apache.org/viewvc?rev=1023359&view=rev
Log:
Added a new test case, which verifies that declaring extra dependencies from an init method,
and using the Component.add(List) signature works properly.

Added:
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java

Added: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java?rev=1023359&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java (added)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependenciesTest.java Sat Oct 16 19:29:19 2010
@@ -0,0 +1,233 @@
+/*
+ * 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;
+
+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 java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+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;
+
+@RunWith(JUnit4TestRunner.class)
+public class MultipleExtraDependenciesTest extends Base {
+    @Configuration
+    public static Option[] configuration() {
+        return options(
+            provision(
+                mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.1.0"),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+            )
+        );
+    }    
+    
+    /**
+     * Check that list of extra dependencies (defined from init method) are handled properly.
+     * The extra dependencies are added using a List object (Component.add(List)).
+     * A component c1 will define two extra dependencies over available c4/c5 services.
+     */
+    @Test
+    public void testWithTwoAvailableExtraDependencies(BundleContext context) {  
+        DependencyManager m = new DependencyManager(context);
+        // Helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        Component c1 = m.createComponent()
+                        .setInterface(Service1.class.getName(), null)
+                        .setImplementation(new MyComponent1(e))
+                        .add(m.createServiceDependency()
+                             .setService(Service2.class)
+                             .setRequired(true)
+                             .setAutoConfig("m_service2"));
+        
+        Component c2 = m.createComponent()
+                        .setImplementation(new MyComponent2(e))
+                        .add(m.createServiceDependency()
+                             .setService(Service1.class)
+                             .setRequired(false)
+                             .setAutoConfig(false)
+                             .setCallbacks("added", null, null));
+             
+        Component c3 = m.createComponent()
+                        .setInterface(Service2.class.getName(), null)
+                        .setImplementation(Service2Impl.class);
+        
+        Hashtable h = new Hashtable();
+        h.put("type", "xx");
+        Component c4 = m.createComponent()
+                        .setInterface(Service3.class.getName(), h)
+                        .setImplementation(Service3Impl1.class);
+
+        h = new Hashtable();
+        h.put("type", "yy");
+        Component c5 = m.createComponent()
+                        .setInterface(Service3.class.getName(), h)
+                        .setImplementation(Service3Impl2.class);
+
+
+        System.out.println("\n+++ Adding c2 / MyComponent2");
+        m.add(c2);
+        System.out.println("\n+++ Adding c3 / Service2");
+        m.add(c3);
+        System.out.println("\n+++ Adding c4 / Service3(xx)");
+        m.add(c4);
+        
+        System.out.println("\n+++ Adding c1 / MyComponent1");
+        m.add(c1);
+
+        // c1 have declared two extra dependency on Service3 (xx/yy).
+        // So, because we have not yet added c5 (yy), c1 should not be started currently.
+        // But, now, we'll add c5 (Service3/yy) and c1 should then be started ...
+        System.out.println("\n+++ Adding c5 / Service3(yy)");
+        m.add(c5);
+        e.waitForStep(3, 3000);
+    }
+
+    /**
+     * Check that list of extra dependencies (defined from init method) are handled properly.
+     * The extra dependencies are added using a List object (Component.add(List)).
+     * A component c1 will define two extra dependencies over c4/c5. At the point c1.init()
+     * is adding the two extra dependencies from its init method, c4 is available, but not c5.
+     * So, c1 is not yet activated.
+     * Then c5 is added, and it triggers the c1 activation ...
+     */
+    @Test
+    public void testWithOneAvailableExtraDependency(BundleContext context) {  
+        DependencyManager m = new DependencyManager(context);
+        // Helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        Component c1 = m.createComponent()
+                        .setInterface(Service1.class.getName(), null)
+                        .setImplementation(new MyComponent1(e))
+                        .add(m.createServiceDependency()
+                             .setService(Service2.class)
+                             .setRequired(true)
+                             .setAutoConfig("m_service2"));
+        
+        Component c2 = m.createComponent()
+                        .setImplementation(new MyComponent2(e))
+                        .add(m.createServiceDependency()
+                             .setService(Service1.class)
+                             .setRequired(false)
+                             .setAutoConfig(false)
+                             .setCallbacks("added", null, null));
+             
+        Component c3 = m.createComponent()
+                        .setInterface(Service2.class.getName(), null)
+                        .setImplementation(Service2Impl.class);
+        
+        Hashtable h = new Hashtable();
+        h.put("type", "xx");
+        Component c4 = m.createComponent()
+                        .setInterface(Service3.class.getName(), h)
+                        .setImplementation(Service3Impl1.class);
+
+        h = new Hashtable();
+        h.put("type", "yy");
+        Component c5 = m.createComponent()
+                        .setInterface(Service3.class.getName(), h)
+                        .setImplementation(Service3Impl2.class);
+
+
+        System.out.println("\n+++ Adding c2 / MyComponent2");
+        m.add(c2);
+        System.out.println("\n+++ Adding c3 / Service2");
+        m.add(c3);
+        System.out.println("\n+++ Adding c4 / Service3(xx)");
+        m.add(c4);
+        System.out.println("\n+++ Adding c5 / Service3(yy)");
+        m.add(c5);
+        System.out.println("\n+++ Adding c1 / MyComponent1");
+        // c1 have declared two extra dependency on Service3 (xx/yy).
+        // both extra dependencies are available, so the c1 component should be started immediately.
+        m.add(c1);
+        e.waitForStep(3, 3000);
+    }
+
+    public interface Service1 {}
+    public interface Service2 {}
+    public interface Service3 {}
+
+    public static class Service2Impl implements Service2 {}
+    public static class Service3Impl1 implements Service3 {}
+    public static class Service3Impl2 implements Service3 {}
+
+    public static class MyComponent1 implements Service1 {
+        Service1 m_service2;
+        Service2 m_service3_xx;
+        Service2 m_service3_yy;
+        Ensure m_ensure;
+        
+        public MyComponent1(Ensure e) {
+            m_ensure = e;
+        }
+
+        void init(Component c) {
+            m_ensure.step(1);
+            DependencyManager dm = c.getDependencyManager();
+            List l = new ArrayList();
+            // Service3/xx currently available
+            l.add(dm.createServiceDependency()
+                     .setInstanceBound(true)
+                     .setService(Service3.class, "(type=xx)")
+                     .setRequired(true)
+                     .setAutoConfig("m_service3_xx"));
+            
+            // Service3/yy not yet available
+            l.add(dm.createServiceDependency()
+                  .setInstanceBound(true)
+                  .setService(Service3.class, "(type=yy)")
+                  .setRequired(true)
+                  .setAutoConfig("m_service3_yy"));
+            c.add(l);
+        }
+        
+        void start() {
+            System.out.println("MyComponent1.start");
+            m_ensure.step(2);
+        }
+    }
+    
+    public static class MyComponent2 {
+        Service1 m_service1;
+        Ensure m_ensure;
+        
+        public MyComponent2(Ensure e) {
+            m_ensure = e;
+        }
+
+        void added(Service1 s1) {
+            System.out.println("MyComponent2.bind(" + s1 + ")");
+            m_ensure.step(3);
+        }
+        
+        void start() {
+            System.out.println("MyComponent2.start");
+        }
+    }
+}