Search in sources :

Example 1 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl 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();
        }
    }
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 2 with SimpleServiceImpl

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

Example 3 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class ComponentFactoryTest method test_component_factory_with_target_filters.

@Test
public void test_component_factory_with_target_filters() throws Exception {
    final String componentfactory = "factory.component.reference.targetfilter";
    getConfigurationsDisabledThenEnable(componentfactory, 0, -1);
    SimpleServiceImpl s1 = SimpleServiceImpl.create(bundleContext, "service1");
    SimpleServiceImpl s2 = SimpleServiceImpl.create(bundleContext, "service2");
    // supply configuration now and ensure active
    configure(componentfactory);
    delay();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    props.put("ref.target", "(value=service2)");
    final ComponentInstance instance = createFactoryComponentInstance(componentfactory, props);
    log.log(LogService.LOG_WARNING, "Bound Services: " + SimpleComponent.INSTANCE.m_multiRef);
    TestCase.assertFalse(SimpleComponent.INSTANCE.m_multiRef.contains(s1));
    TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(s2));
    instance.dispose();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // SCR 112.12.6.2
    TestCase.assertNull(instance.getInstance());
    s2.drop();
    s1.drop();
}
Also used : Hashtable(java.util.Hashtable) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 4 with SimpleServiceImpl

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

Example 5 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl 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());
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl)

Aggregations

SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)42 Test (org.junit.Test)36 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)32 ComponentConfigurationDTO (org.osgi.service.component.runtime.dto.ComponentConfigurationDTO)22 Hashtable (java.util.Hashtable)7 ComponentInstance (org.osgi.service.component.ComponentInstance)7 ComponentFactory (org.osgi.service.component.ComponentFactory)5 ServiceReference (org.osgi.framework.ServiceReference)4 SimpleComponent2 (org.apache.felix.scr.integration.components.SimpleComponent2)2 SimpleService2Impl (org.apache.felix.scr.integration.components.SimpleService2Impl)2 ArrayList (java.util.ArrayList)1 ActivatorComponent (org.apache.felix.scr.integration.components.ActivatorComponent)1 MutatingService (org.apache.felix.scr.integration.components.MutatingService)1 ComponentException (org.osgi.service.component.ComponentException)1