use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class AspectServiceDependencyWithSwapCallbackTest method testServiceRegistrationAndConsumptionRef.
public void testServiceRegistrationAndConsumptionRef() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
ServiceConsumer scimpl = new ServiceConsumer(e);
Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
Component sc = component(m).impl(scimpl).withSvc(ServiceInterface.class, s -> s.add(scimpl::add).remove(scimpl::remove).swap(scimpl::swap)).build();
Component asp = aspect(m, ServiceInterface.class).rank(100).impl(ServiceProviderAspect.class).build();
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(7);
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumptionRef.
public void testServiceRegistrationAndConsumptionRef() {
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 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add(ServiceConsumer::add).remove(ServiceConsumer::remove)).build();
Component asp = aspect(m, ServiceInterface.class).impl(ServiceProviderAspect.class).rank(100).add(ServiceProviderAspect::add).remove(ServiceProviderAspect::remove).swap(ServiceProviderAspect::swap).build();
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);
}
use of org.apache.felix.dm.DependencyManager 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 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add("add").remove("remove")).build();
Component asp = aspect(m, ServiceInterface.class).rank(100).add("add").remove("remove").swap("swap").impl(ServiceProviderAspect.class).build();
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);
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class AspectWithCallbacksServiceDependencyTest method testServiceRegistrationAndConsumptionWithAspectCallbackInstance.
public void testServiceRegistrationAndConsumptionWithAspectCallbackInstance() {
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 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s -> s.add("add").remove("remove")).build();
ServiceProviderAspect providerAspect = new ServiceProviderAspect();
ServiceProviderAspectCallbackInstance aspectCb = new ServiceProviderAspectCallbackInstance(providerAspect);
Component asp = aspect(m, ServiceInterface.class).rank(100).impl(providerAspect).add(aspectCb::add).remove(aspectCb::remove).swap(aspectCb::swap).build();
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);
}
use of org.apache.felix.dm.DependencyManager 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 = component(m).impl(new SImpl()).provides(S.class, props).build();
// Create some "S" aspects
Component[] aspects = new Component[ASPECTS];
for (int rank = 1; rank <= ASPECTS; rank++) {
aspects[rank - 1] = aspect(m, S.class).rank(rank).impl(new A("A" + rank, rank)).add("add").change("change").remove("remove").swap("swap").build();
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 = adapter(m, S.class).add("add").change("change").remove("remove").swap("swap").provides(S2.class).impl(new S2Impl()).build();
// Create Client2, which depends on "S2" service.
Client2 client2Impl;
Component client2 = component(m).impl((client2Impl = new Client2())).withSvc(S2.class, srv -> srv.add("add").change("change")).build();
// 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();
}
Aggregations