@RunWith(PaxExam.class)
public class AspectWhiteboardTest extends TestBase {
@Test
public void testWhiteboardConsumer() {
DependencyManager m = new DependencyManager(context);
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create service providers and consumer
Component sp1 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
ServiceConsumer sci = new ServiceConsumer(e);
Component sc = m.createComponent().setImplementation(sci).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(false).setCallbacks("add", "remove"));
Component sa2 = m.createAspectService(ServiceInterface.class, null, 20, null).setImplementation(new ServiceAspect(e, 3));
Component sa1 = m.createAspectService(ServiceInterface.class, null, 10, null).setImplementation(new ServiceAspect(e, 4));
// start with a service consumer
System.out.println("Adding consumer");
m.add(sc);
// then add two providers, so the consumer will see two services
System.out.println("Adding 2 providers");
m.add(sp1);
m.add(sp2);
// make sure consumer sees both services
Assert.assertEquals(2, sci.services());
// add an aspect with ranking 20
System.out.println("Adding aspect with rank 20");
m.add(sa2);
// make sure the consumer sees the two new aspects and no longer sees the two original services
Assert.assertEquals(2, sci.services());
Assert.assertEquals(20, sci.highestRanking());
Assert.assertEquals(20, sci.lowestRanking());
// add an aspect with ranking 10
System.out.println("Adding aspect with rank 10");
m.add(sa1);
// make sure the consumer still sees the two aspects with ranking 20
Assert.assertEquals(2, sci.services());
Assert.assertEquals(20, sci.highestRanking());
Assert.assertEquals(20, sci.lowestRanking());
// remove the aspect with ranking 20
System.out.println("Removing aspect with rank 20");
m.remove(sa2);
// make sure the consumer now sees the aspects with ranking 10
Assert.assertEquals(2, sci.services());
Assert.assertEquals(10, sci.highestRanking());
Assert.assertEquals(10, sci.lowestRanking());
// remove one of the original services
System.out.println("Removing 1 service");
m.remove(sp1);
// make sure the aspect of that service goes away
Assert.assertEquals(1, sci.services());
Assert.assertEquals(10, sci.highestRanking());
Assert.assertEquals(10, sci.lowestRanking());
// remove the aspect with ranking 10
System.out.println("Removing aspect with rank 10");
m.remove(sa1);
// make sure only the original service remains
Assert.assertEquals(1, sci.services());
Assert.assertEquals(0, sci.highestRanking());
Assert.assertEquals(0, sci.lowestRanking());
System.out.println("Done with test");
// end of test
m.remove(sa2);
m.remove(sp2);
m.remove(sc);
}