Search in sources :

Example 1 with DependencyHandlerDescription

use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.

the class InheritanceTest method getDependency.

private DependencyDescription getDependency(Architecture arch, String id) {
    DependencyHandlerDescription hd = (DependencyHandlerDescription) arch.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
    Assert.assertNotNull(hd);
    for (DependencyDescription dd : hd.getDependencies()) {
        if (dd.getId().equals(id)) {
            return dd;
        }
    }
    Assert.fail("Dependency " + id + " not found");
    return null;
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription)

Example 2 with DependencyHandlerDescription

use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.

the class TestContextualFilters method testContextualFilterAndInstanceReconfiguration.

/**
 * This test check filter set using instance properties.
 * However the instance is reconfigured a couple of times to illustrate the different case:
 * - unset property
 * - property updated
 * - reconfiguration that does not impact the filter
 */
@Test
public void testContextualFilterAndInstanceReconfiguration() {
    Properties configuration = new Properties();
    Properties filters = new Properties();
    filters.put("foo", "(id=${instance.id})");
    configuration.put("requires.filters", filters);
    configuration.put("instance.id", 2);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
    assertTrue(instance.getState() == ComponentInstance.VALID);
    DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    DependencyDescription dependency = desc.getDependencies()[0];
    assertEquals("(id=2)", dependency.getFilter());
    // Reconfigure the instance.
    Properties newProps = new Properties();
    newProps.put("instance.id", "3");
    instance.reconfigure(newProps);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    dependency = desc.getDependencies()[0];
    assertEquals("(id=3)", dependency.getFilter());
    // Another reconfiguration (that does not affect the filters)
    newProps = new Properties();
    newProps.put("instance.id", "3");
    newProps.put("stuff", "stuff");
    instance.reconfigure(newProps);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    dependency = desc.getDependencies()[0];
    assertEquals("(id=3)", dependency.getFilter());
    // Yet another reconfiguration, un-setting instance.id
    newProps = new Properties();
    newProps.put("stuff", "stuff");
    instance.reconfigure(newProps);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    dependency = desc.getDependencies()[0];
    assertEquals("(id=${instance.id})", dependency.getFilter());
    // Finally another reconfiguration to build a fulfilled filter
    newProps = new Properties();
    newProps.put("instance.id", "1");
    newProps.put("stuff", "stuff");
    instance.reconfigure(newProps);
    assertTrue(instance.getState() == ComponentInstance.VALID);
    desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    dependency = desc.getDependencies()[0];
    assertEquals("(id=1)", dependency.getFilter());
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with DependencyHandlerDescription

use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.

the class TestContextualFilters method testContextualFilterWithUnsetSystemProperty.

@Test
public void testContextualFilterWithUnsetSystemProperty() {
    // Just to be sure.
    System.clearProperty("env.id");
    Properties configuration = new Properties();
    Properties filters = new Properties();
    filters.put("foo", "(id=${env.id})");
    configuration.put("requires.filters", filters);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    DependencyDescription dependency = desc.getDependencies()[0];
    assertEquals("(id=${env.id})", dependency.getFilter());
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with DependencyHandlerDescription

use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.

the class TestContextualFiltersAndExternalSources method testContextualFilterUsingOneSourceAppearingLater.

@Test
public void testContextualFilterUsingOneSourceAppearingLater() {
    MyContextSource source = new MyContextSource();
    source.set("source.id", 2);
    Properties configuration = new Properties();
    Properties filters = new Properties();
    filters.put("foo", "(id=${source.id})");
    configuration.put("requires.filters", filters);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    DependencyDescription dependency = desc.getDependencies()[0];
    assertEquals("(id=${source.id})", dependency.getFilter());
    registration = context.registerService(ContextSource.class.getName(), source, null);
    assertTrue(instance.getState() == ComponentInstance.VALID);
    desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    dependency = desc.getDependencies()[0];
    assertEquals("(id=2)", dependency.getFilter());
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Test(org.junit.Test)

Example 5 with DependencyHandlerDescription

use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.

the class TestContextualFiltersAndExternalSources method testContextualFilterUsingOneSourceWithReconfiguration.

@Test
public void testContextualFilterUsingOneSourceWithReconfiguration() {
    MyContextSource source = new MyContextSource();
    source.set("source.id", 2);
    registration = context.registerService(ContextSource.class.getName(), source, null);
    Properties configuration = new Properties();
    Properties filters = new Properties();
    filters.put("foo", "(id=${source.id})");
    configuration.put("requires.filters", filters);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
    assertTrue(instance.getState() == ComponentInstance.VALID);
    // Set id to 3 => INVALID
    source.set("source.id", 3);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    // Set id to null
    source.set("source.id", null);
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
    DependencyDescription dependency = desc.getDependencies()[0];
    assertEquals("(id=${source.id})", dependency.getFilter());
    // Register a new source.
    MyContextSource source2 = new MyContextSource();
    source2.set("source.id", 2);
    registration2 = context.registerService(ContextSource.class.getName(), source2, null);
    assertTrue(instance.getState() == ComponentInstance.VALID);
    // This new source disappear
    registration2.unregister();
    registration2 = null;
    assertTrue(instance.getState() == ComponentInstance.INVALID);
    source.set("source.id", 1);
    assertTrue(instance.getState() == ComponentInstance.VALID);
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Test(org.junit.Test)

Aggregations

DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)18 Test (org.junit.Test)16 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)14 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)9 Architecture (org.apache.felix.ipojo.architecture.Architecture)7 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)5 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)5 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)5 FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)5 ServiceReference (org.osgi.framework.ServiceReference)5 Properties (java.util.Properties)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)2 DependencyCallback (org.apache.felix.ipojo.handlers.dependency.DependencyCallback)2 Ignore (org.junit.Ignore)1