Search in sources :

Example 11 with CheckService

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

the class CustomStrategyTest method testObjectCreation.

@Test
public void testObjectCreation() {
    prov.start();
    // The two consumers are started and use their own objects.
    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();
    FooService fscons1 = (FooService) props.get("object");
    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();
    FooService fscons2 = (FooService) props2.get("object");
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons1, fscons2);
    // Stop the provider
    prov.stop();
    // Cons1 and 2 are invalid.
    assertEquals("Cons1 invalidity", ComponentInstance.INVALID, cons1.getState());
    assertEquals("Cons2 invalidity", ComponentInstance.INVALID, cons2.getState());
    // No object created in prov
    checkCreatedObjects(prov, 0);
    // Restart the provider
    prov.start();
    // Consumers are valid.
    assertEquals("Cons1 validity", ComponentInstance.VALID, cons1.getState());
    assertEquals("Cons2 validity", ComponentInstance.VALID, cons2.getState());
    // Check objects
    refcons1 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons1.getInstanceName());
    assertNotNull("Cons1 Service available", refcons1);
    cs_cons1 = (CheckService) osgiHelper.getRawServiceObject(refcons1);
    props = cs_cons1.getProps();
    Object fscons3 = (FooService) props.get("object");
    refcons2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons2.getInstanceName());
    assertNotNull("Cons2 Service available", refcons2);
    cs_cons2 = (CheckService) osgiHelper.getRawServiceObject(refcons2);
    props2 = cs_cons2.getProps();
    Object fscons4 = (FooService) props2.get("object");
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons3, fscons4);
    assertNotSame("Two new objects - 1", fscons3, fscons1);
    assertNotSame("Two new objects - 2", fscons4, fscons2);
}
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 12 with CheckService

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

the class PerInstanceStrategyTest method testObjectCreation.

@Test
public void testObjectCreation() {
    prov.start();
    // The two consumers are started and use their own objects.
    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();
    FooService fscons1 = (FooService) props.get("object");
    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();
    FooService fscons2 = (FooService) props2.get("object");
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons1, fscons2);
    // Stop the provider
    prov.stop();
    // Cons1 and 2 are invalid.
    assertEquals("Cons1 invalidity", ComponentInstance.INVALID, cons1.getState());
    assertEquals("Cons2 invalidity", ComponentInstance.INVALID, cons2.getState());
    // No object created in prov
    checkCreatedObjects(prov, 0);
    // Restart the provider
    prov.start();
    // Consumers are valid.
    assertEquals("Cons1 validity", ComponentInstance.VALID, cons1.getState());
    assertEquals("Cons2 validity", ComponentInstance.VALID, cons2.getState());
    // Check objects
    refcons1 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons1.getInstanceName());
    assertNotNull("Cons1 Service available", refcons1);
    cs_cons1 = (CheckService) osgiHelper.getRawServiceObject(refcons1);
    props = cs_cons1.getProps();
    Object fscons3 = (FooService) props.get("object");
    refcons2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons2.getInstanceName());
    assertNotNull("Cons2 Service available", refcons2);
    cs_cons2 = (CheckService) osgiHelper.getRawServiceObject(refcons2);
    props2 = cs_cons2.getProps();
    Object fscons4 = (FooService) props2.get("object");
    checkCreatedObjects(prov, 2);
    assertNotSame("Two objects", fscons3, fscons4);
    assertNotSame("Two new objects - 1", fscons3, fscons1);
    assertNotSame("Two new objects - 2", fscons4, fscons2);
}
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 13 with CheckService

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

the class PerInstanceStrategyTest method testTwoServices.

@Test
public void testTwoServices() {
    cons3.stop();
    prov2.start();
    cons1.stop();
    assertEquals("Prov valid", ComponentInstance.VALID, prov2.getState());
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov2.getInstanceName());
    assertNotNull("Service available", ref);
    ServiceReference refBar = ipojoHelper.getServiceReferenceByName(BarService.class.getName(), prov2.getInstanceName());
    assertNotNull("Service available", refBar);
    checkCreatedObjects(prov2, 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(prov2, 1);
    // Step 2 : create a second consumer
    cons3.start();
    ServiceReference refcons2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), cons3.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(prov2, 2);
    assertNotSame("Two objects", fscons1, fscons2);
    // Step 3 : stop the second provider
    cons3.stop();
    checkCreatedObjects(prov2, 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) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 14 with CheckService

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

the class TestMethodProperties method testConfigurationPrimitiveArraysString.

