felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1005431 - /felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/
Date Thu, 07 Oct 2010 12:27:50 GMT
Author: marrs
Date: Thu Oct  7 12:27:50 2010
New Revision: 1005431

URL: http://svn.apache.org/viewvc?rev=1005431&view=rev
Log:
Added and clarified several tests.

Added:
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java
  (with props)
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java
  (with props)
Removed:
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterTest.java
Modified:
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterWithExtraDependenciesTest.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AspectBaseTest.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithAutoConfigTest.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FactoryConfigurationAdapterTest.java
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependencyTest2.java

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterWithExtraDependenciesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterWithExtraDependenciesTest.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterWithExtraDependenciesTest.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AdapterWithExtraDependenciesTest.java
Thu Oct  7 12:27:50 2010
@@ -1,11 +1,29 @@
+/*
+ * 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 org.apache.felix.dm.DependencyManager;
 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;

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AspectBaseTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AspectBaseTest.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AspectBaseTest.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AspectBaseTest.java
Thu Oct  7 12:27:50 2010
@@ -28,8 +28,8 @@ import java.util.Properties;
 
 import junit.framework.Assert;
 
-import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.ServiceUtil;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -118,6 +118,8 @@ public class AspectBaseTest extends Base
         // remove and add will be invoked
         e.waitForStep(4, 2000);
         
+        Assert.assertEquals("a", c.invoke());
+        
         m.remove(sp);
         // after removing the original service, the consumer's remove should be called once
         e.waitForStep(5, 2000);

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Ensure.java
Thu Oct  7 12:27:50 2010
@@ -106,4 +106,33 @@ public class Ensure {
     public static Runnable createRunnableStep(final Ensure ensure, final int nr) {
         return new Runnable() { public void run() { ensure.step(nr); }};
     }
+    
+    public synchronized void steps(Steps steps) {
+        steps.next(this);
+    }
+    
+    /** 
+     * Helper class for naming a list of step numbers. If used with the steps(Steps) method
+     * you can define at which steps in time this point should be passed. That means you
can
+     * check methods that will get invoked multiple times during a test.
+     */
+    public static class Steps {
+        private final int[] m_steps;
+        private int m_stepIndex;
+
+        /** 
+         * Create a list of steps and initialize the step counter to zero.
+         */
+        public Steps(int... steps) {
+            m_steps = steps;
+            m_stepIndex = 0;
+        }
+
+        /**
+         * Ensure we're at the right step. Will throw an index out of bounds exception if
we enter this step more often than defined.
+         */
+        public void next(Ensure ensure) {
+            ensure.step(m_steps[m_stepIndex++]);
+        }
+    }
 }

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithAutoConfigTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithAutoConfigTest.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithAutoConfigTest.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FELIX2344_ExtraDependencyWithAutoConfigTest.java
Thu Oct  7 12:27:50 2010
@@ -23,8 +23,8 @@ import static org.ops4j.pax.exam.CoreOpt
 import static org.ops4j.pax.exam.CoreOptions.provision;
 import junit.framework.Assert;
 
-import org.apache.felix.dm.DependencyManager;
 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;
