Search in sources :

Example 21 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_service2.

/**
 * FELIX-3902.  Start with filter matching two services, remove one, then change the filter
 * to (still) match the other one.  2nd service should remain bound.
 */
@Test
public void test_SimpleComponent_dynamic_optional_configuration_with_required_service2() throws Exception {
    final String targetProp = "ref.target";
    final String filterProp1 = "one";
    final String filterProp2 = "two";
    final SimpleServiceImpl service1 = SimpleServiceImpl.create(bundleContext, "one", 1).setFilterProperty(filterProp1);
    final SimpleServiceImpl service2 = SimpleServiceImpl.create(bundleContext, "two", 2).setFilterProperty(filterProp2);
    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=" + filterProp1 + ")(filterprop=" + filterProp2 + "))");
        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();
        // remove higher ranked service
        if (service2 != null) {
            service2.drop();
        }
        // 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 with new filter --> active
        theConfig.put(targetProp, "(filterprop=" + filterProp1 + ")");
        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);
        deleteConfig(pid);
        delay();
        // mandatory ref missing --> component unsatisfied
        findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
        disableAndCheck(cc);
        TestCase.assertNull(SimpleComponent.INSTANCE);
    } finally {
        theConfig.remove(targetProp);
        if (service1 != null) {
            service1.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 22 with SimpleComponent

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

the class ComponentDisposeTest method test_SimpleComponent_factory_configuration.

@Test
public void test_SimpleComponent_factory_configuration() throws Exception {
    final String factoryPid = "FactoryConfigurationComponent";
    deleteFactoryConfigurations(factoryPid);
    delay();
    // there should be none
    getConfigurationsDisabledThenEnable(factoryPid, 0, ComponentConfigurationDTO.ACTIVE);
    // create two factory configurations expecting two components
    final String pid0 = createFactoryConfiguration(factoryPid, "?");
    final String pid1 = createFactoryConfiguration(factoryPid, "?");
    delay();
    Collection<ComponentConfigurationDTO> ccs = findComponentConfigurationsByName(factoryPid, ComponentConfigurationDTO.ACTIVE);
    Assert.assertEquals(2, ccs.size());
    // expect two components, only first is active, second is disabled
    TestCase.assertEquals(2, SimpleComponent.INSTANCES.size());
    for (ComponentConfigurationDTO cc : ccs) {
        TestCase.assertTrue(SimpleComponent.INSTANCES.containsKey(cc.id));
    }
    // dispose an instance
    final SimpleComponent anInstance = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(anInstance);
    TestCase.assertNotNull(anInstance.m_activateContext);
    anInstance.m_activateContext.getComponentInstance().dispose();
    delay();
    // expect one component
    ComponentConfigurationDTO cc = findComponentConfigurationByName(factoryPid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(1, SimpleComponent.INSTANCES.size());
    TestCase.assertTrue(SimpleComponent.INSTANCES.containsKey(cc.id));
    final SimpleComponent instance = SimpleComponent.INSTANCES.values().iterator().next();
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) Test(org.junit.Test)

Example 23 with SimpleComponent

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

the class ConfigurationChangeTest method singleTest.

private void singleTest(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);
    // all cm event to complete
    delay();
    getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
    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) {
        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 : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl)

Example 24 with SimpleComponent

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

the class ServiceBindGreedyTest method test_required_multiple_dynamic.

@Test
public void test_required_multiple_dynamic() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_required_multiple_dynamic";
    ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.contains(srv1));
    srv1.drop();
    // no delay, should be immediate
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNull(comp11);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.contains(srv2));
    disableAndCheck(cc);
    // async disabling
    delay();
    final SimpleServiceImpl srv3 = SimpleServiceImpl.create(bundleContext, "srv3");
    // enable component with two services available, expect both bind
    // async enabling
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp20 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp20);
    TestCase.assertNotSame(comp10, comp20);
    TestCase.assertNull(comp20.m_singleRef);
    TestCase.assertTrue(comp20.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp20.m_multiRef.contains(srv3));
    srv2.drop();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp21 = SimpleComponent.INSTANCE;
    TestCase.assertSame(comp20, comp21);
    TestCase.assertNull(comp21.m_singleRef);
    TestCase.assertFalse(comp21.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp21.m_multiRef.contains(srv3));
    // create srv4, expect bind
    final SimpleServiceImpl srv4 = SimpleServiceImpl.create(bundleContext, "srv4");
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp22 = SimpleComponent.INSTANCE;
    TestCase.assertSame(comp20, comp22);
    TestCase.assertNull(comp22.m_singleRef);
    TestCase.assertFalse(comp22.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv3));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv4));
    // drop srv4 again, expect no rebind
    srv4.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp23 = SimpleComponent.INSTANCE;
    TestCase.assertSame(comp20, comp23);
    TestCase.assertNull(comp23.m_singleRef);
    TestCase.assertFalse(comp23.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp23.m_multiRef.contains(srv3));
    TestCase.assertFalse(comp23.m_multiRef.contains(srv4));
    // "reset"
    disableAndCheck(cc);
    srv3.drop();
    delay();
    // two services with service ranking (srv6 > srv5)
    final SimpleServiceImpl srv5 = SimpleServiceImpl.create(bundleContext, "srv5", 10);
    final SimpleServiceImpl srv6 = SimpleServiceImpl.create(bundleContext, "srv6", 20);
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp30 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp30);
    TestCase.assertNull(comp30.m_singleRef);
    TestCase.assertTrue(comp30.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp30.m_multiRef.contains(srv6));
    // another service with higher ranking -- no rebind !
    final SimpleServiceImpl srv7 = SimpleServiceImpl.create(bundleContext, "srv7", 30);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp31 = SimpleComponent.INSTANCE;
    TestCase.assertSame(comp30, comp31);
    TestCase.assertNull(comp31.m_singleRef);
    TestCase.assertTrue(comp31.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv7));
    // srv6 goes, rebind to srv7
    srv6.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp32 = SimpleComponent.INSTANCE;
    TestCase.assertSame(comp30, comp32);
    TestCase.assertNull(comp32.m_singleRef);
    TestCase.assertTrue(comp32.m_multiRef.contains(srv5));
    TestCase.assertFalse(comp32.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp32.m_multiRef.contains(srv7));
}
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 25 with SimpleComponent

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

