Search in sources :

Example 86 with Ensure

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

the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumption.

public void testServiceRegistrationAndConsumption() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setCallbacks("add", "remove").setRequired(true));
    Component asp = m.createAspectService(ServiceInterface.class, null, 100, "add", null, "remove", "swap").setImplementation(ServiceProviderAspect.class);
    m.add(sp);
    m.add(sc);
    m.add(asp);
    m.remove(asp);
    m.remove(sc);
    m.remove(sp);
    // ensure we executed all steps inside the component instance
    e.step(8);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 87 with Ensure

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

the class AspectWithPropagationTest method testAspectsWithPropagationNotOverriding.

/**
 * This test does the following:
 *
 * - Create S service with property "p=s"
 * - Create SA (aspect of S) with property "p=aspect"
 * - Create Client, depending on S (actually, on SA).
 * - Client should see SA with properties p=aspect
 * - Change S service property with "p=smodified": the Client should be changed with SA(p=aspect)
 * - Change aspect service property with "p=aspectmodified": The client should be changed with SA(p=aspectmodified)
 */
public void testAspectsWithPropagationNotOverriding() {
    System.out.println("----------- Running testAspectsWithPropagationNotOverriding ...");
    DependencyManager m = getDM();
    m_invokeStep = new Ensure();
    // Create our original "S" service.
    S s = new S() {

        public void invoke() {
        }
    };
    Dictionary props = new Hashtable();
    props.put("p", "s");
    Component sComp = m.createComponent().setImplementation(s).setInterface(S.class.getName(), props);
    // Create SA (aspect of S)
    S sa = new S() {

        volatile S m_s;

        public void invoke() {
        }
    };
    Component saComp = m.createAspectService(S.class, null, 1).setImplementation(sa);
    props = new Hashtable();
    props.put("p", "aspect");
    saComp.setServiceProperties(props);
    // Create client depending on S
    Object client = new Object() {

        int m_changeCount;

        void add(Map props, S s) {
            Assert.assertEquals("aspect", props.get("p"));
            m_invokeStep.step(1);
        }

        void change(Map props, S s) {
            switch(++m_changeCount) {
                case 1:
                    Assert.assertEquals("aspect", props.get("p"));
                    m_invokeStep.step(2);
                    break;
                case 2:
                    Assert.assertEquals("aspectmodified", props.get("p"));
                    m_invokeStep.step(3);
            }
        }
    };
    Component clientComp = m.createComponent().add(m.createServiceDependency().setService(S.class).setRequired(true).setCallbacks("add", "change", null)).setImplementation(client);
    // Add components in dependency manager
    m.add(sComp);
    m.add(saComp);
    m.add(clientComp);
    // client should have been added with SA aspect
    m_invokeStep.waitForStep(1, 5000);
    // now change s "p=s" to "p=smodified": client should not see it
    props = new Hashtable();
    props.put("p", "smodified");
    sComp.setServiceProperties(props);
    m_invokeStep.waitForStep(2, 5000);
    // now change sa aspect "p=aspect" to "p=aspectmodified": client should see it
    props = new Hashtable();
    props.put("p", "aspectmodified");
    saComp.setServiceProperties(props);
    m_invokeStep.waitForStep(3, 5000);
    // remove components
    m.remove(clientComp);
    m.remove(saComp);
    m.remove(sComp);
}
Also used : Dictionary(java.util.Dictionary) 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 88 with Ensure

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

the class AspectWithPropagationTest method testAspectsWithPropagation.

/**
 * This test does the following:
 *
 * - Create S service
 * - Create some S Aspects
 * - Create a Client, depending on S (actually, on the top-level S aspect)
 * - Client has a "change" callback 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 all aspects, and the client has been orderly called in their "change" callback.
 * - Modify the First lowest ranked aspect (rank=1), and check if all aspects, and client have been orderly called in their "change" callback.
 */
public void testAspectsWithPropagation() {
    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", "swap"));
    // 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);
    }
    // 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();
    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());
    // Now, change the top-level ranked aspect: it must propagate to all upper aspects, as well as to the client
    System.out.println("-------------------------- Modifying top-level aspect service properties.");
    m_changeStep = new Ensure();
    for (int i = 1; i <= ASPECTS; i++) {
        // only client has to be changed.
        m_changeStep.step(i);
    }
    props = new Hashtable();
    props.put("a" + ASPECTS, "v" + ASPECTS + "-Modified");
    // That triggers change callbacks for upper aspects (with rank >= 2)
    aspects[ASPECTS - 1].setServiceProperties(props);
    // check if client have been changed.
    m_changeStep.waitForStep(ASPECTS + 1, 5000);
    // Check if top level aspect service properties have been propagated up to the client.
    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 + "-Modified");
    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 89 with Ensure

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

the class BundleDependencyTest method testBundleDependencies.

public void testBundleDependencies() {
    DependencyManager m = getDM();
    // create a service provider and consumer
    Consumer c = new Consumer();
    Component consumer = m.createComponent().setImplementation(c).add(m.createBundleDependency().setCallbacks("add", "remove"));
    // add the service consumer
    m.add(consumer);
    // check if at least one bundle was found
    c.check();
    // remove the consumer again
    m.remove(consumer);
    // check if all bundles were removed correctly
    c.doubleCheck();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component consumerWithFilter = m.createComponent().setImplementation(new FilteredConsumer(e)).add(m.createBundleDependency().setFilter("(Bundle-SymbolicName=" + BSN + ")").setCallbacks("add", "remove"));
    // add a consumer with a filter
    m.add(consumerWithFilter);
    e.step(2);
    // remove the consumer again
    m.remove(consumerWithFilter);
    e.step(4);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) Ensure(org.apache.felix.dm.itest.util.Ensure)

Example 90 with Ensure

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

the class BundleDependencyTest method testRequiredBundleDependencyWithComponentArgInCallbackMethod.

public void testRequiredBundleDependencyWithComponentArgInCallbackMethod() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Component consumerWithFilter = m.createComponent().setImplementation(new FilteredConsumerRequiredWithComponentArg(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)

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