Search in sources :

Example 81 with Ensure

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

the class AdapterWithExtraDependenciesTest method testAdapterWithExtraDependenciesAndCallbacks.

public void testAdapterWithExtraDependenciesAndCallbacks() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service adapter that adapts to services S1 and has an optional dependency on services S2
    Component sa = m.createAdapterService(S1.class, null).setImplementation(SA.class).add(m.createServiceDependency().setService(S2.class).setCallbacks("add", "remove"));
    m.add(sa);
    // create a service S1, which triggers the creation of the first adapter instance (A1)
    Component s1 = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new S1Impl());
    m.add(s1);
    // create a service S2, which will be added to A1
    Component s2 = m.createComponent().setInterface(S2.class.getName(), null).setImplementation(new S2Impl(e));
    m.add(s2);
    // create a second service S1, which triggers the creation of the second adapter instance (A2)
    Component s1b = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new S1Impl());
    m.add(s1b);
    // observe that S2 is also added to A2
    e.waitForStep(2, 5000);
    // remove S2 again
    m.remove(s2);
    // make sure both adapters have their "remove" callbacks invoked
    e.waitForStep(4, 5000);
    m.remove(s1);
    m.remove(sa);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 82 with Ensure

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

the class AdapterWithInstanceBoundDependencyTest method testInstanceBoundDependency.

public void testInstanceBoundDependency() {
    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().setInterface(ServiceInterface.class.getName(), null).setImplementation(new ServiceProvider(e));
    Component sp2 = m.createComponent().setInterface(ServiceInterface2.class.getName(), null).setImplementation(new ServiceProvider2(e));
    Component sc = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface3.class).setRequired(true));
    Component sa = m.createAdapterService(ServiceInterface.class, null).setInterface(ServiceInterface3.class.getName(), null).setImplementation(new ServiceAdapter(e));
    m.add(sc);
    m.add(sp);
    m.add(sp2);
    m.add(sa);
    e.waitForStep(5, 15000);
    // cleanup
    m.remove(sa);
    m.remove(sp2);
    m.remove(sp);
    m.remove(sc);
    m.clear();
    // make sure all components are stopped
    e.waitForStep(9, 5000);
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 83 with Ensure

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

the class AspectBaseTest method testSingleAspect.

public void testSingleAspect() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create a service provider and consumer
    ServiceProvider p = new ServiceProvider(e, "a");
    ServiceConsumer c = new ServiceConsumer(e);
    Hashtable<String, Object> props = new Hashtable<>();
    props.put("name", "a");
    Component sp = m.createComponent().setInterface(ServiceInterface.class.getName(), props).setImplementation(p);
    Component sc = m.createComponent().setImplementation(c).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", "remove").setAutoConfig("m_service"));
    Component sa = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(ServiceAspect.class);
    m.add(sc);
    m.add(sp);
    // after the provider was added, the consumer's add should have been invoked once
    e.waitForStep(1, 2000);
    Assert.assertEquals("a", c.invoke());
    m.add(sa);
    // after the aspect was added, the consumer should get and add for the aspect and a remove
    // for the original service
    e.waitForStep(3, 2000);
    Assert.assertEquals("aa", c.invoke());
    m.remove(sa);
    // removing the aspect again should give a remove and add
    e.waitForStep(5, 2000);
    Assert.assertEquals("a", c.invoke());
    m.remove(sp);
    // finally removing the original service should give a remove
    e.waitForStep(6, 2000);
    m.remove(sc);
    e.step(7);
}
Also used : Hashtable(java.util.Hashtable) DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 84 with Ensure

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

the class AspectDynamicsTest method testDynamicallyAddAndRemoveAspect.

