Search in sources :

Example 36 with SimpleComponent

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

the class ServiceChangedTest method test_required_single_static.

@Test
public void test_required_single_static() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_required_single_static_target";
    getDisabledConfigurationAndEnable(name, 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 a service property
    srv1.update("srv1-modified");
    // no changes in bindings expected
    TestCase.assertEquals(srv1, comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp10.m_singleRefBind);
    TestCase.assertEquals(0, comp10.m_singleRefUnbind);
    // set target to not match any more -> deactivate this component
    srv1.setFilterProperty("don't match");
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp10.m_singleRefBind);
    TestCase.assertEquals(1, comp10.m_singleRefUnbind);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertEquals(srv2, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp12.m_singleRefBind);
    TestCase.assertEquals(0, comp12.m_singleRefUnbind);
    // make srv1 match again, expect not changes in bindings
    srv1.setFilterProperty("match");
    final SimpleComponent comp13 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertSame(comp12, comp13);
    TestCase.assertEquals(srv2, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp12.m_singleRefBind);
    TestCase.assertEquals(0, comp12.m_singleRefUnbind);
    // make srv2 to not match, expect binding to srv1
    srv2.setFilterProperty("don't match");
    // reactivation required
    delay();
    final SimpleComponent comp14 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp14);
    TestCase.assertNotSame(comp12, comp14);
    TestCase.assertNotSame(comp13, comp14);
    TestCase.assertEquals(srv1, comp14.m_singleRef);
    TestCase.assertTrue(comp14.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp14.m_singleRefBind);
    TestCase.assertEquals(0, comp14.m_singleRefUnbind);
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 37 with SimpleComponent

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

the class ServiceChangedTest method test_optional_multiple_static.

@Test
public void test_optional_multiple_static() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_optional_multiple_static_target";
    getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.contains(srv1));
    TestCase.assertEquals(1, comp10.m_multiRefBind);
    TestCase.assertEquals(0, comp10.m_multiRefUnbind);
    // update a service property
    srv1.update("srv1-modified");
    // no changes in bindings expected
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.contains(srv1));
    TestCase.assertEquals(1, comp10.m_multiRefBind);
    TestCase.assertEquals(0, comp10.m_multiRefUnbind);
    // set target to not match any more
    srv1.setFilterProperty("don't match");
    // async reactivation (for unbind)
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp11);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertFalse(comp10.m_multiRef.contains(srv1));
    TestCase.assertEquals(1, comp10.m_multiRefBind);
    TestCase.assertEquals(1, comp10.m_multiRefUnbind);
    TestCase.assertNull(comp11.m_singleRef);
    TestCase.assertFalse(comp11.m_multiRef.contains(srv1));
    TestCase.assertEquals(0, comp11.m_multiRefBind);
    TestCase.assertEquals(0, comp11.m_multiRefUnbind);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding (not expected for an optional static ref)
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertSame(comp11, comp12);
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertFalse(comp12.m_multiRef.contains(srv1));
    TestCase.assertFalse(comp12.m_multiRef.contains(srv2));
    TestCase.assertEquals(0, comp12.m_multiRefBind);
    TestCase.assertEquals(0, comp12.m_multiRefUnbind);
    // make srv1 match again, expect not changes in bindings
    srv1.setFilterProperty("match");
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertFalse(comp12.m_multiRef.contains(srv1));
    TestCase.assertFalse(comp12.m_multiRef.contains(srv2));
    TestCase.assertEquals(0, comp12.m_multiRefBind);
    TestCase.assertEquals(0, comp12.m_multiRefUnbind);
    // make srv2 to not match, expect binding to srv1
    srv2.setFilterProperty("don't match");
    // allow reactivation delay (for unbind/bind)
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp13 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp13);
    TestCase.assertSame(comp11, comp13);
    TestCase.assertSame(comp12, comp13);
    TestCase.assertNull(comp13.m_singleRef);
    TestCase.assertFalse(comp13.m_multiRef.contains(srv1));
    TestCase.assertFalse(comp13.m_multiRef.contains(srv2));
    TestCase.assertEquals(0, comp13.m_multiRefBind);
    TestCase.assertEquals(0, comp13.m_multiRefUnbind);
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 38 with SimpleComponent

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

