Search in sources :

Example 11 with DependencyDescription

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

the class InheritanceTest method testArchitecture.

@Test
public void testArchitecture() {
    if (isKnopflerfish()) {
        System.out.println("Test disabled on knopflerfish");
        return;
    }
    osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=d)", 10000);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "d");
    Assert.assertNotNull(ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(ref);
    System.out.println(arch.getInstanceDescription().getDescription());
    Assert.assertEquals(ComponentInstance.VALID, arch.getInstanceDescription().getState());
    DependencyDescription dd = getDependency(arch, "org.apache.felix.ipojo.runtime.core.components.inheritance.b.IB");
    Assert.assertTrue(!dd.getServiceReferences().isEmpty());
    ServiceReference dref = (ServiceReference) dd.getServiceReferences().get(0);
    Assert.assertEquals(dref.getBundle().getSymbolicName(), "C");
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 12 with DependencyDescription

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

the class TestContextualFilters method testContextualFilterWithInstanceProperty.

@Test
public void testContextualFilterWithInstanceProperty() {
    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());
}
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 13 with DependencyDescription

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

the class TestContextualFilters method testContextualFilterWithSystemProperty.

@Test
public void testContextualFilterWithSystemProperty() {
    // Set the system property.
    System.setProperty("env.id", "2");
    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.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());
}
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 14 with DependencyDescription

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

the class TestContextualFiltersAndExternalSources method testContextualFilterUsingOneSource.

@Test
public void testContextualFilterUsingOneSource() {
    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);
    DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
    // Only one dependency.
    DependencyDescription 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 15 with DependencyDescription

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

the class TestDependencyArchitecture method testMultipleDependency.

@Test
public void testMultipleDependency() {
    ServiceReference arch_dep = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance3.getInstanceName());
    assertNotNull("Check architecture availability", arch_dep);
    PrimitiveInstanceDescription id_dep = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_dep.getState() == ComponentInstance.INVALID);
    // Check dependency handler invalidity
    DependencyHandlerDescription dhd = getDependencyDesc(id_dep);
    DependencyDescription dd = getDependencyDescBySpecification(id_dep, FooService.class.getName());
    assertFalse("Check dependency handler invalidity", dhd.isValid());
    assertTrue("Check dependency invalidity", dd.getState() == Dependency.UNRESOLVED);
    // Check dependency metadata
    assertEquals("Check dependency interface", dhd.getDependencies()[0].getInterface(), FooService.class.getName());
    assertTrue("Check dependency cardinality", dhd.getDependencies()[0].isMultiple());
    assertFalse("Check dependency optionality", dhd.getDependencies()[0].isOptional());
    assertNull("Check dependency ref -1", dhd.getDependencies()[0].getServiceReferences());
    assertEquals("Check dependency interface", dd.getSpecification(), FooService.class.getName());
    assertTrue("Check dependency cardinality", dd.isMultiple());
    assertFalse("Check dependency optionality", dd.isOptional());
    assertNull("Check dependency ref -1", dd.getServiceReferences());
    assertFalse("Check dependency proxy", dhd.getDependencies()[0].isProxy());
    fooProvider1.start();
    ServiceReference arch_ps1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps1);
    PrimitiveInstanceDescription id_ps1 = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id_ps1.getState() == ComponentInstance.VALID);
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertTrue("Check dependency handler validity", dhd.isValid());
    assertEquals("Check dependency ref - 2 ", dhd.getDependencies()[0].getServiceReferences().size(), 1);
    assertEquals("Check used ref - 1 (" + dhd.getDependencies()[0].getUsedServices().size() + ")", dhd.getDependencies()[0].getUsedServices().size(), 0);
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Check object graph
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    ProvidedServiceHandlerDescription psh = getPSDesc(id_ps1);
    assertEquals("Check POJO creation", id_ps1.getCreatedObjects().length, 1);
    // Start a second foo service provider
    fooProvider2.start();
    arch_ps1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
    ServiceReference arch_ps2 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider2.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps1);
    assertNotNull("Check architecture 2 availability", arch_ps2);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    PrimitiveInstanceDescription id_ps2 = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ps2)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_ps1.getState() == ComponentInstance.VALID);
    assertTrue("Check instance 2 invalidity - 1", id_ps2.getState() == ComponentInstance.VALID);
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertTrue("Check dependency handler validity", dhd.isValid());
    assertEquals("Check dependency ref - 3 ", dhd.getDependencies()[0].getServiceReferences().size(), 2);
    // provider 2 not already used
    assertEquals("Check used ref - 2 ", dhd.getDependencies()[0].getUsedServices().size(), 1);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Check object graph
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    // id_ps2 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    ProvidedServiceHandlerDescription psh1 = getPSDesc(id_ps1);
    ProvidedServiceHandlerDescription psh2 = getPSDesc(id_ps2);
    assertEquals("Check POJO creation", id_ps1.getCreatedObjects().length, 1);
    assertEquals("Check POJO creation", id_ps2.getCreatedObjects().length, 1);
    assertEquals("Check used ref - 3 (" + dhd.getDependencies()[0].getUsedServices().size() + ")", dhd.getDependencies()[0].getUsedServices().size(), 2);
    fooProvider2.stop();
    arch_ps1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps1);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id_ps1.getState() == ComponentInstance.VALID);
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertTrue("Check dependency handler validity", dhd.isValid());
    assertEquals("Check dependency ref - 2 ", dhd.getDependencies()[0].getServiceReferences().size(), 1);
    assertEquals("Check used ref - 4 ", dhd.getDependencies()[0].getUsedServices().size(), 1);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Check object graph
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    psh = getPSDesc(id_ps1);
    assertEquals("Check POJO creation", id_ps1.getCreatedObjects().length, 1);
    assertEquals("Check used ref - 5 ", dhd.getDependencies()[0].getUsedServices().size(), 1);
    fooProvider1.stop();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertFalse("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertFalse("Check dependency handler invalidity", dhd.isValid());
    fooProvider2.start();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    arch_ps2 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider2.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps1);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_ps2.getState() == ComponentInstance.VALID);
    psh = getPSDesc(id_ps2);
    assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
    assertTrue("Check dependency handler validity", dhd.isValid());
    assertEquals("Check dependency ref -3", dhd.getDependencies()[0].getServiceReferences().size(), 1);
    assertEquals("Check used ref - 6 ", dhd.getDependencies()[0].getUsedServices().size(), 0);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    assertTrue("check CheckService invocation", cs.check());
    // Check object graph
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    // id_ps1 = ((Architecture) osgiHelper.getRawServiceObject(arch_ps1)).getInstanceDescription();
    psh = getPSDesc(id_ps2);
    assertEquals("Check used ref - 7 ", dhd.getDependencies()[0].getUsedServices().size(), 1);
    fooProvider2.stop();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertFalse("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertFalse("Check dependency handler invalidity", dhd.isValid());
    id_dep = null;
    cs = null;
    getContext().ungetService(arch_dep);
    getContext().ungetService(cs_ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.test.services.FooService) DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

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