use of org.osgi.service.component.ComponentFactory in project felix by apache.
the class ComponentFactoryTest method test_component_factory_reference.
@Test
public void test_component_factory_reference() throws Exception {
final String componentname = "factory.component.reference";
final String componentfactory = "factory.component.factory.reference";
SimpleServiceImpl.create(bundleContext, "ignored").setFilterProperty("ignored");
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
// register a service : filterprop=match
SimpleServiceImpl match = SimpleServiceImpl.create(bundleContext, "required").setFilterProperty("required");
delay();
TestCase.assertNull(SimpleComponent.INSTANCE);
final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
TestCase.assertEquals(1, SimpleComponent.INSTANCE.m_multiRef.size());
TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(match));
// 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);
// overwritten filterprop
Hashtable<String, String> propsNonMatch = new Hashtable<String, String>();
propsNonMatch.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
propsNonMatch.put("ref.target", "(filterprop=nomatch)");
ComponentFactory factory = getComponentFactory(componentfactory);
try {
factory.newInstance(propsNonMatch);
TestCase.fail("Missing reference must fail instance creation");
} catch (ComponentException ce) {
// expected
}
final SimpleServiceImpl noMatch = SimpleServiceImpl.create(bundleContext, "nomatch").setFilterProperty("nomatch");
delay();
final ComponentInstance instanceNonMatch = factory.newInstance(propsNonMatch);
TestCase.assertNotNull(instanceNonMatch);
TestCase.assertNotNull(instanceNonMatch.getInstance());
TestCase.assertEquals(SimpleComponent.INSTANCE, instanceNonMatch.getInstance());
TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME_FACTORY));
TestCase.assertEquals(1, SimpleComponent.INSTANCE.m_multiRef.size());
TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(noMatch));
// check registered components
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
match.getRegistration().unregister();
delay();
// check registered components (ComponentFactory aint no longer)
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// it has already been deactivated.... this should cause an exception?
noMatch.getRegistration().unregister();
delay();
// check registered components (ComponentFactory aint no longer)
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
// deactivated due to unsatisfied reference
TestCase.assertNull(instanceNonMatch.getInstance());
TestCase.assertNull(SimpleComponent.INSTANCE);
// Check that calling dispose on a deactivated instance has no effect
instanceNonMatch.dispose();
TestCase.assertNull(SimpleComponent.INSTANCE);
// SCR 112.12.6.2
TestCase.assertNull(instanceNonMatch.getInstance());
}
use of org.osgi.service.component.ComponentFactory in project felix by apache.
the class ComponentTestBase method createFactoryComponentInstance.
protected ComponentInstance createFactoryComponentInstance(final String componentfactory, Hashtable<String, String> props) throws InvalidSyntaxException {
final ComponentFactory factory = getComponentFactory(componentfactory);
final ComponentInstance instance = factory.newInstance(props);
TestCase.assertNotNull(instance);
TestCase.assertNotNull(instance.getInstance());
TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME_FACTORY));
return instance;
}
use of org.osgi.service.component.ComponentFactory in project felix by apache.
the class ComponentTestBase method getComponentFactory.
// component factory test helper methods
protected ComponentFactory getComponentFactory(final String componentfactory) throws InvalidSyntaxException {
final ServiceReference[] refs = bundleContext.getServiceReferences(ComponentFactory.class.getName(), "(" + ComponentConstants.COMPONENT_FACTORY + "=" + componentfactory + ")");
TestCase.assertNotNull(refs);
TestCase.assertEquals(1, refs.length);
final ComponentFactory factory = (ComponentFactory) bundleContext.getService(refs[0]);
TestCase.assertNotNull(factory);
return factory;
}
use of org.osgi.service.component.ComponentFactory 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.osgi.service.component.ComponentFactory in project felix by apache.
the class ConfigurationComponentFactoryTest method test_non_spec_component_factory_with_factory_configuration.
@Test
public void test_non_spec_component_factory_with_factory_configuration() throws Exception {
// this test is about non-standard behaviour of ComponentFactory services
final String componentname = "factory.component";
final String componentfactory = "factory.component.factory";
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
final ComponentFactory factory = getComponentFactory(componentfactory);
final String factoryConfigPid = createFactoryConfiguration(componentname, "?");
delay();
TestCase.assertNotNull(SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
// check registered components
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// modify the configuration
Configuration config = getConfigurationAdmin().getConfiguration(factoryConfigPid, "?");
Dictionary<String, Object> props = config.getProperties();
props.put(PROP_NAME, PROP_NAME_FACTORY);
config.update(props);
delay();
// ensure instance with new configuration
TestCase.assertNotNull(SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
// check registered components
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// disable the factory
disableAndCheck(componentname);
delay();
// enabled the factory, factory configuration results in component instance
getConfigurationsDisabledThenEnable(componentname, 1, ComponentConfigurationDTO.ACTIVE);
// delete the configuration
getConfigurationAdmin().getConfiguration(factoryConfigPid).delete();
delay();
// factory is enabled but instance has been removed
// check registered components
checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
}
Aggregations