Search in sources :

Example 6 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestDelayedOptionalMultipleImport method testSimple.

@Test
public void testSimple() {
    import4.start();
    // Two providers
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import4);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 2);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    fs = (FooService) sc.getService(refs[1]);
    assertTrue("Test foo invocation (2)", fs.foo());
    sc.ungetService(refs[1]);
    foo1.stop();
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    // Stop the second provider
    foo2.stop();
    assertTrue("Test component validity - 2", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertEquals("Test foo availability inside the composite - 1", refs.length, 0);
    foo2.start();
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 7 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestDelayedOptionalMultipleImport method testSimple2.

@Test
public void testSimple2() {
    import4.start();
    // Two providers
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import4);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 2);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    fs = (FooService) sc.getService(refs[1]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[1]);
    foo2.stop();
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    // Stop the second provider
    foo1.stop();
    assertTrue("Test component validity - 2", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertEquals("Test foo availability inside the composite - 1", refs.length, 0);
    foo1.start();
    assertTrue("Test component validity", import4.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import4);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 8 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestFilteredImport method testSimple2.

@Test
@Ignore("known as broken after interception inception in service dependencies")
public void testSimple2() {
    import1.start();
    // Two providers
    assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import1);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    foo2.stop();
    assertTrue("Test component invalidity - 1", import1.getState() == ComponentInstance.INVALID);
    // Stop the second provider
    foo1.stop();
    assertTrue("Test component invalidity - 2", import1.getState() == ComponentInstance.INVALID);
    foo1.start();
    assertTrue("Test component invalidity - 3", import1.getState() == ComponentInstance.INVALID);
    foo2.start();
    assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import1);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ServiceReference(org.osgi.framework.ServiceReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestMultipleImport method testSimple.

@Test
public void testSimple() {
    // No provider -> Invalid
    assertTrue("Test component invalidity", import2.getState() == ComponentInstance.INVALID);
    ComponentInstance foo = null;
    Properties p = new Properties();
    p.put("instance.name", "foo");
    try {
        foo = fooProvider.createComponentInstance(p);
    } catch (Exception e) {
        fail("Fail to instantiate the foo component " + e.getMessage());
    }
    ComponentInstance foo2 = null;
    Properties p2 = new Properties();
    p2.put("instance.name", "foo2");
    try {
        foo2 = fooProvider.createComponentInstance(p2);
    } catch (Exception e) {
        fail("Fail to instantiate the foo2 component " + e.getMessage());
    }
    // The foo service is available => import1 must be valid
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import2);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2 (" + refs.length + ")", refs.length, 2);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    FooService fs2 = (FooService) sc.getService(refs[1]);
    assertTrue("Test foo invocation", fs2.foo());
    sc.ungetService(refs[0]);
    sc.ungetService(refs[1]);
    // Stop the second provider
    foo2.dispose();
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 2", refs);
    assertEquals("Test foo availability inside the composite - 2.1 (" + refs.length + ")", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    // stop the foo provider
    foo.stop();
    // No provider -> Invalid
    assertTrue("Test component invalidity - 2", import2.getState() == ComponentInstance.INVALID);
    foo.start();
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    foo.dispose();
    // No provider -> Invalid
    assertTrue("Test component invalidity - 3", import2.getState() == ComponentInstance.INVALID);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 10 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestMultipleImport method testSimple2.

@Test
public void testSimple2() {
    // No provider -> Invalid
    assertTrue("Test component invalidity", import2.getState() == ComponentInstance.INVALID);
    ComponentInstance foo1 = null;
    Properties p = new Properties();
    p.put("instance.name", "foo");
    try {
        foo1 = fooProvider.createComponentInstance(p);
    } catch (Exception e) {
        fail("Fail to instantiate the foo component " + e.getMessage());
    }
    ComponentInstance foo2 = null;
    Properties p2 = new Properties();
    p2.put("instance.name", "foo2");
    try {
        foo2 = fooProvider.createComponentInstance(p2);
    } catch (Exception e) {
        fail("Fail to instantiate the foo2 component " + e.getMessage());
    }
    // The foo service is available => import1 must be valid
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import2);
    ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 1", refs);
    assertEquals("Test foo availability inside the composite - 1.2 (" + refs.length + ")", refs.length, 2);
    FooService fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    FooService fs2 = (FooService) sc.getService(refs[1]);
    assertTrue("Test foo invocation", fs2.foo());
    sc.ungetService(refs[0]);
    sc.ungetService(refs[1]);
    // Stop the first provider
    foo1.stop();
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 2", refs);
    assertEquals("Test foo availability inside the composite - 2.1 (" + refs.length + ")", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    // stop the second foo provider
    foo2.dispose();
    // No provider -> Invalid
    assertTrue("Test component invalidity - 2", import2.getState() == ComponentInstance.INVALID);
    foo1.start();
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertNotNull("Test foo availability inside the composite - 3", refs);
    assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
    fs = (FooService) sc.getService(refs[0]);
    assertTrue("Test foo invocation", fs.foo());
    sc.ungetService(refs[0]);
    foo1.dispose();
    // No provider -> Invalid
    assertTrue("Test component invalidity - 3", import2.getState() == ComponentInstance.INVALID);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceContext (org.apache.felix.ipojo.ServiceContext)62 Test (org.junit.Test)60 Properties (java.util.Properties)48 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)47 ServiceReference (org.osgi.framework.ServiceReference)35 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 Factory (org.apache.felix.ipojo.Factory)14 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)10 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)7 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)6 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 Architecture (org.apache.felix.ipojo.architecture.Architecture)2 List (java.util.List)1 ComponentTypeDescription (org.apache.felix.ipojo.architecture.ComponentTypeDescription)1 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)1 Attribute (org.apache.felix.ipojo.metadata.Attribute)1 Element (org.apache.felix.ipojo.metadata.Element)1 Ignore (org.junit.Ignore)1