Search in sources :

Example 6 with ComponentFactory

use of org.osgi.service.component.ComponentFactory in project felix by apache.

the class PersistentComponentFactoryTest 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);
    // works even without required reference
    final ComponentInstance instanceNonMatch = factory.newInstance(propsNonMatch);
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleServiceImpl noMatch = SimpleServiceImpl.create(bundleContext, "nomatch").setFilterProperty("nomatch");
    delay();
    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 is still present)
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
    // it has already been deactivated.... this should cause an exception?
    noMatch.getRegistration().unregister();
    delay();
    // check registered components (ComponentFactory is still present)
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // 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());
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 7 with ComponentFactory

use of org.osgi.service.component.ComponentFactory in project felix by apache.

the class PersistentComponentFactoryTest method test_component_factory_newInstance_failure.

@Test
public void test_component_factory_newInstance_failure() throws Exception {
    final String componentname = "factory.component";
    final String componentfactory = "factory.component.factory";
    getConfigurationsDisabledThenEnable(componentname, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    props.put(SimpleComponent.PROP_ACTIVATE_FAILURE, "Requested Failure");
    final ComponentFactory factory = getComponentFactory(componentfactory);
    final ComponentInstance instance = factory.newInstance(props);
    TestCase.assertNotNull(instance);
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.SATISFIED);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) Test(org.junit.Test)

Example 8 with ComponentFactory

use of org.osgi.service.component.ComponentFactory 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);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleService(org.apache.felix.scr.integration.components.SimpleService) Test(org.junit.Test)

Example 9 with ComponentFactory

use of org.osgi.service.component.ComponentFactory 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);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleService(org.apache.felix.scr.integration.components.SimpleService)

Example 10 with ComponentFactory

use of org.osgi.service.component.ComponentFactory in project felix by apache.

the class PersistentComponentFactoryTest 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);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleService(org.apache.felix.scr.integration.components.SimpleService) Test(org.junit.Test)

Aggregations

ComponentFactory (org.osgi.service.component.ComponentFactory)12 ComponentInstance (org.osgi.service.component.ComponentInstance)10 Hashtable (java.util.Hashtable)9 Test (org.junit.Test)8 SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)5 ServiceReference (org.osgi.framework.ServiceReference)4 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)3 SimpleService (org.apache.felix.scr.integration.components.SimpleService)3 Configuration (org.osgi.service.cm.Configuration)1 ComponentException (org.osgi.service.component.ComponentException)1