Search in sources :

Example 31 with Ensure

use of org.apache.felix.dm.itest.util.Ensure in project felix by apache.

the class AspectWithPropagationTest method testAspectsWithPropagationAndNoCallbacks.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects without any callbacks (add/change/remove/swap)
 * - Create a Client, depending on S (actually, on the top-level S aspect)
 * - Client has a "change" callack in order to track S service properties modifications.
 * - First, invoke Client.invoke(): all S aspects, and finally original S service must be invoked orderly.
 * - Modify S original service properties, and check if the client has been called in its "change" callback.
 */
public void testAspectsWithPropagationAndNoCallbacks() {
    System.out.println("----------- Running testAspectsWithPropagation ...");
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component s = m.createComponent().setImplementation(new SImpl()).setInterface(S.class.getName(), props);
    // Create an aspect aware client, depending on "S" service.
    Client clientImpl;
    Component client = m.createComponent().setImplementation((clientImpl = new Client())).add(m.createServiceDependency().setService(S.class).setRequired(true).setCallbacks("add", "change", "remove"));
    // Create some "S" aspects
    Component[] aspects = new Component[ASPECTS];
    for (int rank = 1; rank <= ASPECTS; rank++) {
        aspects[rank - 1] = m.createAspectService(S.class, null, rank).setImplementation(new A("A" + rank, rank));
        props = new Hashtable();
        props.put("a" + rank, "v" + rank);
        aspects[rank - 1].setServiceProperties(props);
    }
    // Register client
    m.add(client);
    // Randomly register aspects and original service
    boolean originalServiceAdded = false;
    for (int i = 0; i < ASPECTS; i++) {
        int index = getRandomAspect();
        m.add(aspects[index]);
        if (!originalServiceAdded && _rnd.nextBoolean()) {
            m.add(s);
            originalServiceAdded = true;
        }
    }
    if (!originalServiceAdded) {
        m.add(s);
    }
    // All set, check if client has inherited from top level aspect properties + original service properties
    Map check = new HashMap();
    check.put("foo", "bar");
    for (int i = 1; i < (ASPECTS - 1); i++) {
        // we must not inherit from lower ranks, only from the top-level aspect.
        check.put("a" + i, null);
    }
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, clientImpl.getServiceProperties());
    // Now invoke client, which orderly calls all aspects in the chain, and finally the original service "S".
    System.out.println("-------------------------- Invoking client.");
    clientImpl.invoke();
    m_invokeStep.waitForStep(ASPECTS + 1, 5000);
    // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, and on client.
    System.out.println("-------------------------- Modifying original service properties.");
    m_changeStep = new Ensure();
    for (int i = 1; i <= ASPECTS; i++) {
        // skip aspects, which have no "change" callbacks.
        m_changeStep.step(i);
    }
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if aspects and client have been orderly called in their "changed" callback
    m_changeStep.waitForStep(ASPECTS + 1, 5000);
    // Check if modified "foo" original service property has been propagated
    check = new HashMap();
    check.put("foo", "barModified");
    for (int i = 1; i < (ASPECTS - 1); i++) {
        // we must not inherit from lower ranks, only from the top-level aspect.
        check.put("a" + i, null);
    }
    // we only see top-level aspect service properties
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, clientImpl.getServiceProperties());
    // Clear all components.
    m_changeStep = null;
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with Ensure

use of org.apache.felix.dm.itest.util.Ensure in project felix by apache.

the class AspectWithPropagationTest method testAdapterWithAspectsAndPropagationNoCallbacks.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects without any callbacks (add/change/remove)
 * - Create S2 Adapter, which adapts S to S2 (but does not have any add/change/remove callbacks)
 * - Create Client2, which depends on S2. Client2 listens to S2 property change events.
 * - Now, invoke Client2.invoke(): all S aspects, and finally original S service must be invoked orderly.
 * - Modify S original service properties, and check if all aspects, S2 Adapter, and Client2 have been orderly called in their "change" callback.
 */