the class ServiceChangedTest method test_required_single_dynamic.

@Test
public void test_required_single_dynamic() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_required_single_dynamic_target";
    getDisabledConfigurationAndEnable(name, 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 a service property
    srv1.update("srv1-modified");
    // no changes in bindings expected
    TestCase.assertEquals(srv1, comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp10.m_singleRefBind);
    TestCase.assertEquals(0, comp10.m_singleRefUnbind);
    // set target to not match any more -> deactivate this component
    srv1.setFilterProperty("don't match");
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp10.m_singleRefBind);
    TestCase.assertEquals(1, comp10.m_singleRefUnbind);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertEquals(srv2, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp12.m_singleRefBind);
    TestCase.assertEquals(0, comp12.m_singleRefUnbind);
    // make srv1 match again, expect not changes in bindings
    srv1.setFilterProperty("match");
    TestCase.assertEquals(srv2, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    TestCase.assertEquals(1, comp12.m_singleRefBind);
    TestCase.assertEquals(0, comp12.m_singleRefUnbind);
    // make srv2 to not match, expect binding to srv1
    srv2.setFilterProperty("don't match");
    TestCase.assertEquals(srv1, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    TestCase.assertEquals(2, comp12.m_singleRefBind);
    TestCase.assertEquals(1, comp12.m_singleRefUnbind);
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 39 with SimpleComponent

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

the class TargetedPIDTest method testTargetedPID.

@Test
public void testTargetedPID() throws Exception {
    try {
        new ConfigurationPermission(REGION, ConfigurationPermission.TARGET);
    } catch (IllegalArgumentException e) {
        // not an R5 CA
        return;
    }
    String pid = COMPONENT_NAME;
    theConfig.put(TARGETED_PID, pid);
    Configuration config = configure(pid);
    config.setBundleLocation(REGION);
    String pidSN = pid + "|simplecomponent2";
    theConfig.put(TARGETED_PID, pidSN);
    Configuration configSN = configure(pidSN);
    configSN.setBundleLocation(REGION);
    String pidSNV = pidSN + "|0.0.12";
    theConfig.put(TARGETED_PID, pidSNV);
    Configuration configSNV = configure(pidSNV);
    configSNV.setBundleLocation(REGION);
    String pidSNVL = pidSNV + "|bundleLocation";
    theConfig.put(TARGETED_PID, pidSNVL);
    Configuration configSNVL = configure(pidSNVL);
    configSNVL.setBundleLocation(REGION);
    delay();
    // Add more and more specific components to check that they pick up the appropriate configuration
    Set<ComponentConfigurationDTO> known = new HashSet<ComponentConfigurationDTO>();
    final ComponentConfigurationDTO component = findComponentConfigurationByName(COMPONENT_NAME, ComponentConfigurationDTO.ACTIVE);
    known.add(component);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    SimpleComponent sc = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pid, sc.getProperty(TARGETED_PID));
    Bundle bSN = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
    bSN.start();
    findComponentConfigurationByName(bSN, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSN = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSN, scSN.getProperty(TARGETED_PID));
    Bundle bSNV = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.12", null);
    bSNV.start();
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSNV = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSNV, scSNV.getProperty(TARGETED_PID));
    Bundle bSNVL = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.12", "bundleLocation");
    bSNVL.start();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSNVL = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSNVL, scSNVL.getProperty(TARGETED_PID));
    // remove configurations to check that the components now use the less specific configurations.
    configSNVL.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSNV, scSNVL.getProperty(TARGETED_PID));
    configSNV.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSN, scSNVL.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSN, scSNV.getProperty(TARGETED_PID));
    configSN.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSNVL.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSNV.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSN, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSN.getProperty(TARGETED_PID));
}
Also used : Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) ConfigurationPermission(org.osgi.service.cm.ConfigurationPermission) HashSet(java.util.HashSet) 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