@Test
public void testConfigurationPrimitiveArraysString() {
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Test check service availability", ref);
    CheckService check = (CheckService) osgiHelper.getRawServiceObject(ref);
    Properties props = check.getProps();
    byte[] b = (byte[]) props.get("bs");
    short[] s = (short[]) props.get("ss");
    int[] i = (int[]) props.get("is");
    long[] l = (long[]) props.get("ls");
    double[] d = (double[]) props.get("ds");
    float[] f = (float[]) props.get("fs");
    char[] c = (char[]) props.get("cs");
    boolean[] bool = (boolean[]) props.get("bools");
    assertEquals("Check b 0", b[0], 1);
    assertEquals("Check b 1", b[1], 2);
    assertEquals("Check b 2", b[2], 3);
    assertEquals("Check s 0", s[0], 1);
    assertEquals("Check s 1", s[1], 2);
    assertEquals("Check s 2", s[2], 3);
    assertEquals("Check i 0", i[0], 1);
    assertEquals("Check i 1", i[1], 2);
    assertEquals("Check i 2", i[2], 3);
    assertEquals("Check l 0", l[0], 1);
    assertEquals("Check l 1", l[1], 2);
    assertEquals("Check l 2", l[2], 3);
    assertEquals("Check d 0", d[0], 1, 0);
    assertEquals("Check d 1", d[1], 2, 0);
    assertEquals("Check d 2", d[2], 3, 0);
    assertEquals("Check f 0", f[0], 1, 0);
    assertEquals("Check f 1", f[1], 2, 0);
    assertEquals("Check f 2", f[2], 3, 0);
    assertEquals("Check c 0", c[0], 'a');
    assertEquals("Check c 1", c[1], 'b');
    assertEquals("Check c 2", c[2], 'c');
    assertTrue("Check bool 0", bool[0]);
    assertTrue("Check bool 1", bool[0]);
    assertTrue("Check bool 2", bool[0]);
    // Integer upb = (Integer) props.get("upbs");
    // Integer ups = (Integer) props.get("upss");
    // Integer upi = (Integer) props.get("upis");
    // Integer upl = (Integer) props.get("upls");
    // Integer upd = (Integer) props.get("upds");
    // Integer upf = (Integer) props.get("upfs");
    // Integer upc = (Integer) props.get("upcs");
    // Integer upbool = (Integer) props.get("upbools");
    // 
    // assertEquals("Check upb", upb, new Integer(1));
    // assertEquals("Check ups", ups, new Integer(1));
    // assertEquals("Check upi", upi, new Integer(1));
    // assertEquals("Check upl", upl, new Integer(1));
    // assertEquals("Check upd", upd, new Integer(1));
    // assertEquals("Check upf", upf, new Integer(1));
    // assertEquals("Check upc", upc, new Integer(1));
    // assertEquals("Check upbool", upbool, new Integer(1));
    reconfigureString(instance);
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Test check service availability", ref);
    check = (CheckService) osgiHelper.getRawServiceObject(ref);
    props = check.getProps();
    b = (byte[]) props.get("bs");
    s = (short[]) props.get("ss");
    i = (int[]) props.get("is");
    l = (long[]) props.get("ls");
    d = (double[]) props.get("ds");
    f = (float[]) props.get("fs");
    c = (char[]) props.get("cs");
    bool = (boolean[]) props.get("bools");
    assertEquals("2) Check b 0", b[0], 3);
    assertEquals("2) Check b 1", b[1], 2);
    assertEquals("2) Check b 2", b[2], 1);
    assertEquals("2) Check s 0", s[0], 3);
    assertEquals("2) Check s 1", s[1], 2);
    assertEquals("2) Check s 2", s[2], 1);
    assertEquals("2) Check i 0", i[0], 3);
    assertEquals("2) Check i 1", i[1], 2);
    assertEquals("2) Check i 2", i[2], 1);
    assertEquals("2) Check l 0", l[0], 3);
    assertEquals("2) Check l 1", l[1], 2);
    assertEquals("2) Check l 2", l[2], 1);
    assertEquals("2) Check d 0", d[0], 3, 0);
    assertEquals("2) Check d 1", d[1], 2, 0);
    assertEquals("2) Check d 2", d[2], 1, 0);
    assertEquals("2) Check f 0", f[0], 3, 0);
    assertEquals("2) Check f 1", f[1], 2, 0);
    assertEquals("2) Check f 2", f[2], 1, 0);
    assertEquals("2) Check c 0", c[0], 'c');
    assertEquals("2) Check c 1", c[1], 'b');
    assertEquals("2) Check c 2", c[2], 'a');
    assertFalse("2) Check bool 0", bool[0]);
    assertFalse("2) Check bool 1", bool[0]);
    assertFalse("2) Check bool 2", bool[0]);
// upb = (Integer) props.get("upbs");
// ups = (Integer) props.get("upss");
// upi = (Integer) props.get("upis");
// upl = (Integer) props.get("upls");
// upd = (Integer) props.get("upds");
// upf = (Integer) props.get("upfs");
// upc = (Integer) props.get("upcs");
// upbool = (Integer) props.get("upbools");
// 
// assertEquals("2) Check upb", upb, new Integer(2));
// assertEquals("2) Check ups", ups, new Integer(2));
// assertEquals("2) Check upi", upi, new Integer(2));
// assertEquals("2) Check upl", upl, new Integer(2));
// assertEquals("2) Check upd", upd, new Integer(2));
// assertEquals("2) Check upf", upf, new Integer(2));
// assertEquals("2) Check upc", upc, new Integer(2));
// assertEquals("2) Check upbool", upbool, new Integer(2));
}
Also used : CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 15 with CheckService

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

