Search in sources :

Example 16 with ComponentImpl

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

the class ComponentTest method createAndStartComponentAddTwoDependenciesMakeBothAvailableAndUnavailableWithCallbacks.

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

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

        public void remove() {
            e.step();
        }
    });
    // start the component, it should become available
    c.start();
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setCallbacks("add", "remove");
    d1.setRequired(true);
    SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setCallbacks("add", "remove");
    d2.setRequired(true);
    // add the first dependency, ComponentImpl should be unavailable
    c.add(d1);
    c.add(d2);
    // make first dependency available, ComponentImpl should still be unavailable
    d1.add(new EventImpl());
    e.step(1);
    // make second dependency available, ComponentImpl available, callbacks should
    // be invoked
    d2.add(new EventImpl());
    e.step(4);
    // remove the first dependency, callbacks should be invoked
    d1.remove(new EventImpl());
    e.step(7);
    // remove second dependency, no callbacks should be invoked
    d2.remove(new EventImpl());
    e.step(8);
    c.remove(d2);
    c.remove(d1);
    c.stop();
    e.step(9);
    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 17 with ComponentImpl

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

the class ComponentTest method testAtomicallyAddMultipleDependenciesFromInitCallback.

@Test
public void testAtomicallyAddMultipleDependenciesFromInitCallback() {
    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, d2);
            // won't trigger start because d2 is not yet available
            d.add(new EventImpl());
        }

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

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

        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 18 with ComponentImpl

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

the class ComponentTest method createAndStartComponentAddDependencyMakeAvailableAndUnavailableWithCallbacks.

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

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

        public void remove() {
            e.step(3);
        }
    });
    SimpleServiceDependency d1 = new SimpleServiceDependency();
    d1.setCallbacks("add", "remove");
    d1.setRequired(true);
    // start the ComponentImpl (it should become available)
    c.start();
    // add the dependency (it should become unavailable)
    c.add(d1);
    // make the dependency available, which should invoke the
    // add callback
    d1.add(new EventImpl());
    e.step(2);
    // make the dependency unavailable, should trigger the
    // remove callback
    d1.remove(new EventImpl());
    e.step(4);
    c.remove(d1);
    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 19 with ComponentImpl

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

the class ComponentTest method createDependenciesWithCallbackInstance.

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

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

        void stop() {
            e.step(4);
        }
    });
    Object callbackInstance = new Object() {

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

        void remove() {
            e.step(5);
        }
    };
    SimpleServiceDependency d = new SimpleServiceDependency();
    d.setCallbacks(callbackInstance, "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 20 with ComponentImpl

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

the class ComponentTest method testInitCallbackOfComponent.

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

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

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

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

        void destroy() {
            e.step(6);
        }
    });
    e.step(1);
    c.start();
    e.step(4);
    c.stop();
    e.step(7);
}
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