public class ServiceUpdateTest extends TestBase {
@Test
public void testServiceUpdate() throws Exception {
DependencyManager m = new DependencyManager(context);
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a resource provider
ResourceProvider provider = new ResourceProvider(e);
// activate it
m.add(m.createComponent()
.setImplementation(new ServiceProvider(e))
.add(m.createServiceDependency()
.setService(ServiceInterface.class)
.setRequired(true)
.setCallbacks("add", "change", "remove")
)
);
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
CallbackInstance callbackInstance = new CallbackInstance(e);
Properties serviceProps = new Properties();
serviceProps.setProperty("number", "1");
Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed")
.setImplementation(new ResourceAdapter(e))
.setInterface(ServiceInterface.class.getName(), serviceProps)
.setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
m.add(component);
// wait until the single resource is available
e.waitForStep(1, 5000);
// wait until the component gets the dependency injected
e.waitForStep(2, 5000);
// trigger a 'change' in our resource
provider.change();
// wait until the changed callback is invoked
e.waitForStep(3, 5000);
// wait until the changed event arrived at the component
e.waitForStep(4, 5000);
System.out.println("Done!");
m.clear();
}