@@ -88,7 +88,7 @@ public class FELIX2344_ExtraDependencyWi
     
     // This client is not using callbacks, but instead, it uses auto config.
     public static class Client {
-        ProviderInterface m_provider = null;
+        volatile ProviderInterface m_provider;
         private Ensure m_ensure;
         private final boolean m_required;
         private final int m_startStep;

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FactoryConfigurationAdapterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FactoryConfigurationAdapterTest.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FactoryConfigurationAdapterTest.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/FactoryConfigurationAdapterTest.java
Thu Oct  7 12:27:50 2010
@@ -29,8 +29,8 @@ import java.util.Properties;
 
 import junit.framework.Assert;
 
-import org.apache.felix.dm.DependencyManager;
 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;
@@ -170,7 +170,7 @@ public class FactoryConfigurationAdapter
     }
 
     public static class Adapter implements AdapterService {
-        AdapterExtraDependency m_extraDependency; // extra dependency.
+        volatile AdapterExtraDependency m_extraDependency; // extra dependency.
         private int updateCount;
         
         void updated(Dictionary settings) {

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependencyTest2.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependencyTest2.java?rev=1005431&r1=1005430&r2=1005431&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependencyTest2.java
(original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/MultipleExtraDependencyTest2.java
Thu Oct  7 12:27:50 2010
@@ -24,8 +24,8 @@ import static org.ops4j.pax.exam.CoreOpt
 
 import java.util.Hashtable;
 
-import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.ServiceDependency;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -134,36 +134,33 @@ public class MultipleExtraDependencyTest
         public void doService();
     }
     
-    public static class ServiceConsumer
-    {
+    public static class ServiceConsumer {
         volatile Sequencer m_sequencer;
         volatile ServiceInterface m_service;
         ServiceDependency m_d1, m_d2;
 
-        public void init(Object serviceInstance, DependencyManager m, Component s)
-        {
-           s.add(m_d1 = m.createServiceDependency()
-                   .setService(Sequencer.class)
-                   .setRequired(true)
-                   .setInstanceBound(true)
-                   .setAutoConfig("m_sequencer"));
-           s.add(m_d2 = m.createServiceDependency()
-                   .setService(ServiceInterface.class, "(foo=bar)")
-                   .setRequired(true)
-                   .setInstanceBound(true)
-                   .setAutoConfig("m_service"));
+        public void init(Component s) {
+            DependencyManager m = s.getDependencyManager();
+            s.add(m_d1 = m.createServiceDependency()
+                .setService(Sequencer.class)
+                .setRequired(true)
+                .setInstanceBound(true)
+                .setAutoConfig("m_sequencer"));
+            s.add(m_d2 = m.createServiceDependency()
+                .setService(ServiceInterface.class, "(foo=bar)")
+                .setRequired(true)
+                .setInstanceBound(true)
+                .setAutoConfig("m_service"));
         }
         
-        void start()
-        {
+        void start() {
             m_d1.setInstanceBound(false);
             m_d2.setInstanceBound(false);
             m_sequencer.step(6);
             m_service.doService();
         }
 
-        void stop()
-        {
+        void stop() {
             m_sequencer.step(8);
         }
     }
@@ -174,8 +171,9 @@ public class MultipleExtraDependencyTest
         ServiceProvider2 m_serviceProvider2;
         ServiceDependency m_d1, m_d2;
 
-        public void init(Object serviceInstance, DependencyManager m, Component s)
+        public void init(Component s)
         {
+            DependencyManager m = s.getDependencyManager();
             s.add(m_d1 = m.createServiceDependency()
                   .setService(Sequencer.class)
                   .setRequired(true)
@@ -224,8 +222,9 @@ public class MultipleExtraDependencyTest
         Runnable m_runnable;
         ServiceDependency m_d1, m_d2;
 
-        public void init(Object serviceInstance, DependencyManager m, Component s)
+        public void init(Component s)
         {
+            DependencyManager m = s.getDependencyManager();
             s.add(m_d1 = m.createServiceDependency()
                   .setService(Runnable.class, "(foo=bar)")
                   .setRequired(false)

Added: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java?rev=1005431&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java
(added)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java
Thu Oct  7 12:27:50 2010
@@ -0,0 +1,286 @@
+/*
+ * 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.net.MalformedURLException;
+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.Component;
+import org.apache.felix.dm.ComponentStateListener;
+import org.apache.felix.dm.Dependency;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.ResourceHandler;
+import org.apache.felix.dm.ResourceUtil;
+import org.apache.felix.dm.ServiceDependency;
+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.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+
+@RunWith(JUnit4TestRunner.class)
+public class ResourceAdapterDependencyAddAndRemoveTest 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()
+            )
+        );
+    }    
+
+    @Test
+    public void testBasicResourceAdapter(BundleContext context) throws Exception {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+
+        // create and add a service provider
+        m.add(m.createComponent()
+        		.setInterface(ServiceInterface.class.getName(), null)
+        		.setImplementation(new ServiceProvider(e)));
+        
+        // create and add a resource provider
+        ResourceProvider provider = new ResourceProvider(e);
+        m.add(m.createComponent()
+        		.setImplementation(provider)
+        		.add(m.createServiceDependency()
+        			.setService(ResourceHandler.class)
+        			.setCallbacks("add", "remove"))
+        		);
+        
+        // create a resource adapter for our single resource
+        // note that we can provide an actual implementation instance here because there
will be only one
+        // adapter, normally you'd want to specify a Class here
+        // also, create a callback instance which will be used for both callbacks on resource
changes and
+        // life cycle callbacks on the adapters themselves
+        CallbackInstance callbackInstance = new CallbackInstance(e);
+        Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))",
false, callbackInstance, "changed")
+            .setImplementation(new ResourceAdapter(e))
+            .setCallbacks(callbackInstance, "init", "start", "stop", "destroy")
+            .add(m.createServiceDependency()
+      			.setService(ServiceInterface.class)
+      			.setRequired(true)
+      			.setInstanceBound(true));
+        // add a component state listener
+        component.addStateListener(new ComponentStateListenerImpl(e));
+        // add the resource adapter
+        m.add(component);
+        // wait until the single resource is available (the adapter has been started)
+        e.waitForStep(1, 5000);
+        // trigger a 'change' in our resource
+        provider.change();
+        // wait until the changed callback is invoked
+        e.waitForStep(2, 5000);
+        // and has completed (ensuring no "extra" steps are invoked in the mean time)
+        e.waitForStep(3, 5000);
+        
+        // remove the resource adapter again
+        m.remove(component);
+        
+        // wait for the stopped callback in the state listener
+        e.waitForStep(4, 5000);
+     }
+    
+    static class ResourceAdapter {
+        protected URL m_resource; // injected by reflection.
+        private Ensure m_ensure;
+        
+        ResourceAdapter(Ensure e) {
+            m_ensure = e;
+        }
+        
+    }
+    
+    static class ResourceProvider {
+        private volatile BundleContext m_context;
+        private final Ensure m_ensure;
+        private final Map m_handlers = new HashMap();
+        private URL[] m_resources;
+
+        public ResourceProvider(Ensure ensure) throws MalformedURLException {
+            m_ensure = ensure;
+            m_resources = new URL[] {
+                new URL("file://localhost/path/to/file1.txt")
+            };
+        }
+        
+        public void change() {
+            ResourceHandler[] handlers;
+            synchronized (m_handlers) {
+                handlers = (ResourceHandler[]) m_handlers.keySet().toArray(new ResourceHandler[m_handlers.size()]);
+            }
+            for (int i = 0; i < m_resources.length; i++) {
+                for (int j = 0; j < handlers.length; j++) {
+                    ResourceHandler handler = handlers[j];
+                    handler.changed(m_resources[i]);
+                }
+            }
+        }
+
+        public void add(ServiceReference ref, ResourceHandler handler) {
+            String filterString = (String) ref.getProperty("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])))
{
+                    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 == null || filter.match(ResourceUtil.createProperties(m_resources[i])))
{
+                        handler.removed(m_resources[i]);
+                    }
+                }
+            }
+
+        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());
+            }
+            
+            System.out.println("DESTROY..." + m_handlers.size());
+        }
+    }
+    
+    static interface ServiceInterface {
+        public void invoke();
+    }
+
+    static class ServiceProvider implements ServiceInterface {
+        private final Ensure m_ensure;
+        public ServiceProvider(Ensure e) {
+            m_ensure = e;
+        }
+        public void invoke() {
+        }
+    }    
+    
+    static class CallbackInstance {
+    	
+    	private final Ensure m_ensure;
+    	
+    	public CallbackInstance(Ensure e) {
+    		m_ensure = e;
+    	}
+    	
+    	void init() {
+    		System.out.println("init");
+    	}
+    	
+    	void start() {
+    		System.out.println("start");
+    		m_ensure.step(1);
+    	}
+    	
+    	void stop() {
+    		System.out.println("stop");
+    	}
+    	
+    	void destroy() {
+    		System.out.println("destroy");
+    	}
+    	
+    	void changed(Component component) {
+    		m_ensure.step(2);
+    		Dependency oldDependency = null;
+    		
+    		for (Object dependency : component.getDependencies()) {
+    			if (dependency instanceof ServiceDependency) {
+    				// remove the dependency
+    				oldDependency = (Dependency) dependency;
+    			}
+    		}
+    		
+    		// and add a new dependency
+    		component.add(component.getDependencyManager().createServiceDependency().setService(ServiceInterface.class).setRequired(true).setInstanceBound(true));
+    		// remove the old dependency
+    		component.remove(oldDependency);
+    		System.out.println("Changed the dependencies");
+            m_ensure.step(3);
+    	}
+    }
+    
+    static class ComponentStateListenerImpl implements ComponentStateListener {
+    	
+    	private final Ensure m_ensure;
+    	
+    	public ComponentStateListenerImpl(Ensure e) {
+    		this.m_ensure = e;
+    	}
+
+		public void started(Component c) {
+			System.out.println("started");
+		}
+
+		public void starting(Component c) {
+			System.out.println("starting");
+		}
+
+		public void stopped(Component c) {
+			System.out.println("stopped");
+			m_ensure.step(4);
+		}
+
+		public void stopping(Component c) {
+			System.out.println("stopping");
+		}
+    }
+}

Propchange: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ResourceAdapterDependencyAddAndRemoveTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java?rev=1005431&view=auto
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java
(added)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java
Thu Oct  7 12:27:50 2010
@@ -0,0 +1,153 @@
+/*
+ * 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.waitForFrameworkStartupFor;
+//import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption;
+
+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.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.ServiceDependency;
+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.util.tracker.ServiceTracker;
+
+
+@RunWith(JUnit4TestRunner.class)
+public class ServiceDependencyComponentLifeCycleTest extends Base {
+    @Configuration
+    public static Option[] configuration() {
+        return options(
+            //vmOption( "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005" ),
+            //waitForFrameworkStartupFor(Long.MAX_VALUE),
+            provision(
+                mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.1.0"),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+            )
+        );
+    }
+
+    @Test
+    public void testComponentLifeCycleWhenAddingAndRemovingDependencies(BundleContext context)
throws Exception {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a resource provider
+        
+        Component component = m.createComponent().setInterface(MyService2.class.getName(),
null).setImplementation(new MyComponent(e));
+        ServiceDependency dependency = m.createServiceDependency().setService(MyService.class).setRequired(true);
+        ServiceDependency dependency2 = m.createServiceDependency().setService(MyService.class).setRequired(true);
+        ServiceTracker st = new ServiceTracker(context, MyService2.class.getName(), null);
+        st.open();
+        Component component2 = m.createComponent().setInterface(MyService.class.getName(),
null).setImplementation(MyImpl.class);
+        
+        // add the component: it has no dependencies so it should be activated immediately
+        m.add(component);
+        Assert.assertNotNull("service should be available", st.getService());
+                
+        // add a required dependency that is not available, so the component should be deactivated
+        component.add(dependency);
+        Assert.assertNull("service should no longer be available", st.getService());
+        // remove the dependency again, so the component should be activated again
+        component.remove(dependency);
+        Assert.assertNotNull("service should be available", st.getService());
+        // make the dependency instance bound
+        dependency.setInstanceBound(true);
+        
+        // add it again, the component was already active so even though the dependency
+        // is required, the component will *NOT* go through the destroy life cycle methods
+        component.add(dependency);
+        Assert.assertNull("service should no longer be available", st.getService());
+        component.remove(dependency);
+        Assert.assertNotNull("service should be available", st.getService());
+        
+        // make the second dependency instance bound too
+        dependency2.setInstanceBound(true);
+        
+        // activate the service we depend on
+        m.add(component2);
+        
+        component.add(dependency);
+        Assert.assertNotNull("service should be available", st.getService());
+        component.add(dependency2);
+        Assert.assertNotNull("service should be available", st.getService());
+        component.remove(dependency);
+        Assert.assertNotNull("service should be available", st.getService());
+        
+        e.step(9);
+        
+        // remove the service again
+        m.remove(component2);
+        e.step(11);
+        Assert.assertNull("service should no longer be available", st.getService());
+        component.remove(dependency2);
+        Assert.assertNotNull("service should be available", st.getService());
+        m.remove(component);
+        e.step(15);
+    }
+    
+    public static class MyComponent implements MyService2 {
+        private final Ensure m_ensure;
+        private final Ensure.Steps m_initSteps = new Ensure.Steps(1, 5);
+        private final Ensure.Steps m_startSteps = new Ensure.Steps(2, 6, 8, 12);
+        private final Ensure.Steps m_stopSteps = new Ensure.Steps(3, 7, 10, 13);
+        private final Ensure.Steps m_destroySteps = new Ensure.Steps(4, 14);
+
+        public MyComponent(Ensure e) {
+            m_ensure = e;
+        }
+        
+        public void init() {
+            System.out.println("init");
+            m_ensure.steps(m_initSteps);
+        }
+        
+        public void start() {
+            System.out.println("start");
+            m_ensure.steps(m_startSteps);
+        }
+        
+        public void stop() {
+            System.out.println("stop");
+            m_ensure.steps(m_stopSteps);
+        }
+
+        public void destroy() {
+            System.out.println("destroy");
+            m_ensure.steps(m_destroySteps);
+        }
+    }
+    
+    public static interface MyService2 {
+    }
+    
+    public static interface MyService {
+    }
+    
+    public static class MyImpl implements MyService {
+    }
+}

Propchange: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ServiceDependencyComponentLifeCycleTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message