use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class ComponentFactoryTest method testConfiguredFactory.
private ComponentInstance testConfiguredFactory(final String componentname, final String componentfactory, boolean optional, boolean expectComponent) throws InvocationTargetException, InterruptedException, InvalidSyntaxException {
// ensure there is no configuration for the component
deleteConfig(componentname);
delay();
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
// At this point, since we don't have created the configuration, then the ComponentFactory
// should not be available.
checkFactory(componentfactory, optional);
// supply configuration now and ensure active
configure(componentname);
delay();
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNull(SimpleComponent.INSTANCE);
// get the component factory service
final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
final Object instanceObject = instance.getInstance();
TestCase.assertNotNull(instanceObject);
TestCase.assertEquals(SimpleComponent.INSTANCE, instanceObject);
TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME_FACTORY));
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// delete config, ensure factory is not active anymore and component instance gone
// (configuration required >> dispose of instance. Also for pre-1.3 components, removing config unconditionally
// deactivates component.
deleteConfig(componentname);
delay();
checkFactory(componentfactory, optional);
if (expectComponent) {
TestCase.assertNotNull(instance.getInstance());
TestCase.assertNotNull(SimpleComponent.INSTANCE);
// with removal of the factory, the created instance should also be removed
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
} else {
TestCase.assertNull(instance.getInstance());
TestCase.assertNull(SimpleComponent.INSTANCE);
// with removal of the factory, the created instance should also be removed
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
}
return instance;
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class ComponentFactoryTest method test_component_factory.
@Test
public void test_component_factory() throws Exception {
final String componentname = "factory.component";
final String componentfactory = "factory.component.factory";
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
// check registered components
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
instance.dispose();
TestCase.assertNull(SimpleComponent.INSTANCE);
// SCR 112.12.6.2
TestCase.assertNull(instance.getInstance());
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class ComponentFactoryTest method test_component_factory_referredTo.
@Test
public void test_component_factory_referredTo() throws Exception {
// set up the component that refers to the service the factory will create.
final String referringComponentName = "ComponentReferringToFactoryObject";
getConfigurationsDisabledThenEnable(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
final String componentname = "factory.component.referred";
final String componentfactory = "factory.component.factory.referred";
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("service.pid", "myFactoryInstance");
final ComponentFactory factory = getComponentFactory(componentfactory);
final ComponentInstance instance = factory.newInstance(props);
TestCase.assertNotNull(instance);
TestCase.assertNotNull(instance.getInstance());
TestCase.assertTrue(instance.getInstance() instanceof SimpleService);
// The referring service should now be active
checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.ACTIVE);
instance.dispose();
// SCR 112.12.6.2
TestCase.assertNull(instance.getInstance());
// make sure it's unsatisfied (service is no longer available)
checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class Felix5356Test method do_test_factory_component_with_factory_configuration.
private void do_test_factory_component_with_factory_configuration(boolean createConfigBeforeEnable) throws Exception {
final String componentname = "factory.component.referred";
final String componentfactory = "factory.component.factory.referred";
String factoryConfigPid = null;
if (createConfigBeforeEnable) {
// create the factory configuration before enabling
factoryConfigPid = createFactoryConfiguration(componentname, "?");
delay();
}
getConfigurationsDisabledThenEnable(componentname, 0, -1);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("service.pid", "myFactoryInstance");
final ComponentFactory factory = getComponentFactory(componentfactory);
final ComponentInstance instance = factory.newInstance(props);
TestCase.assertNotNull(instance);
TestCase.assertNotNull(instance.getInstance());
TestCase.assertTrue(instance.getInstance() instanceof SimpleService);
// check registered components
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// check registered service
findServices(SimpleService.class.getName(), null, 1);
if (!createConfigBeforeEnable) {
factoryConfigPid = createFactoryConfiguration(componentname, "?");
delay();
// check registered components, again should only be 1
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// check registered service
findServices(SimpleService.class.getName(), null, 1);
TestCase.assertNotNull(instance.getInstance());
}
// delete the factory config
getConfigurationAdmin().getConfiguration(factoryConfigPid).delete();
delay();
// check registered components, again should only be 1
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
instance.dispose();
// check registered components
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class PersistentComponentFactoryTest method testConfiguredFactory.
private ComponentInstance testConfiguredFactory(final String componentname, final String componentfactory, boolean optional, boolean expectComponent) throws InvocationTargetException, InterruptedException, InvalidSyntaxException {
// ensure there is no configuration for the component
deleteConfig(componentname);
delay();
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
// At this point, since we don't have created the configuration, then the ComponentFactory
// should not be available.
checkFactory(componentfactory, optional);
// supply configuration now and ensure active
configure(componentname);
delay();
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNull(SimpleComponent.INSTANCE);
// get the component factory service
final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
final Object instanceObject = instance.getInstance();
TestCase.assertNotNull(instanceObject);
TestCase.assertEquals(SimpleComponent.INSTANCE, instanceObject);
TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME_FACTORY));
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// delete config, ensure factory is not active anymore and component instance gone
// (configuration required >> dispose of instance. Also for pre-1.3 components, removing config unconditionally
// deactivates component.
deleteConfig(componentname);
delay();
checkFactory(componentfactory, optional);
if (expectComponent) {
TestCase.assertNotNull(instance.getInstance());
TestCase.assertNotNull(SimpleComponent.INSTANCE);
// with removal of the factory, the created instance should also be removed
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
} else {
TestCase.assertNull(instance.getInstance());
TestCase.assertNull(SimpleComponent.INSTANCE);
// with removal of the factory, the created instance should also be removed
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
}
return instance;
}
Aggregations