Search in sources :

Example 96 with FooService

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

the class CustomStrategyTest method testOneService.

@Test
public void testOneService() {
    prov.start();
    cons2.stop();
    cons1.stop();
    assertEquals("Prov valid", ComponentInstance.VALID, prov.getState());
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov.getInstanceName());
    assertNotNull("Service available", ref);
    checkCreatedObjects(prov, 0);
    // Step 1 : create start one consumer
    cons1.start();
    ServiceReference refcons1 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons1.getInstanceName());
    assertNotNull("Cons1 Service available", refcons1);
    CheckService cs_cons1 = (CheckService) osgiHelper.getRawServiceObject(refcons1);
    Properties props = cs_cons1.getProps();
    Long id = (Long) props.get("id");
    FooService fscons1 = (FooService) props.get("object");
    assertEquals("id 1", 1, id.intValue());
    checkCreatedObjects(prov, 1);
    // Step 2 : create a second consumer
    cons2.start();
    ServiceReference refcons2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons2.getInstanceName());
    assertNotNull("Cons2 Service available", refcons2);
    CheckService cs_cons2 = (CheckService) osgiHelper.getRawServiceObject(refcons2);
    Properties props2 = cs_cons2.getProps();
    Long id2 = (Long) props2.get("id");
    FooService fscons2 = (FooService) props2.get("object");
    assertEquals("id 2", 2, id2.intValue());
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons1, fscons2);
    // Step 3 : stop the second provider
    System.out.println("cons2 stopping");
    cons2.stop();
    System.out.println("cons2 stopped");
    checkCreatedObjects(prov, 1);
    // Step 4 : stop the first consumer
    cons1.stop();
    checkCreatedObjects(prov, 0);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 97 with FooService

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

the class PerInstanceStrategyTest method testOneService.

@Test
public void testOneService() {
    prov.start();
    cons2.stop();
    cons1.stop();
    assertEquals("Prov valid", ComponentInstance.VALID, prov.getState());
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov.getInstanceName());
    assertNotNull("Service available", ref);
    checkCreatedObjects(prov, 0);
    // Step 1 : create start one consumer
    cons1.start();
    ServiceReference refcons1 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons1.getInstanceName());
    assertNotNull("Cons1 Service available", refcons1);
    CheckService cs_cons1 = (CheckService) osgiHelper.getRawServiceObject(refcons1);
    Properties props = cs_cons1.getProps();
    Long id = (Long) props.get("id");
    FooService fscons1 = (FooService) props.get("object");
    assertEquals("id 1", 1, id.intValue());
    checkCreatedObjects(prov, 1);
    // Step 2 : create a second consumer
    cons2.start();
    ServiceReference refcons2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons2.getInstanceName());
    assertNotNull("Cons2 Service available", refcons2);
    CheckService cs_cons2 = (CheckService) osgiHelper.getRawServiceObject(refcons2);
    Properties props2 = cs_cons2.getProps();
    Long id2 = (Long) props2.get("id");
    FooService fscons2 = (FooService) props2.get("object");
    assertEquals("id 2", 2, id2.intValue());
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons1, fscons2);
    // Step 3 : stop the second provider
    System.out.println("cons2 stopping");
    cons2.stop();
    System.out.println("cons2 stopped");
    checkCreatedObjects(prov, 1);
    // Step 4 : stop the first consumer
    cons1.stop();
    checkCreatedObjects(prov, 0);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 98 with FooService

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

the class TestTypedList method getProps.

public Properties getProps() {
    Properties props = new Properties();
    if (list != null) {
        props.put("list", list);
        int i = 0;
        for (FooService fs : list) {
            props.put(i, fs.foo());
            i++;
        }
    }
    return props;
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Properties(java.util.Properties)

Example 99 with FooService

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

the class TestExceptionHandling method testTry.

/**
 * Check that the exception is correctly catch by the POJO.
 */
@Test
public void testTry() {
    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.testTry();
    } catch (Exception e) {
        fail("The method has returned an exception");
    }
}
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 100 with FooService

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

the class TestManagedServiceFactoryTestForImmediate method testCreationAndReconfiguration2.

@Test
public void testCreationAndReconfiguration2() {
    // The reconfiguration happens before the service invocation
    Configuration configuration = null;
    try {
        configuration = admin.createFactoryConfiguration("CA-ImmConfigurableProvider", "?");
    } catch (IOException e) {
        fail(e.getMessage());
    }
    Dictionary props = configuration.getProperties();
    if (props == null) {
        props = new Properties();
    }
    props.put("message", "message");
    try {
        configuration.update(props);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    String pid = configuration.getPid();
    System.out.println("PID : " + pid);
    // Wait for the processing of the first configuration.
    grace();
    // The instance should be created, wait for the architecture service
    osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=" + pid + ")", 1000);
    Architecture architecture = (Architecture) osgiHelper.getServiceObject(Architecture.class.getName(), "(architecture.instance=" + pid + ")");
    assertEquals("Check object", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    props.put("message", "message2");
    try {
        configuration.update(props);
        // Update the configuration ...
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // architecture = (Architecture) osgiHelper.getServiceObject( Architecture.class.getName(), "(architecture.instance="+pid+")");
    assertEquals("Check object -2", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    // Invoke
    FooService fs = (FooService) osgiHelper.getServiceObject(FooService.class.getName(), "(instance.name=" + pid + ")");
    Properties p = fs.fooProps();
    String mes = p.getProperty("message");
    int count = ((Integer) p.get("count")).intValue();
    // architecture = (Architecture) osgiHelper.getServiceObject( Architecture.class.getName(), "(architecture.instance="+pid+")");
    assertEquals("Assert Message", "message2", mes);
    assertEquals("Assert count", 2, count);
    assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) architecture.getInstanceDescription()).getCreatedObjects().length);
    try {
        configuration.delete();
        grace();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ServiceReference ref = osgiHelper.getServiceReference(FooService.class.getName(), "(instance.name=" + pid + ")");
    assertNull("Check unavailability", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Architecture(org.apache.felix.ipojo.architecture.Architecture) IOException(java.io.IOException) Properties(java.util.Properties) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) IOException(java.io.IOException) 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