use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class DMCommandTest method testComponentThatHaveCycliclyDependencyOnAOtheComponentShouldRegisterAsFailure.
@Test
public void testComponentThatHaveCycliclyDependencyOnAOtheComponentShouldRegisterAsFailure() {
OUT.println("testComponentThatHaveCycliclyDependencyOnAOtheComponentShouldRegisterAsFailure");
setupEmptyBundles();
DependencyManager dm = new DependencyManager(m_bundleContext);
DependencyManager.getDependencyManagers().add(dm);
Component component1 = dm.createComponent().setImplementation(Cipher.class).setInterface(Cipher.class.getName(), null).add(dm.createServiceDependency().setService(Math.class).setRequired(true));
dm.add(component1);
Component component2 = dm.createComponent().setImplementation(Math.class).setInterface(Math.class.getName(), null).add(dm.createServiceDependency().setService(Cipher.class).setRequired(true));
dm.add(component2);
dme.wtf();
String output = outContent.toString();
assertTrue(output.contains("-> java.lang.Math -> javax.crypto.Cipher -> java.lang.Math") || output.contains("-> javax.crypto.Cipher -> java.lang.Math -> javax.crypto.Cipher"));
// remove the mess
dm.remove(component1);
dm.remove(component2);
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class RemovedDependencyTest method testRemoveDependencyAndConsumerMustRemainStarted.
public void testRemoveDependencyAndConsumerMustRemainStarted() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// Create two providers
Hashtable props = new Hashtable();
props.put("name", "provider1");
Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
props = new Properties();
props.put("name", "provider2");
Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
props = new Properties();
props.put("name", "provider3");
Component sp3 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
// Create the consumer, and start it
Dependency d1 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider1)").setRequired(true).setCallbacks("add", "remove");
Dependency d2 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider2)").setRequired(true).setCallbacks("add", "remove");
Dependency d3 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider3)").setRequired(true).setCallbacks("add", "remove");
ServiceConsumer consumer = new ServiceConsumer(e, d3);
Component sc = m.createComponent().setImplementation(consumer).add(d1, d2);
// Add the first two providers and the consumer
m.add(sp);
m.add(sp2);
m.add(sp3);
m.add(sc);
// Check if consumer has been bound to the three providers
e.waitForStep(3, 5000);
Assert.assertEquals(3, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNotNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider2, and check if the consumer is still alive
sc.remove(d2);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(2, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider3 (the consumer has an instance bound dependency on it), and check if the consumer is still alive
sc.remove(d3);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(1, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNull(consumer.getProvider("provider3"));
m.clear();
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class ResourceAdapterDependencyAddAndRemoveTest method testBasicResourceAdapter.
public void testBasicResourceAdapter() throws Exception {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create and add a service provider
m.add(m.createComponent().setInterface(ServiceInterface.class.getName(), null).setImplementation(new ServiceProvider(e)));
// create and add a resource provider
ResourceProvider provider = new ResourceProvider(context, new URL("file://localhost/path/to/file1.txt"));
m.add(m.createComponent().setImplementation(provider).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove")));
// create a resource adapter for our single resource
// note that we can provide an actual implementation instance here because there will be only one
// adapter, normally you'd want to specify a Class here
// also, create a callback instance which will be used for both callbacks on resource changes and
// life cycle callbacks on the adapters themselves
Dependency d = m.createServiceDependency().setService(ServiceInterface.class).setRequired(true);
CallbackInstance callbackInstance = new CallbackInstance(e, d);
Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed").setImplementation(new ResourceAdapter(e)).setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
// add the resource adapter
m.add(component);
// wait until the single resource is available (the adapter has been started)
e.waitForStep(1, 5000);
// trigger a 'change' in our resource
provider.change();
// wait until the changed callback is invoked
e.waitForStep(2, 5000);
// and has completed (ensuring no "extra" steps are invoked in the mean time)
e.waitForStep(3, 5000);
// remove the resource adapter again
// add a component state listener, in order to track resource adapter destruction
component.add(new ComponentStateListenerImpl(e));
m.remove(component);
// wait for the stopped callback in the state listener
e.waitForStep(4, 5000);
m.clear();
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class ServiceDependencyTest 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).setRequired(true));
Component sc2 = m.createComponent().setImplementation(new ServiceConsumerCallbacks(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
m.add(sp);
m.add(sc);
m.remove(sc);
m.add(sc2);
m.remove(sp);
m.remove(sc2);
// ensure we executed all steps inside the component instance
e.step(6);
}
use of org.apache.felix.dm.DependencyManager in project felix by apache.
the class TemporalServiceDependencyTest method testServiceConsumptionWithCallbackAndIntermittentAvailability.
public void testServiceConsumptionWithCallbackAndIntermittentAvailability() {
final DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
TemporalServiceProvider provider = new TemporalServiceProvider(e);
Component sp = m.createComponent().setImplementation(provider).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
Component sp2 = m.createComponent().setImplementation(provider2).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceConsumerWithCallback consumer = new TemporalServiceConsumerWithCallback(e);
ServiceDependency temporalDep = m.createTemporalServiceDependency(10000).setService(TemporalServiceInterface.class).setRequired(true).setCallbacks("add", "remove");
Component sc = m.createComponent().setImplementation(consumer).add(temporalDep);
// add the service consumer
m.add(sc);
// now add the first provider
m.add(sp);
e.waitForStep(2, 5000);
// and remove it again (this should not affect the consumer yet)
m.remove(sp);
// now add the second provider
m.add(sp2);
e.step(3);
e.waitForStep(4, 5000);
// and remove it again
m.remove(sp2);
// finally remove the consumer
m.remove(sc);
// Wait for the consumer.remove callback
e.waitForStep(6, 5000);
// ensure we executed all steps inside the component instance
e.step(7);
m.clear();
}
Aggregations