Search in sources :

Example 21 with ComponentImpl

use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.

the class ComponentTest method createComponentWithOptionalAndRequiredDependency.

@Test
public void createComponentWithOptionalAndRequiredDependency() {
    final Ensure e = new Ensure();
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(new Object() {

        public void add() {
            e.step();
        }

        public void remove() {
            e.step();
        }
    });
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setCallbacks("add", "remove");
    d1.setRequired(false);
    SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setCallbacks("add", "remove");
    d2.setRequired(true);
    // add the dependencies to the component
    c.add(d1);
    c.add(d2);
    // start the component
    c.start();
    Assert.assertEquals("Component started with a required and optional dependency, should not be available", false, c.isAvailable());
    // make the optional dependency available
    d1.add(new EventImpl());
    e.step(1);
    Assert.assertEquals("Component should not be available", false, c.isAvailable());
    // make the required dependency available
    d2.add(new EventImpl());
    e.step(4);
    Assert.assertEquals("Component should be available", true, c.isAvailable());
    // remove the optional dependency
    d1.remove(new EventImpl());
    e.step(6);
    Assert.assertEquals("Component should be available", true, c.isAvailable());
    // remove the required dependency
    d1.remove(new EventImpl());
    e.step(8);
    Assert.assertEquals("Component should be available", true, c.isAvailable());
    c.stop();
    c.remove(d1);
    Assert.assertEquals("Component stopped, should be unavailable again", false, c.isAvailable());
}
Also used : ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Example 22 with ComponentImpl

use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.

the class ComponentTest method testAddAvailableDependencyFromInitCallback.

@Test
public void testAddAvailableDependencyFromInitCallback() {
    final Ensure e = new Ensure();
    final SimpleServiceDependency d = new SimpleServiceDependency();
    d.setRequired(true);
    final SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setRequired(true);
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(new Object() {

        void init(Component c) {
            System.out.println("init");
            e.step(2);
            c.add(d);
            d.add(new EventImpl());
            c.add(d2);
        }

        void start() {
            System.out.println("start");
            e.step();
        }

        void stop() {
            System.out.println("stop");
            e.step();
        }

        void destroy() {
            System.out.println("destroy");
            e.step(7);
        }
    });
    e.step(1);
    c.start();
    e.step(3);
    d2.add(new EventImpl());
    e.step(5);
    d.remove(new EventImpl());
    c.stop();
    e.step(8);
}
Also used : Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Example 23 with ComponentImpl

use of org.apache.felix.dm.impl.ComponentImpl 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)

Example 24 with ComponentImpl

use of org.apache.felix.dm.impl.ComponentImpl in project felix by apache.

the class ComponentTest method createComponentStartItAndAddDependency.

@Test
public void createComponentStartItAndAddDependency() {
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(MyComponent.class);
    SimpleServiceDependency d = new SimpleServiceDependency();
    d.setRequired(true);
    c.start();
    Assert.assertEquals("should be available when started", true, c.isAvailable());
    c.add(d);
    Assert.assertEquals("dependency should not be available", false, d.isAvailable());
    Assert.assertEquals("Component should not be available", false, c.isAvailable());
    c.remove(d);
    c.stop();
}
Also used : ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Aggregations

ComponentImpl (org.apache.felix.dm.impl.ComponentImpl)24 Test (org.junit.Test)23 Component (org.apache.felix.dm.Component)7 ComponentState (org.apache.felix.dm.ComponentState)3 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)3 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 ConfigurationDependencyImpl (org.apache.felix.dm.impl.ConfigurationDependencyImpl)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 Event (org.apache.felix.dm.context.Event)1