the class ServiceBindGreedyTest method test_required_multiple_static.

@Test
public void test_required_multiple_static() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_required_multiple_static";
    ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.contains(srv1));
    srv1.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNull(comp11);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.contains(srv2));
    disableAndCheck(cc);
    // async disabling
    delay();
    final SimpleServiceImpl srv3 = SimpleServiceImpl.create(bundleContext, "srv3");
    // enable component with two services available, expect both bind
    // async enabling
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp20 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp20);
    TestCase.assertNotSame(comp10, comp20);
    TestCase.assertNotSame(comp12, comp20);
    TestCase.assertNull(comp20.m_singleRef);
    TestCase.assertTrue(comp20.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp20.m_multiRef.contains(srv3));
    srv2.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp21 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp21);
    TestCase.assertNull(comp21.m_singleRef);
    TestCase.assertFalse(comp21.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp21.m_multiRef.contains(srv3));
    // create srv4, expect greedy bind
    final SimpleServiceImpl srv4 = SimpleServiceImpl.create(bundleContext, "srv4");
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp22 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp22);
    TestCase.assertNotSame(comp21, comp22);
    TestCase.assertNull(comp22.m_singleRef);
    TestCase.assertFalse(comp22.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv3));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv4));
    // drop srv4 again, expect greedy rebind
    srv4.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp23 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp23);
    TestCase.assertNotSame(comp21, comp23);
    TestCase.assertNotSame(comp22, comp23);
    TestCase.assertNull(comp23.m_singleRef);
    TestCase.assertFalse(comp23.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp23.m_multiRef.contains(srv3));
    TestCase.assertFalse(comp23.m_multiRef.contains(srv4));
    // "reset"
    disableAndCheck(cc);
    srv3.drop();
    delay();
    // two services with service ranking (srv6 > srv5)
    final SimpleServiceImpl srv5 = SimpleServiceImpl.create(bundleContext, "srv5", 10);
    final SimpleServiceImpl srv6 = SimpleServiceImpl.create(bundleContext, "srv6", 20);
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp30 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp23, comp30);
    TestCase.assertNull(comp30.m_singleRef);
    TestCase.assertTrue(comp30.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp30.m_multiRef.contains(srv6));
    // another service with higher ranking -- greedy rebind !
    final SimpleServiceImpl srv7 = SimpleServiceImpl.create(bundleContext, "srv7", 30);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp31 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp31);
    TestCase.assertNull(comp31.m_singleRef);
    TestCase.assertTrue(comp31.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv7));
    // srv6 goes, rebind to srv7
    srv6.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp32 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp32);
    TestCase.assertNotSame(comp31, comp32);
    TestCase.assertNull(comp32.m_singleRef);
    TestCase.assertTrue(comp32.m_multiRef.contains(srv5));
    TestCase.assertFalse(comp32.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp32.m_multiRef.contains(srv7));
}
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)

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