public void testAdapterWithAspectMultipleTimes() {
// TODO this test is broken, it assumes that the order in which listeners are added to the BundleContext will also
// be the order in which they're invoked (which from a spec point of view is not true)
DependencyManager m = new DependencyManager(context);
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
Component provider = m.createComponent()
.setImplementation(new ServiceProvider())
.setInterface(OriginalService.class.getName(), null);
Component consumer = m.createComponent()
.setImplementation(new ServiceConsumer(e))
.add(m.createServiceDependency()
.setService(AdaptedService.class)
.setCallbacks("add", null, "remove", "swap")
);
Component adapter = m.createAdapterService(OriginalService.class, null, "add", null, "remove", "swap")
.setInterface(AdaptedService.class.getName(), null)
.setImplementation(new ServiceAdapter(e,1));
Component adapter2 = m.createAdapterService(OriginalService.class, null, "add", null, "remove", "swap")
.setInterface(AdaptedService.class.getName(), null)
.setImplementation(new ServiceAdapter(e,2));
Component aspect = m.createAspectService(OriginalService.class, null, 10, null)
.setImplementation(ServiceAspect.class);
m.add(provider);
int stepsInLoop = 10;
int loops = 10;
for (int loop = 0; loop < loops; loop++) {
int offset = stepsInLoop * loop;
System.out.println("add adapter");
m.add(adapter);
System.out.println("add consumer");
m.add(consumer);
e.waitForStep(1 + offset, 5000);
System.out.println("add aspect");
m.add(aspect);
// a swap is expected on the adapter
e.waitForStep(2 + offset, 5000);
System.out.println("add adapter2");
m.add(adapter2);
// another aspect adapter will appear
e.waitForStep(4 + offset, 5000);
System.out.println("remove provider");
m.remove(provider);
// two times:
// the aspect adapter will disappear
// the original adapter will (briefly) appear
// the original adapter will disappear
// TODO the test will fail somewhere here most of the time
e.waitForStep(8 + offset, 5000);
System.out.println("remove consumer");
m.remove(consumer);
// nothing should happen, all consumed services were already gone
System.out.println("add provider");
m.add(provider);
// still nothing should happen
System.out.println("remove adapter");
m.remove(adapter);
System.out.println("remove adapter2");
m.remove(adapter2);
System.out.println("remove aspect");
m.remove(aspect);
}
m.remove(provider);
e.waitForStep(stepsInLoop * loops, 5000);
}