use of org.apache.felix.dm.ComponentStateListener in project felix by apache.
the class ComponentTest method createComponentStartItAddDependencyAndListenerMakeDependencyAvailableAndUnavailableImmediately.
@Test
public void createComponentStartItAddDependencyAndListenerMakeDependencyAvailableAndUnavailableImmediately() {
ComponentImpl c = new ComponentImpl();
c.setImplementation(MyComponent.class);
final SimpleServiceDependency d = new SimpleServiceDependency();
d.setRequired(true);
ComponentStateListener l = new ComponentStateListener() {
@Override
public void changed(Component c, ComponentState state) {
// make the dependency unavailable
d.remove(new EventImpl());
}
};
c.start();
c.add(d);
// we add a listener here which immediately triggers an 'external event' that
// makes the dependency unavailable again as soon as it's invoked
c.add(l);
Assert.assertEquals("Component unavailable, dependency unavailable", false, c.isAvailable());
// so even though we make the dependency available here, before our call returns it
// is made unavailable again
d.add(new EventImpl());
Assert.assertEquals("Component *still* unavailable, because the listener immediately makes the dependency unavailable", false, c.isAvailable());
c.remove(l);
Assert.assertEquals("listener removed, component still unavailable", false, c.isAvailable());
c.remove(d);
Assert.assertEquals("dependency removed, component available", true, c.isAvailable());
c.stop();
Assert.assertEquals("Component stopped, should be unavailable", false, c.isAvailable());
}
Aggregations