Search in sources :

Example 51 with FooService

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

the class TestCompositeAPI method testInstantiator.

@Test
public void testInstantiator() {
    String type = "composite.instantiator";
    Factory fact = ipojoHelper.getFactory(type);
    ComponentInstance ci = null;
    Properties p = new Properties();
    p.put("instance.name", "mon_coeur");
    try {
        ci = fact.createComponentInstance(p);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue("Check ci", ci.getState() == ComponentInstance.VALID);
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(BazService.class.getName(), "mon_coeur");
    assertNotNull("Check ref", ref);
    BazService bs = (BazService) getContext().getService(ref);
    assertTrue("Check invocation", bs.foo());
    getContext().ungetService(ref);
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "mon_coeur");
    assertNotNull("Check ref 2 ", ref);
    FooService fs = (FooService) getContext().getService(ref);
    assertTrue("Check invocation", fs.foo());
    getContext().ungetService(ref);
    ci.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) BazService(org.apache.felix.ipojo.runtime.core.services.BazService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 52 with FooService

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

the class TestExceptionHandling method testException.

/**
 * Check that the exception is correctly propagated.
 */
@Test
public void testException() {
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy.getInstanceName());
    assertNotNull("Check that a FooService from " + ci_lazzy.getInstanceName() + " is available", ref);
    FooProviderType1 fs = (FooProviderType1) osgiHelper.getServiceObject(ref);
    try {
        fs.testException();
        fail("The method must returns an exception");
    } catch (Exception e) {
    // OK
    }
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) FooProviderType1(org.apache.felix.ipojo.runtime.core.components.FooProviderType1) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 53 with FooService

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

the class TestGenericList method testTypedList.

@Test
public void testTypedList() {
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), checker.getInstanceName());
    CheckService check = (CheckService) osgiHelper.getServiceObject(ref);
    assertNotNull("Checker availability", check);
    // Check without providers
    assertFalse("Empty list", check.check());
    // Start the first provider
    foo1.start();
    assertTrue("List with one element", check.check());
    Properties props = check.getProps();
    List<FooService> list = (List<FooService>) props.get("list");
    assertEquals("Check size - 1", 1, list.size());
    // Start the second provider
    foo2.start();
    assertTrue("List with two element", check.check());
    props = check.getProps();
    list = (List<FooService>) props.get("list");
    assertEquals("Check size - 2", 2, list.size());
    // Stop the first one
    foo1.stop();
    assertTrue("List with one element (2)", check.check());
    props = check.getProps();
    list = (List<FooService>) props.get("list");
    assertEquals("Check size - 3", 1, list.size());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) List(java.util.List) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 54 with FooService

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

the class TestGetComponentInstance method testGetComponentInstance.

/**
 * Check if a pojo can correctly be cast in POJO.
 * Check the getComponentInstance method.
 */
@Test
public void testGetComponentInstance() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
    String factName = "Manipulation-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref = null;
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    Properties props = new Properties();
    props.put("instance.name", compName);
    ComponentInstance ci = null;
    try {
        ci = fact.createComponentInstance(props);
    } catch (Exception e1) {
        fail(e1.getMessage());
    }
    assertEquals("Check instance name", compName, ci.getInstanceName());
    // Get a FooService provider
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), compName);
    assertNotNull("FS not available", ref);
    // Get foo object
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    // Cast to POJO
    Pojo pojo = (Pojo) fs;
    Field im = fs.getClass().getDeclaredField("__IM");
    assertNotNull(im);
    im.setAccessible(true);
    assertNotNull(im.get(fs));
    Method method = fs.getClass().getMethod("getComponentInstance");
    assertNotNull(method);
    // GetComponentInstance
    ComponentInstance instance = pojo.getComponentInstance();
    assertNotNull(instance);
    assertEquals("Check component instance name", instance.getInstanceName(), compName);
    assertEquals("Check component factory name", instance.getFactory().getName(), factName);
    assertNotNull("Instance description not null", instance.getInstanceDescription());
    PrimitiveInstanceDescription id = (PrimitiveInstanceDescription) instance.getInstanceDescription();
    assertTrue("Check instance state", id.getState() == ComponentInstance.VALID);
    assertEquals("Check created pojo count", id.getCreatedObjects().length, 1);
    assertEquals("Check instance description name", id.getName(), compName);
    ci.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Field(java.lang.reflect.Field) Pojo(org.apache.felix.ipojo.Pojo) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Method(java.lang.reflect.Method) Properties(java.util.Properties) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 55 with FooService

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

the class TestDelayedFilteredImport method testSimple.

@Test
public void testSimple() {
    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]);
    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 - 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 invalidity - 2", 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) Test(org.junit.Test)

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