use of org.apache.felix.scr.integration.components.SimpleComponent in project felix by apache.
the class ComponentConfigurationTest method test_SimpleComponent_dynamic_optional_configuration_with_required_service.
@Test
public void test_SimpleComponent_dynamic_optional_configuration_with_required_service() throws Exception {
final String targetProp = "ref.target";
final String filterProp = "required";
final SimpleServiceImpl service = SimpleServiceImpl.create(bundleContext, "sample").setFilterProperty(filterProp);
try {
final String pid = "DynamicConfigurationComponentWithRequiredReference";
deleteConfig(pid);
delay();
// mandatory ref missing --> component unsatisfied
ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
// dynamically configure without the correct target
configure(pid);
delay();
// mandatory ref missing --> component unsatisfied
findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
// dynamically configure with correct target
theConfig.put(targetProp, "(filterprop=" + filterProp + ")");
configure(pid);
delay();
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNotNull(SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
TestCase.assertEquals(pid, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
final SimpleComponent instance = SimpleComponent.INSTANCE;
configure(pid);
delay();
// same instance after reconfiguration
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertEquals(instance, SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
TestCase.assertEquals(pid, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
TestCase.assertNotNull(SimpleComponent.INSTANCE.m_singleRef);
// reconfigure without target --> unsatisifed
theConfig.remove(targetProp);
configure(pid);
delay();
// mandatory ref missing --> component unsatisfied
findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
deleteConfig(pid);
delay();
// mandatory ref missing --> component unsatisfied
findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
disableAndCheck(cc);
TestCase.assertNull(SimpleComponent.INSTANCE);
} finally {
theConfig.remove(targetProp);
if (service != null) {
service.drop();
}
}
}
use of org.apache.felix.scr.integration.components.SimpleComponent in project felix by apache.
the class ComponentConfigurationTest method test_SimpleComponent_configuration_optional.
@Test
public void test_SimpleComponent_configuration_optional() throws Exception {
final String pid = "SimpleComponent.configuration.optional";
ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent firstInstance = SimpleComponent.INSTANCE;
TestCase.assertNotNull(firstInstance);
TestCase.assertNull(firstInstance.getProperty(PROP_NAME));
configure(pid);
delay();
final SimpleComponent secondInstance = SimpleComponent.INSTANCE;
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNotNull(secondInstance);
TestCase.assertEquals(PROP_NAME, secondInstance.getProperty(PROP_NAME));
deleteConfig(pid);
delay();
final SimpleComponent thirdInstance = SimpleComponent.INSTANCE;
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNotNull(thirdInstance);
TestCase.assertNull(thirdInstance.getProperty(PROP_NAME));
TestCase.assertNotSame("Expect new instance object after reconfiguration", firstInstance, secondInstance);
TestCase.assertNotSame("Expect new instance object after configuration deletion (1)", firstInstance, thirdInstance);
TestCase.assertNotSame("Expect new instance object after configuration deletion (2)", secondInstance, thirdInstance);
disableAndCheck(cc);
TestCase.assertNull(SimpleComponent.INSTANCE);
}
use of org.apache.felix.scr.integration.components.SimpleComponent in project felix by apache.
the class ConfigurationChangeTest method testSingleDynamicRequiredFactory.
// I'm not sure what should happen in this case, asking on dev list.
// @Test
public void testSingleDynamicRequiredFactory() throws Exception {
String pid = "test_required_single_dynamic_factory";
final String factoryPid = "factory_" + pid;
boolean dynamic = true;
final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
theConfig.put("ref.target", "(value=srv1)");
configure(pid);
// ?????? Not clear what should happen.
getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
// create a component instance
final ServiceReference[] refs = bundleContext.getServiceReferences(ComponentFactory.class.getName(), "(" + ComponentConstants.COMPONENT_FACTORY + "=" + factoryPid + ")");
TestCase.assertNotNull(refs);
TestCase.assertEquals(1, refs.length);
final ComponentFactory factory = (ComponentFactory) bundleContext.getService(refs[0]);
TestCase.assertNotNull(factory);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
final ComponentInstance instance = factory.newInstance(props);
TestCase.assertNotNull(instance);
TestCase.assertNotNull(instance.getInstance());
TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
final SimpleComponent comp10 = SimpleComponent.INSTANCE;
TestCase.assertNotNull(comp10);
TestCase.assertEquals(srv1, comp10.m_singleRef);
TestCase.assertTrue(comp10.m_multiRef.isEmpty());
TestCase.assertEquals(1, comp10.m_singleRefBind);
TestCase.assertEquals(0, comp10.m_singleRefUnbind);
// update configuration to target srv2
theConfig.put("ref.target", "(value=srv2)");
configure(pid);
delay();
// should bind to srv2
SimpleComponent comp20;
if (dynamic) {
// fails here, config modifications are not propagated to instances from factory.
TestCase.assertEquals(1, comp10.m_modified);
comp20 = comp10;
TestCase.assertEquals(2, comp20.m_singleRefBind);
TestCase.assertEquals(1, comp20.m_singleRefUnbind);
} else {
TestCase.assertEquals(0, comp10.m_modified);
comp20 = SimpleComponent.INSTANCE;
TestCase.assertNotSame(comp10, comp20);
TestCase.assertEquals(0, comp20.m_modified);
TestCase.assertEquals(1, comp20.m_singleRefBind);
TestCase.assertEquals(0, comp20.m_singleRefUnbind);
TestCase.assertEquals(1, comp10.m_singleRefUnbind);
}
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertEquals(srv2, comp20.m_singleRef);
TestCase.assertTrue(comp20.m_multiRef.isEmpty());
}
use of org.apache.felix.scr.integration.components.SimpleComponent in project felix by apache.
the class ConfigurationChangeTest method multipleTest.
private void multipleTest(String pid, boolean dynamic) throws Exception {
final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
theConfig.put("ref.target", "(value=srv1)");
configure(pid);
// let cm thread finish before enabling.
delay();
getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
final SimpleComponent comp10 = SimpleComponent.INSTANCE;
TestCase.assertNotNull(comp10);
TestCase.assertEquals(1, comp10.m_multiRef.size());
TestCase.assertEquals(srv1, comp10.m_multiRef.iterator().next());
TestCase.assertEquals(1, comp10.m_multiRefBind);
TestCase.assertEquals(0, comp10.m_multiRefUnbind);
// update configuration to target srv2
theConfig.put("ref.target", "(value=srv2)");
configure(pid);
delay();
// should bind to srv2
SimpleComponent comp20;
if (dynamic) {
TestCase.assertEquals(1, comp10.m_modified);
comp20 = comp10;
TestCase.assertEquals(2, comp20.m_multiRefBind);
TestCase.assertEquals(1, comp20.m_multiRefUnbind);
} else {
TestCase.assertEquals(0, comp10.m_modified);
comp20 = SimpleComponent.INSTANCE;
TestCase.assertNotSame(comp10, comp20);
TestCase.assertEquals(0, comp20.m_modified);
TestCase.assertEquals(1, comp20.m_multiRefBind);
TestCase.assertEquals(0, comp20.m_multiRefUnbind);
TestCase.assertEquals(1, comp10.m_multiRefUnbind);
}
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertEquals(1, comp20.m_multiRef.size());
TestCase.assertEquals(srv2, comp20.m_multiRef.iterator().next());
}
use of org.apache.felix.scr.integration.components.SimpleComponent in project felix by apache.
the class Felix4350Test method doTest.
protected void doTest(String componentName) throws Exception {
ServiceRegistration dep1Reg = register(new SimpleComponent(), 0);
ServiceRegistration dep2Reg = register(new SimpleComponent2(), 1000);
final ComponentDescriptionDTO main = findComponentDescriptorByName(componentName);
TestCase.assertNotNull(main);
// needs to be async
asyncEnable(main);
// dep2 getService has not yet returned
delay(300);
dep1Reg.unregister();
// dep2 getService has returned
delay(2000);
Felix4350Component.check(0, 0, false);
dep1Reg = register(new SimpleComponent(), 0);
delay(300);
Felix4350Component.check(1, 0, true);
// does not need to be asyncv??
disableAndCheck(main);
dep1Reg.unregister();
dep2Reg.unregister();
Felix4350Component.check(1, 1, false);
dep1Reg = register(new SimpleComponent(), 0);
dep2Reg = register(new SimpleComponent2(), 1000);
Felix4350Component.check(1, 1, false);
// needs to be async
asyncEnable(main);
delay(300);
dep1Reg.unregister();
delay(100);
dep1Reg = register(new SimpleComponent(), 0);
delay(2000);
// n.b. counts are cumulative
Felix4350Component.check(2, 1, true);
}
Aggregations