Search in sources :

Example 16 with SimpleComponent

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

the class ServiceChangedTest 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_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");
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNull(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);
    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.assertFalse(comp12.m_multiRef.contains(srv1));
    TestCase.assertTrue(comp12.m_multiRef.contains(srv2));
    TestCase.assertEquals(1, 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.assertTrue(comp12.m_multiRef.contains(srv1));
    TestCase.assertTrue(comp12.m_multiRef.contains(srv2));
    TestCase.assertEquals(2, comp12.m_multiRefBind);
    TestCase.assertEquals(0, comp12.m_multiRefUnbind);
    // make srv2 to not match, expect binding to srv1
    srv2.setFilterProperty("don't match");
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.contains(srv1));
    TestCase.assertFalse(comp12.m_multiRef.contains(srv2));
    TestCase.assertEquals(2, comp12.m_multiRefBind);
    TestCase.assertEquals(1, comp12.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 17 with SimpleComponent

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

the class ServiceComponentTest method test_SimpleComponent_service.

@Test
public void test_SimpleComponent_service() throws Exception {
    final String pid = "ServiceComponent";
    // one single component exists without configuration
    getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent instance = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(instance);
    // assert component properties (all !)
    TestCase.assertEquals("required", instance.getProperty("prop.public"));
    TestCase.assertEquals("private", instance.getProperty(".prop.private"));
    // get the service
    ServiceReference reference = bundleContext.getServiceReference("java.lang.Object");
    TestCase.assertNotNull(reference);
    try {
        TestCase.assertEquals(instance, bundleContext.getService(reference));
    } finally {
        bundleContext.ungetService(reference);
    }
    // check service properties
    TestCase.assertEquals("required", reference.getProperty("prop.public"));
    TestCase.assertNull(reference.getProperty(".prop.private"));
    // check property keys do not contain private keys
    for (String propKey : reference.getPropertyKeys()) {
        TestCase.assertTrue("Property key [" + propKey + "] must have at least one character and not start with a dot", propKey.length() > 0 && !propKey.startsWith("."));
    }
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 18 with SimpleComponent

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

the class TargetPropertyTest method checkTarget.

void checkTarget(String expected, final SimpleServiceImpl srv1) {
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertEquals("(value=" + expected + ")", comp10.getProperty("one.target"));
    TestCase.assertEquals(srv1, comp10.m_singleRef);
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent)

Example 19 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_optional_service.

@Test
public void test_SimpleComponent_dynamic_optional_configuration_with_optional_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 = "DynamicConfigurationComponentWithOptionalReference";
        deleteConfig(pid);
        delay();
        // optional ref missing --> component active
        ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
        TestCase.assertNotNull(SimpleComponent.INSTANCE);
        final SimpleComponent instance = SimpleComponent.INSTANCE;
        // dynamically configure without the correct target
        configure(pid);
        delay();
        // optional ref missing --> component active
        findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
        TestCase.assertEquals(instance, SimpleComponent.INSTANCE);
        TestCase.assertNull(SimpleComponent.INSTANCE.m_singleRef);
        // dynamically configure with correct target
        theConfig.put(targetProp, "(filterprop=" + filterProp + ")");
        configure(pid);
        delay();
        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);
        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 --> active
        theConfig.remove(targetProp);
        configure(pid);
        delay();
        // optional ref missing --> component active
        findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
        TestCase.assertEquals(instance, SimpleComponent.INSTANCE);
        TestCase.assertNull(SimpleComponent.INSTANCE.m_singleRef);
        deleteConfig(pid);
        delay();
        // optional ref missing --> component active
        findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
        TestCase.assertNotSame(instance, SimpleComponent.INSTANCE);
        TestCase.assertNull(SimpleComponent.INSTANCE.m_singleRef);
        disableAndCheck(cc);
        TestCase.assertNull(SimpleComponent.INSTANCE);
    } finally {
        // Thread.sleep( 60000 );
        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 20 with SimpleComponent

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

the class ComponentConfigurationTest method dynamicConfigTest.

private void dynamicConfigTest(final String pid, boolean pre13, boolean recreateOnDelete) throws Exception {
    Object pidWithout;
    Object pidWith;
    if (pre13) {
        pidWithout = pid + ".description";
        pidWith = pid;
    } else {
        pidWithout = pid + ".description";
        pidWith = Arrays.asList(new String[] { pid + ".description", pid });
    }
    deleteConfig(pid);
    delay();
    ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    TestCase.assertNull(SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    TestCase.assertEquals(pidWithout, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
    final SimpleComponent instance = SimpleComponent.INSTANCE;
    configure(pid);
    delay();
    findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(instance, SimpleComponent.INSTANCE);
    TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    TestCase.assertEquals(pidWith, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
    deleteConfig(pid);
    delay();
    findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
    if (recreateOnDelete) {
        TestCase.assertNotSame(instance, SimpleComponent.INSTANCE);
    } else {
        TestCase.assertSame(instance, SimpleComponent.INSTANCE);
    }
    TestCase.assertNull(SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    TestCase.assertEquals(pidWithout, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
    disableAndCheck(cc);
    TestCase.assertNull(SimpleComponent.INSTANCE);
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO)

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