public void testAdapterWithAspectsAndPropagationNoCallbacks() {
    System.out.println("----------- Running testAdapterWithAspectsAndPropagationNoCallbacks ...");
    DependencyManager m = getDM();
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component s = m.createComponent().setImplementation(new SImpl()).setInterface(S.class.getName(), props);
    // Create some "S" aspects
    Component[] aspects = new Component[ASPECTS];
    for (int rank = 1; rank <= ASPECTS; rank++) {
        aspects[rank - 1] = m.createAspectService(S.class, null, rank).setImplementation(new A("A" + rank, rank));
        props = new Hashtable();
        props.put("a" + rank, "v" + rank);
        aspects[rank - 1].setServiceProperties(props);
    }
    // Create S2 adapter (which adapts S1 to S2 interface)
    Component adapter = m.createAdapterService(S.class, null).setInterface(S2.class.getName(), null).setImplementation(new S2Impl());
    // Create Client2, which depends on "S2" service.
    Client2 client2Impl;
    Component client2 = m.createComponent().setImplementation((client2Impl = new Client2())).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    // Register client2
    m.add(client2);
    // Register S2 adapter
    m.add(adapter);
    // Randomly register aspects, original service
    boolean originalServiceAdded = false;
    for (int i = 0; i < ASPECTS; i++) {
        int index = getRandomAspect();
        m.add(aspects[index]);
        if (!originalServiceAdded && _rnd.nextBoolean()) {
            m.add(s);
            originalServiceAdded = true;
        }
    }
    if (!originalServiceAdded) {
        m.add(s);
    }
    // Now invoke client2, which orderly calls all S1 aspects, then S1Impl, and finally S2 service
    System.out.println("-------------------------- Invoking client2.");
    client2Impl.invoke2();
    m_invokeStep.waitForStep(ASPECTS + 2, 5000);
    // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, S2Impl, and Client2.
    System.out.println("-------------------------- Modifying original service properties.");
    m_changeStep = new Ensure();
    for (int i = 1; i <= ASPECTS + 1; i++) {
        // skip all aspects and the adapter
        m_changeStep.step(i);
    }
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if Client2 has been called in its "changed" callback
    m_changeStep.waitForStep(ASPECTS + 2, 5000);
    // Check if modified "foo" original service property has been propagated to Client2
    Map check = new HashMap();
    check.put("foo", "barModified");
    for (int i = 1; i < (ASPECTS - 1); i++) {
        // we must not inherit from lower ranks, only from the top-level aspect.
        check.put("a" + i, null);
    }
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, client2Impl.getServiceProperties());
    // Clear all components.
    m_changeStep = null;
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with Ensure

use of org.apache.felix.dm.itest.util.Ensure in project felix by apache.

the class AspectWithPropagationTest method testAdapterWithAspectsAndPropagation.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects
 * - Create S2 Adapter, which adapts S to S2
 * - Create Client2, which depends on S2. Client2 listens to S2 property change events.
 * - Now, invoke Client2.invoke(): all S aspects, and finally original S service must be invoked orderly.
 * - Modify S original service properties, and check if all aspects, S2 Adapter, and Client2 have been orderly called in their "change" callback.
 */
