{
final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
String name ="test_optional_single_dynamic";
ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp10 = SimpleComponent.INSTANCE;
TestCase.assertNotNull( comp10 );
TestCase.assertEquals( srv1, comp10.m_singleRef );
TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
srv1.drop();
// no delay, should be immediate
final SimpleComponent comp11 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp10, comp11 );
TestCase.assertNull( comp11.m_singleRef );
TestCase.assertTrue( comp11.m_multiRef.isEmpty() );
final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
delay(); // async binding
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp12 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp10, comp12 );
TestCase.assertEquals( srv2, comp12.m_singleRef );
TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
disableAndCheck(cc);
final SimpleServiceImpl srv3 = SimpleServiceImpl.create( bundleContext, "srv3" );
// enable component with two services available, expect srv2 bind
// async enabling
enableAndCheck(cc.description);
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp20 = SimpleComponent.INSTANCE;
TestCase.assertNotNull( comp20 );
TestCase.assertNotSame( comp10, comp20 );
TestCase.assertEquals( srv2, comp20.m_singleRef );
TestCase.assertTrue( comp20.m_multiRef.isEmpty() );
// drop srv2, expect rebind to srv3 (synchronously)
srv2.drop();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp21 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp20, comp21 );
TestCase.assertEquals( srv3, comp21.m_singleRef );
TestCase.assertTrue( comp21.m_multiRef.isEmpty() );
// create srv4, expect no rebind
final SimpleServiceImpl srv4 = SimpleServiceImpl.create( bundleContext, "srv4" );
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp22 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp20, comp22 );
TestCase.assertEquals( srv3, comp22.m_singleRef );
TestCase.assertTrue( comp22.m_multiRef.isEmpty() );
// drop srv4 again, expect no rebind
srv4.drop();
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp23 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp20, comp23 );
TestCase.assertEquals( srv3, comp23.m_singleRef );
TestCase.assertTrue( comp23.m_multiRef.isEmpty() );
// "reset"
disableAndCheck(cc);
srv3.drop();
delay();
// two services with service ranking (srv6 > srv5)
final SimpleServiceImpl srv5 = SimpleServiceImpl.create( bundleContext, "srv5", 10 );
final SimpleServiceImpl srv6 = SimpleServiceImpl.create( bundleContext, "srv6", 20 );
enableAndCheck(cc.description);
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp30 = SimpleComponent.INSTANCE;
TestCase.assertNotSame( comp20, comp30 );
TestCase.assertEquals( srv6, comp30.m_singleRef );
TestCase.assertTrue( comp30.m_multiRef.isEmpty() );
// another service with higher ranking -- no rebind !
final SimpleServiceImpl srv7 = SimpleServiceImpl.create( bundleContext, "srv7", 30 );
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp31 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp30, comp31 );
TestCase.assertEquals( srv6, comp31.m_singleRef );
TestCase.assertTrue( comp31.m_multiRef.isEmpty() );
// srv6 goes, rebind to srv7
srv6.drop();
delay();
findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp32 = SimpleComponent.INSTANCE;
TestCase.assertSame( comp30, comp32 );
TestCase.assertEquals( srv7, comp32.m_singleRef );
TestCase.assertTrue( comp32.m_multiRef.isEmpty() );
}