You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ma...@apache.org on 2011/03/02 16:56:20 UTC
svn commit: r1076267 -
/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java
Author: marrs
Date: Wed Mar 2 15:56:19 2011
New Revision: 1076267
URL: http://svn.apache.org/viewvc?rev=1076267&view=rev
Log:
Added a new test that reveals an issue with life cycle listeners that are dynamically added.
Modified:
felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java
Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java?rev=1076267&r1=1076266&r2=1076267&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java (original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/ComponentLifeCycleTest.java Wed Mar 2 15:56:19 2011
@@ -21,8 +21,10 @@ 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 junit.framework.Assert;
import org.apache.felix.dm.Component;
+import org.apache.felix.dm.ComponentStateListener;
import org.apache.felix.dm.DependencyManager;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,6 +32,8 @@ 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.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
@RunWith(JUnit4TestRunner.class)
public class ComponentLifeCycleTest extends Base {
@@ -59,6 +63,26 @@ public class ComponentLifeCycleTest exte
e.step(6);
}
+ static class ComponentInstance {
+ private final Ensure m_ensure;
+ public ComponentInstance(Ensure e) {
+ m_ensure = e;
+ m_ensure.step(1);
+ }
+ public void init() {
+ m_ensure.step(2);
+ }
+ public void start() {
+ m_ensure.step(3);
+ }
+ public void stop() {
+ m_ensure.step(4);
+ }
+ public void destroy() {
+ m_ensure.step(5);
+ }
+ }
+
@Test
public void testCustomComponentLifeCycleCallbacks(BundleContext context) {
DependencyManager m = new DependencyManager(context);
@@ -75,44 +99,183 @@ public class ComponentLifeCycleTest exte
// ensure we executed all steps inside the component instance
e.step(6);
}
-
- static class ComponentInstance {
+
+ static class CustomComponentInstance {
private final Ensure m_ensure;
- public ComponentInstance(Ensure e) {
+ public CustomComponentInstance(Ensure e) {
m_ensure = e;
m_ensure.step(1);
}
- public void init() {
+ public void a() {
m_ensure.step(2);
}
- public void start() {
+ public void b() {
m_ensure.step(3);
}
- public void stop() {
+ public void c() {
m_ensure.step(4);
}
- public void destroy() {
+ public void d() {
m_ensure.step(5);
}
}
+
+
+
+ @Test
+ public void testComponentStateListingLifeCycle(BundleContext context) {
+ DependencyManager m = new DependencyManager(context);
+ // helper class that ensures certain steps get executed in sequence
+ Ensure e = new Ensure();
+ // create a simple service component
+ ComponentStateListeningInstance implementation = new ComponentStateListeningInstance(e);
+ Component s = m.createComponent()
+ .setInterface(MyInterface.class.getName(), null)
+ .setImplementation(implementation);
+ // add the state listener
+ s.addStateListener(implementation);
+ // add it, and since it has no dependencies, it should be activated immediately
+ m.add(s);
+ // remove it so it gets destroyed
+ m.remove(s);
+ // remove the state listener
+ s.removeStateListener(implementation);
+ // ensure we executed all steps inside the component instance
+ e.step(10);
+ }
+
+ public static interface MyInterface {}
- static class CustomComponentInstance {
+ static class ComponentStateListeningInstance implements MyInterface, ComponentStateListener {
+ volatile ServiceRegistration m_registration;
private final Ensure m_ensure;
- public CustomComponentInstance(Ensure e) {
+
+ public ComponentStateListeningInstance(Ensure e) {
m_ensure = e;
m_ensure.step(1);
}
- public void a() {
+
+ private void debug() {
+ StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
+ System.out.println("AT: " + stackTrace[2].getClassName() + "." + stackTrace[2].getMethodName() + "():" + stackTrace[2].getLineNumber());
+ }
+
+ public void init(Component c) {
+ debug();
m_ensure.step(2);
}
- public void b() {
+
+ public void start(Component c) {
+ debug();
+ m_ensure.step(4);
+ }
+ public void stop(Component c) {
+ debug();
+ m_ensure.step(7);
+ }
+
+ public void destroy(Component c) {
+ debug();
+ m_ensure.step(9);
+ }
+
+ public void starting(Component component) {
+ debug();
m_ensure.step(3);
}
- public void c() {
+
+ public void started(Component component) {
+ debug();
+ m_ensure.step(5);
+ ServiceReference reference = m_registration.getReference();
+ Assert.assertNotNull("Service not yet registered.", reference);
+ }
+
+ public void stopping(Component component) {
+ debug();
+ m_ensure.step(6);
+ }
+
+ public void stopped(Component component) {
+ debug();
+ m_ensure.step(8);
+ }
+ }
+
+
+
+ @Test
+ public void testDynamicComponentStateListingLifeCycle(BundleContext context) {
+ DependencyManager m = new DependencyManager(context);
+ // helper class that ensures certain steps get executed in sequence
+ Ensure e = new Ensure();
+ // create a simple service component
+ Component s = m.createComponent()
+ .setInterface(MyInterface.class.getName(), null)
+ .setImplementation(new DynamicComponentStateListeningInstance(e));
+ // add it, and since it has no dependencies, it should be activated immediately
+ m.add(s);
+ // remove it so it gets destroyed
+ m.remove(s);
+ // ensure we executed all steps inside the component instance
+ e.step(10);
+ }
+
+ static class DynamicComponentStateListeningInstance implements MyInterface, ComponentStateListener {
+ volatile ServiceRegistration m_registration;
+ private final Ensure m_ensure;
+
+ public DynamicComponentStateListeningInstance(Ensure e) {
+ m_ensure = e;
+ m_ensure.step(1);
+ }
+
+ private void debug() {
+ StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
+ System.out.println("AT: " + stackTrace[2].getClassName() + "." + stackTrace[2].getMethodName() + "():" + stackTrace[2].getLineNumber());
+ }
+
+ public void init(Component c) {
+ debug();
+ m_ensure.step(2);
+ c.addStateListener(this);
+ }
+
+ public void start(Component c) {
+ debug();
m_ensure.step(4);
}
- public void d() {
+ public void stop(Component c) {
+ debug();
+ m_ensure.step(7);
+ }
+
+ public void destroy(Component c) {
+ debug();
+ m_ensure.step(9);
+ c.removeStateListener(this);
+ }
+
+ public void starting(Component component) {
+ debug();
+ m_ensure.step(3);
+ }
+
+ public void started(Component component) {
+ debug();
m_ensure.step(5);
+ ServiceReference reference = m_registration.getReference();
+ Assert.assertNotNull("Service not yet registered.", reference);
+ }
+
+ public void stopping(Component component) {
+ debug();
+ m_ensure.step(6);
+ }
+
+ public void stopped(Component component) {
+ debug();
+ m_ensure.step(8);
}
}
}