the class TestMethodProperties method testConfigurationPrimitiveStringNoValue.

@Test
public void testConfigurationPrimitiveStringNoValue() {
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Test check service availability", ref);
    CheckService check = (CheckService) osgiHelper.getRawServiceObject(ref);
    Properties props = check.getProps();
    Byte b = (Byte) props.get("b");
    Short s = (Short) props.get("s");
    Integer i = (Integer) props.get("i");
    Long l = (Long) props.get("l");
    Double d = (Double) props.get("d");
    Float f = (Float) props.get("f");
    Character c = (Character) props.get("c");
    Boolean bool = (Boolean) props.get("bool");
    assertEquals("Check b", b, new Byte("0"));
    assertEquals("Check s", s, new Short("0"));
    assertEquals("Check i", i, new Integer("0"));
    assertEquals("Check l", l, new Long("0"));
    assertEquals("Check d", d, new Double("0"));
    assertEquals("Check f", f, new Float("0"));
    assertEquals("Check c", c, new Character((char) 0));
    assertEquals("Check bool", bool, new Boolean("false"));
    Integer upb = (Integer) props.get("upb");
    Integer ups = (Integer) props.get("ups");
    Integer upi = (Integer) props.get("upi");
    Integer upl = (Integer) props.get("upl");
    Integer upd = (Integer) props.get("upd");
    Integer upf = (Integer) props.get("upf");
    Integer upc = (Integer) props.get("upc");
    Integer upbool = (Integer) props.get("upbool");
    assertEquals("Check upb", upb, new Integer(0));
    assertEquals("Check ups", ups, new Integer(0));
    assertEquals("Check upi", upi, new Integer(0));
    assertEquals("Check upl", upl, new Integer(0));
    assertEquals("Check upd", upd, new Integer(0));
    assertEquals("Check upf", upf, new Integer(0));
    assertEquals("Check upc", upc, new Integer(0));
    assertEquals("Check upbool", upbool, new Integer(0));
    reconfigureString(instance3);
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance3.getInstanceName());
    assertNotNull("Test check service availability", ref);
    check = (CheckService) osgiHelper.getRawServiceObject(ref);
    props = check.getProps();
    b = (Byte) props.get("b");
    s = (Short) props.get("s");
    i = (Integer) props.get("i");
    l = (Long) props.get("l");
    d = (Double) props.get("d");
    f = (Float) props.get("f");
    c = (Character) props.get("c");
    bool = (Boolean) props.get("bool");
    assertEquals("2) Check b (" + b + ")", b, new Byte("2"));
    assertEquals("2) Check s", s, new Short("2"));
    assertEquals("2) Check i", i, new Integer("2"));
    assertEquals("2) Check l", l, new Long("2"));
    assertEquals("2) Check d", d, new Double("2"));
    assertEquals("2) Check f", f, new Float("2"));
    assertEquals("2) Check c", c, new Character('b'));
    assertEquals("2) Check bool", bool, new Boolean("false"));
    upb = (Integer) props.get("upb");
    ups = (Integer) props.get("ups");
    upi = (Integer) props.get("upi");
    upl = (Integer) props.get("upl");
    upd = (Integer) props.get("upd");
    upf = (Integer) props.get("upf");
    upc = (Integer) props.get("upc");
    upbool = (Integer) props.get("upbool");
    assertEquals("2) Check upb", upb, new Integer(1));
    assertEquals("2) Check ups", ups, new Integer(1));
    assertEquals("2) Check upi", upi, new Integer(1));
    assertEquals("2) Check upl", upl, new Integer(1));
    assertEquals("2) Check upd", upd, new Integer(1));
    assertEquals("2) Check upf", upf, new Integer(1));
    assertEquals("2) Check upc", upc, new Integer(1));
    assertEquals("2) Check upbool", upbool, new Integer(1));
}
Also used : CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)124 Test (org.junit.Test)122 ServiceReference (org.osgi.framework.ServiceReference)90 Properties (java.util.Properties)87 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)50 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)20 Factory (org.apache.felix.ipojo.Factory)11 ServiceContext (org.apache.felix.ipojo.ServiceContext)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)6 Hashtable (java.util.Hashtable)5 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)2 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)2 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)2 CallbackCheckService (org.apache.felix.ipojo.runtime.core.services.CallbackCheckService)2 Before (org.junit.Before)2 List (java.util.List)1