public void testAdapterWithAspectsAndPropagation() {
    System.out.println("----------- Running testAdapterWithAspectsAndPropagation ...");
    DependencyManager m = getDM();
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    Dictionary props = new Hashtable();
    props.put("foo", "bar");
    Component s = m.createComponent().setImplementation(new SImpl()).setInterface(S.class.getName(), props);
    // Create some "S" aspects
    Component[] aspects = new Component[ASPECTS];
    for (int rank = 1; rank <= ASPECTS; rank++) {
        aspects[rank - 1] = m.createAspectService(S.class, null, rank, "add", "change", "remove", "swap").setImplementation(new A("A" + rank, rank));
        props = new Hashtable();
        props.put("a" + rank, "v" + rank);
        aspects[rank - 1].setServiceProperties(props);
    }
    // Create S2 adapter (which adapts S1 to S2 interface)
    Component adapter = m.createAdapterService(S.class, null, "add", "change", "remove", "swap").setInterface(S2.class.getName(), null).setImplementation(new S2Impl());
    // Create Client2, which depends on "S2" service.
    Client2 client2Impl;
    Component client2 = m.createComponent().setImplementation((client2Impl = new Client2())).add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks("add", "change", null));
    // Register client2
    m.add(client2);
    // Register S2 adapter
    m.add(adapter);
    // Randomly register aspects, original service
    boolean originalServiceAdded = false;
    for (int i = 0; i < ASPECTS; i++) {
        int index = getRandomAspect();
        m.add(aspects[index]);
        if (!originalServiceAdded && _rnd.nextBoolean()) {
            m.add(s);
            originalServiceAdded = true;
        }
    }
    if (!originalServiceAdded) {
        m.add(s);
    }
    // Now invoke client2, which orderly calls all S1 aspects, then S1Impl, and finally S2 service
    System.out.println("-------------------------- Invoking client2.");
    client2Impl.invoke2();
    m_invokeStep.waitForStep(ASPECTS + 2, 5000);
    // Now, change original service "S" properties: this will orderly trigger "change" callbacks on aspects, S2Impl, and Client2.
    System.out.println("-------------------------- Modifying original service properties.");
    m_changeStep = new Ensure();
    props = new Hashtable();
    props.put("foo", "barModified");
    s.setServiceProperties(props);
    // Check if aspects and Client2 have been orderly called in their "changed" callback
    m_changeStep.waitForStep(ASPECTS + 2, 5000);
    // Check if modified "foo" original service property has been propagated to Client2
    Map check = new HashMap();
    check.put("foo", "barModified");
    for (int i = 1; i < (ASPECTS - 1); i++) {
        // we must not inherit from lower ranks, only from the top-level aspect.
        check.put("a" + i, null);
    }
    check.put("a" + ASPECTS, "v" + ASPECTS);
    checkServiceProperties(check, client2Impl.getServiceProperties());
    // Clear all components.
    m_changeStep = null;
    m.clear();
}
Also used : Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with Ensure

use of org.apache.felix.dm.itest.util.Ensure in project felix by apache.

the class BundleDependencyTest method testRequiredBundleDependency.

public void testRequiredBundleDependency() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component consumerWithFilter = m.createComponent().setImplementation(new FilteredConsumerRequired(e)).add(m.createBundleDependency().setRequired(true).setFilter("(Bundle-SymbolicName=" + BSN + ")").setCallbacks("add", "remove"));
    // add a consumer with a filter
    m.add(consumerWithFilter);
    e.waitForStep(1, 5000);
    // remove the consumer again
    m.remove(consumerWithFilter);
    e.waitForStep(2, 5000);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 35 with Ensure

use of org.apache.felix.dm.itest.util.Ensure in project felix by apache.

the class ComponentStateListenerTest method testDynamicComponentStateListingLifeCycle.

public void testDynamicComponentStateListingLifeCycle() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a simple service component
    Component s = m.createComponent().setInterface(MyInterface.class.getName(), null).setImplementation(new DynamicComponentStateListeningInstance(e));
    // add it, and since it has no dependencies, it should be activated immediately
    m.add(s);
    // remove it so it gets destroyed
    m.remove(s);
    // ensure we executed all steps inside the component instance
    e.step(10);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Aggregations

Ensure (org.apache.felix.dm.itest.util.Ensure)135 Component (org.apache.felix.dm.Component)90 DependencyManager (org.apache.felix.dm.DependencyManager)90 ServiceRegistration (org.osgi.framework.ServiceRegistration)42 Hashtable (java.util.Hashtable)32 Dictionary (java.util.Dictionary)8 URL (java.net.URL)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ResourceHandler (org.apache.felix.dm.ResourceHandler)5 IOException (java.io.IOException)4 Dependency (org.apache.felix.dm.Dependency)4 ServiceDependency (org.apache.felix.dm.ServiceDependency)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 ArrayList (java.util.ArrayList)2 ForkJoinPool (java.util.concurrent.ForkJoinPool)2 TimeUnit (java.util.concurrent.TimeUnit)2 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)2 TestBase (org.apache.felix.dm.itest.util.TestBase)2 Bundle (org.osgi.framework.Bundle)2