Search in sources :

Example 1 with SimpleComponent

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();
        }
    }
}
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 SimpleComponent

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

Example 3 with SimpleComponent

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());
}
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 4 with SimpleComponent

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

Example 5 with SimpleComponent

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);
}
Also used : ComponentDescriptionDTO(org.osgi.service.component.runtime.dto.ComponentDescriptionDTO) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleComponent2(org.apache.felix.scr.integration.components.SimpleComponent2) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)39 Test (org.junit.Test)33 SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)32 ComponentConfigurationDTO (org.osgi.service.component.runtime.dto.ComponentConfigurationDTO)23 ServiceReference (org.osgi.framework.ServiceReference)4 Hashtable (java.util.Hashtable)3 ComponentFactory (org.osgi.service.component.ComponentFactory)3 ComponentInstance (org.osgi.service.component.ComponentInstance)3 HashSet (java.util.HashSet)1 SimpleComponent2 (org.apache.felix.scr.integration.components.SimpleComponent2)1 Bundle (org.osgi.framework.Bundle)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 Configuration (org.osgi.service.cm.Configuration)1 ConfigurationPermission (org.osgi.service.cm.ConfigurationPermission)1 ComponentDescriptionDTO (org.osgi.service.component.runtime.dto.ComponentDescriptionDTO)1