Search in sources :

Example 61 with FooService

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

the class TestOptionalImport method testSimple.

@Test
public void testSimple() {
    // No provider -> valid
    assertTrue("Test component invalidity", import3.getState() == ComponentInstance.VALID);
    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", import3.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(import3);
    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]);
    // Stop the second provider
    foo2.dispose();
    assertTrue("Test component validity", import3.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import3);
    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", import3.getState() == ComponentInstance.VALID);
    foo.start();
    assertTrue("Test component validity", import3.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import3);
    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", import3.getState() == ComponentInstance.VALID);
}
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 62 with FooService

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

the class TestOptionalMultipleImport method testSimple2.

@Test
public void testSimple2() {
    // No provider -> Invalid
    assertTrue("Test component validity", import2.getState() == ComponentInstance.VALID);
    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 validity - 2", import2.getState() == ComponentInstance.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertEquals("Test foo non-availability inside the composite - 1", refs.length, 0);
    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.VALID);
    sc = getServiceContext(import2);
    refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
    assertEquals("Test foo non-availability inside the composite - 2", refs.length, 0);
}
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 63 with FooService

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

the class TestSimpleImport method testSimple2.

@Test
public void testSimple2() {
    // No provider -> Invalid
    assertTrue("Test component invalidity", import1.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", 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]);
    // Stop the first provider
    foo1.stop();
    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 - 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", import1.getState() == ComponentInstance.INVALID);
    foo1.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]);
    foo1.dispose();
    // No provider -> Invalid
    assertTrue("Test component invalidity - 3", import1.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 64 with FooService

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

the class TestConfigurableInstantiation method testAccept.

@Test
public void testAccept() {
    Properties props = new Properties();
    props.put("instance.name", "under-A");
    ComponentInstance under = null;
    try {
        under = acceptF.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(under);
    ServiceReference ref = sc.getServiceReference(FooService.class.getName());
    assertNotNull("Check refs not null", ref);
    FooService foo = (FooService) sc.getService(ref);
    Properties p = foo.fooProps();
    boolean b = ((Boolean) p.get("boolProp")).booleanValue();
    String s = (String) p.get("strProp");
    int i = ((Integer) p.get("intProp")).intValue();
    assertTrue("Test boolean", b);
    assertEquals("Test string", s, "foo");
    // TODO See why it fails...
    // assertEquals("Test int", i, 5); // The code fix to 5.
    under.dispose();
}
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 65 with FooService

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

the class TestPOJOCreation method testCustomConstuctor.

/**
 * Test a custom constructor.
 * Not manipulated.
 */
@Test
public void testCustomConstuctor() {
    FooService fs = new FooProviderType1(0, "foo", bc);
    Properties props = fs.fooProps();
    assertEquals("Check bar", 0, ((Integer) props.get("bar")).intValue());
    assertEquals("Check foo", "foo", props.get("foo"));
    assertEquals("Check context", bc, props.get("context"));
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Properties(java.util.Properties) FooProviderType1(org.apache.felix.ipojo.runtime.core.components.FooProviderType1) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Aggregations

FooService (org.apache.felix.ipojo.runtime.core.services.FooService)123 Test (org.junit.Test)121 ServiceReference (org.osgi.framework.ServiceReference)110 Properties (java.util.Properties)68 Configuration (org.osgi.service.cm.Configuration)33 Hashtable (java.util.Hashtable)30 Dictionary (java.util.Dictionary)28 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)25 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)24 ServiceContext (org.apache.felix.ipojo.ServiceContext)21 IOException (java.io.IOException)19 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)17 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)15 ConfigurationException (org.osgi.service.cm.ConfigurationException)13 ManagedService (org.osgi.service.cm.ManagedService)13 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)13 Architecture (org.apache.felix.ipojo.architecture.Architecture)11 Factory (org.apache.felix.ipojo.Factory)8 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)5 FooProviderType1 (org.apache.felix.ipojo.runtime.core.components.FooProviderType1)3