public void testDynamicallyAddAndRemoveAspect() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    Ensure aspectStopEnsure = new Ensure();
    // create a service provider and consumer
    Component provider = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component provider2 = m.createComponent().setImplementation(new ServiceProvider2(e)).setInterface(ServiceInterface2.class.getName(), null);
    Component consumer = m.createComponent().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true).setCallbacks("add", null, null, "swap"));
    Component aspect = m.createAspectService(ServiceInterface.class, null, 1, null).setImplementation(new ServiceAspect(e, aspectStopEnsure));
    m.add(consumer);
    m.add(provider);
    // the consumer should invoke the provider here, and when done, arrive at step 3
    // finally wait for step 6 before continuing
    e.waitForStep(3, 15000);
    m.add(aspect);
    // after adding the aspect, we wait for its init to be invoked, arriving at
    // step 4 after an instance bound dependency was added (on a service provided by
    // provider 2)
    e.waitForStep(4, 15000);
    m.add(provider2);
    // after adding provider 2, we should now see the client being swapped, so
    // we wait for step 5 to happen
    e.waitForStep(5, 15000);
    // now we continue with step 6, which will trigger the next part of the consumer's
    // run method to be executed
    e.step(6);
    // invoking step 7, 8 and 9 when invoking the aspect which in turn invokes the
    // dependency and the original service, so we wait for that to finish here, which
    // is after step 10 has been reached (the client will now wait for step 12)
    e.waitForStep(10, 15000);
    m.remove(aspect);
    aspectStopEnsure.waitForStep(1, 15000);
    // removing the aspect should trigger step 11 (in the swap method of the consumer)
    e.waitForStep(11, 15000);
    // step 12 triggers the client to continue
    e.step(12);
    // wait for step 13, the final invocation of the provided service (without aspect)
    e.waitForStep(13, 15000);
    // clean up
    m.remove(provider2);
    m.remove(provider);
    m.remove(consumer);
    e.waitForStep(16, 15000);
    m.clear();
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 85 with Ensure

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

the class AspectWhiteboardTest method testWhiteboardConsumer.

public void testWhiteboardConsumer() {
    DependencyManager m = getDM();
    // helper class that ensures certain steps get executed in sequence
    Ensure e = new Ensure();
    // create service providers and consumer
    Component sp1 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
    ServiceConsumer sci = new ServiceConsumer(e);
    Component sc = m.createComponent().setImplementation(sci).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
    Component sa2 = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(new ServiceAspect(e, 3));
    Component sa1 = m.createAspectService(ServiceInterface.class, null, 10, null).setImplementation(new ServiceAspect(e, 4));
    // start with a service consumer
    System.out.println("Adding consumer");
    m.add(sc);
    // then add two providers, so the consumer will see two services
    System.out.println("Adding 2 providers");
    m.add(sp1);
    m.add(sp2);
    // make sure consumer sees both services
    Assert.assertEquals(2, sci.services());
    // add an aspect with ranking 20
    System.out.println("Adding aspect with rank 20");
    m.add(sa2);
    // make sure the consumer sees the two new aspects and no longer sees the two original services
    Assert.assertEquals(2, sci.services());
    Assert.assertEquals(20, sci.highestRanking());
    Assert.assertEquals(20, sci.lowestRanking());
    // add an aspect with ranking 10
    System.out.println("Adding aspect with rank 10");
    m.add(sa1);
    // make sure the consumer still sees the two aspects with ranking 20
    Assert.assertEquals(2, sci.services());
    Assert.assertEquals(20, sci.highestRanking());
    Assert.assertEquals(20, sci.lowestRanking());
    // remove the aspect with ranking 20
    System.out.println("Removing aspect with rank 20");
    m.remove(sa2);
    // make sure the consumer now sees the aspects with ranking 10
    Assert.assertEquals(2, sci.services());
    Assert.assertEquals(10, sci.highestRanking());
    Assert.assertEquals(10, sci.lowestRanking());
    // remove one of the original services
    System.out.println("Removing 1 service");
    m.remove(sp1);
    // make sure the aspect of that service goes away
    Assert.assertEquals(1, sci.services());
    Assert.assertEquals(10, sci.highestRanking());
    Assert.assertEquals(10, sci.lowestRanking());
    // remove the aspect with ranking 10
    System.out.println("Removing aspect with rank 10");
    m.remove(sa1);
    // make sure only the original service remains
    Assert.assertEquals(1, sci.services());
    Assert.assertEquals(0, sci.highestRanking());
    Assert.assertEquals(0, sci.lowestRanking());
    System.out.println("Done with test");
    // end of test
    m.remove(sa2);
    m.remove(sp2);
    m.remove(sc);
}
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