@RunWith(PaxExam.class)
public class ResourceAdapterDependencyAddAndRemoveTest extends TestBase {
@Test
public void testBasicResourceAdapter() throws Exception {
DependencyManager m = new DependencyManager(context);
// 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(e);
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
CallbackInstance callbackInstance = new CallbackInstance(e);
Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed")
.setImplementation(new ResourceAdapter(e))
.setCallbacks(callbackInstance, "init", "start", "stop", "destroy")
.add(m.createServiceDependency()
.setService(ServiceInterface.class)
.setRequired(true)
.setInstanceBound(true));
// add a component state listener
component.addStateListener(new ComponentStateListenerImpl(e));
// 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
m.remove(component);
// wait for the stopped callback in the state listener
e.waitForStep(4, 5000);
}