Search in sources :

Example 6 with ComponentStateListener

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());
}
Also used : Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState) Test(org.junit.Test)

Aggregations

Component (org.apache.felix.dm.Component)6 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)6 ComponentState (org.apache.felix.dm.ComponentState)5 ComponentImpl (org.apache.felix.dm.impl.ComponentImpl)3 Ensure (org.apache.felix.dm.itest.util.Ensure)3 TestBase (org.apache.felix.dm.itest.util.TestBase)3 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 ForkJoinPool (java.util.concurrent.ForkJoinPool)2 TimeUnit (java.util.concurrent.TimeUnit)2 DependencyManager (org.apache.felix.dm.DependencyManager)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 Semaphore (java.util.concurrent.Semaphore)1 ServiceDependency (org.apache.felix.dm.ServiceDependency)1 Event (org.apache.felix.dm.context.Event)1 ConfigurationDependencyImpl (org.apache.felix.dm.impl.ConfigurationDependencyImpl)1