Search in sources :

Example 16 with FooService

use of org.apache.felix.ipojo.runtime.core.test.services.FooService in project felix by apache.

the class SetCheckService method getLong.

private long getLong() {
    long r = 0;
    Iterator it = fs.iterator();
    while (it.hasNext()) {
        r = r + ((FooService) it.next()).getLong();
    }
    return r;
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.test.services.FooService) Iterator(java.util.Iterator)

Example 17 with FooService

use of org.apache.felix.ipojo.runtime.core.test.services.FooService in project felix by apache.

the class SetCheckService method check.

public boolean check() {
    boolean r = fs.size() != 0;
    Iterator it = fs.iterator();
    while (it.hasNext()) {
        r = r & ((FooService) it.next()).foo();
    }
    return r;
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.test.services.FooService) Iterator(java.util.Iterator)

Example 18 with FooService

use of org.apache.felix.ipojo.runtime.core.test.services.FooService in project felix by apache.

the class TestDependencyArchitecture method testOptionalDependency.

@Test
public void testOptionalDependency() {
    ServiceReference arch_dep = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance2.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.VALID);
    // Check dependency handler invalidity
    DependencyHandlerDescription dhd = getDependencyDesc(id_dep);
    assertTrue("Check dependency handler invalidity", dhd.isValid());
    // Check dependency metadata
    assertEquals("Check dependency interface", dhd.getDependencies()[0].getInterface(), FooService.class.getName());
    assertEquals("Check dependency id", dhd.getDependencies()[0].getId(), "FooService");
    assertFalse("Check dependency cardinality", dhd.getDependencies()[0].isMultiple());
    assertTrue("Check dependency optionality", dhd.getDependencies()[0].isOptional());
    assertNull("Check dependency ref -1", dhd.getDependencies()[0].getServiceReferences());
    fooProvider1.start();
    ServiceReference arch_ps = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps);
    PrimitiveInstanceDescription id_ps = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_ps.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);
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance2.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_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
    ProvidedServiceHandlerDescription psh = getPSDesc(id_ps);
    assertEquals("Check POJO creation", id_ps.getCreatedObjects().length, 1);
    fooProvider1.stop();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertTrue("Check dependency handler invalidity", dhd.isValid());
    fooProvider1.start();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    dhd = getDependencyDesc(id_dep);
    arch_ps = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
    assertNotNull("Check architecture availability", arch_ps);
    // id_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id_ps.getState() == ComponentInstance.VALID);
    psh = getPSDesc(id_ps);
    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);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance2.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_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
    psh = getPSDesc(id_ps);
    fooProvider1.stop();
    // id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
    assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
    dhd = getDependencyDesc(id_dep);
    assertTrue("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) 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)

Example 19 with FooService

use of org.apache.felix.ipojo.runtime.core.test.services.FooService in project felix by apache.

the class TestDynamicPriority method testSimple.

@Test
public void testSimple() {
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance1.getInstanceName());
    assertNotNull("Check architecture availability", arch_ref);
    InstanceDescription id = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id.getState() == ComponentInstance.INVALID);
    fooProvider.start();
    fooProvider2.start();
    id = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) getContext().getService(cs_ref);
    // Check grade
    Integer grade = (Integer) cs.getProps().get("int");
    assertEquals("Check first grade", 1, grade.intValue());
    // Turn off the best provider.
    fooProvider.stop();
    id = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) getContext().getService(cs_ref);
    // Check grade
    grade = (Integer) cs.getProps().get("int");
    assertEquals("Check second grade", 0, grade.intValue());
    // Turn on the best provider.
    fooProvider.start();
    id = ((Architecture) getContext().getService(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) getContext().getService(cs_ref);
    // Check grade
    grade = (Integer) cs.getProps().get("int");
    assertEquals("Check third grade", 1, grade.intValue());
    // Increase the second provider grade.
    ServiceReference fs_ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), fooProvider2.getInstanceName());
    assertNotNull("Check foo service (2) reference", fs_ref);
    FooService fs = (FooService) getContext().getService(fs_ref);
    // Increase the grade (now = 2)
    fs.foo();
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) getContext().getService(cs_ref);
    // Check grade
    grade = (Integer) cs.getProps().get("int");
    assertEquals("Check fourth grade", 2, grade.intValue());
    // Increase the other provider grade.
    fs_ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), fooProvider.getInstanceName());
    assertNotNull("Check foo service (1) reference", fs_ref);
    fs = (FooService) getContext().getService(fs_ref);
    // (grade = 3)
    fs.foo();
    cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    cs = (CheckService) getContext().getService(cs_ref);
    // Check grade
    grade = (Integer) cs.getProps().get("int");
    assertEquals("Check fifth grade", 3, grade.intValue());
    id = null;
    cs = null;
    getContext().ungetService(arch_ref);
    getContext().ungetService(cs_ref);
    getContext().ungetService(fs_ref);
    fooProvider.stop();
    fooProvider2.stop();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.test.services.FooService) Architecture(org.apache.felix.ipojo.architecture.Architecture) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)19 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)15 Test (org.junit.Test)15 ServiceReference (org.osgi.framework.ServiceReference)15 Properties (java.util.Properties)13 Architecture (org.apache.felix.ipojo.architecture.Architecture)8 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)7 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)7 Iterator (java.util.Iterator)4 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)1 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)1 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)1