Search in sources :

Example 6 with ComponentImpl

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

the class ComponentTest method createComponentWithOptionalDependency.

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

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

        public void change() {
            e.step(3);
        }

        public void remove() {
            e.step(5);
        }
    });
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setCallbacks("add", "change", "remove");
    d1.setRequired(false);
    // add the dependency to the component
    c.add(d1);
    // start the component
    c.start();
    Assert.assertEquals("Component started with an optional dependency, should be available", true, c.isAvailable());
    // make the dependency available, we expect the add callback
    // to be invoked here
    d1.add(new EventImpl());
    e.step(2);
    Assert.assertEquals("Component started with an optional dependency, should be available", true, c.isAvailable());
    // change the dependency
    d1.change(new EventImpl());
    e.step(4);
    Assert.assertEquals("Component started with an optional dependency, should be available", true, c.isAvailable());
    // remove the dependency, should trigger the remove callback
    d1.remove(new EventImpl());
    Assert.assertEquals("Component started with an optional dependency, should be available", true, c.isAvailable());
    e.step(6);
    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 7 with ComponentImpl

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

the class ComponentTest method createComponentAddTwoDependenciesMakeBothAvailableAndUnavailable.

@Test
public void createComponentAddTwoDependenciesMakeBothAvailableAndUnavailable() {
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(MyComponent.class);
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setRequired(true);
    SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setRequired(true);
    c.start();
    c.add(d1);
    c.add(d2);
    Assert.assertEquals("Component should be unavailable, both dependencies are too", false, c.isAvailable());
    d1.add(new EventImpl());
    Assert.assertEquals("one dependency available, component should still be unavailable", false, c.isAvailable());
    d2.add(new EventImpl());
    Assert.assertEquals("both dependencies available, component should be available", true, c.isAvailable());
    d1.remove(new EventImpl());
    Assert.assertEquals("one dependency unavailable again, component should be unavailable too", false, c.isAvailable());
    d2.remove(new EventImpl());
    Assert.assertEquals("both dependencies unavailable, component should be too", false, c.isAvailable());
    c.remove(d2);
    Assert.assertEquals("removed one dependency, still unavailable", false, c.isAvailable());
    c.remove(d1);
    Assert.assertEquals("removed the other dependency, component should be available now", true, c.isAvailable());
    c.stop();
    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 8 with ComponentImpl

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

the class ComponentTest method createComponentAddAvailableDependencyRemoveDependencyCheckStopCalledBeforeUnbind.

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

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

        void start() {
            e.step(2);
        }

        void stop() {
            e.step(4);
        }

        void remove() {
            e.step(5);
        }
    });
    SimpleServiceDependency d = new SimpleServiceDependency();
    d.setCallbacks("add", "remove");
    d.setRequired(true);
    // add the dependency to the component
    c.add(d);
    // start the component
    c.start();
    // make the dependency available, we expect the add callback
    // to be invoked here, then start is called.
    d.add(new EventImpl());
    e.step(3);
    // remove the dependency, should trigger the stop, then remove callback
    d.remove(new EventImpl());
    e.step(6);
    c.stop();
    c.remove(d);
    Assert.assertEquals("Component stopped, should be unavailable", false, c.isAvailable());
}
Also used : ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Example 9 with ComponentImpl

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

the class ComponentTest method createStartAndStopComponent.

@Test
public void createStartAndStopComponent() {
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(MyComponent.class);
    Assert.assertEquals("should not be available until started", false, c.isAvailable());
    c.start();
    Assert.assertEquals("should be available", true, c.isAvailable());
    c.stop();
    Assert.assertEquals("should no longer be available when stopped", false, c.isAvailable());
}
Also used : ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Example 10 with ComponentImpl

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

the class ComponentTest method createAndStartComponentAddTwoDependenciesWithMultipleServicesWithCallbacks.

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

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

        public void remove() {
            e.step();
        }
    });
    // start component
    c.start();
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setCallbacks("add", "remove");
    d1.setRequired(true);
    SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setCallbacks("add", "remove");
    d2.setRequired(true);
    c.add(d1);
    c.add(d2);
    // add three instances to first dependency, no callbacks should
    // be triggered
    d1.add(new EventImpl(1));
    d1.add(new EventImpl(2));
    d1.add(new EventImpl(3));
    e.step(1);
    // add two instances to the second dependency, callbacks should
    // be invoked (4x)
    d2.add(new EventImpl(1));
    e.step(6);
    // add another dependency, triggering another callback
    d2.add(new EventImpl(2));
    e.step(8);
    // remove first dependency (all three of them) which makes the
    // ComponentImpl unavailable so it should trigger calling remove for
    // all of them (so 5x)
    d1.remove(new EventImpl(1));
    d1.remove(new EventImpl(2));
    d1.remove(new EventImpl(3));
    e.step(14);
    // remove second dependency, should not trigger further callbacks
    d2.remove(new EventImpl(1));
    d2.remove(new EventImpl(2));
    e.step(15);
    c.remove(d2);
    c.remove(d1);
    c.stop();
    e.step(16);
    Assert.assertEquals("Component stopped, should be unavailable again", false, c.isAvailable());
}
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