Search in sources :

Example 21 with FooService

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

the class TestSimplePS method testPS.

@Test
public void testPS() {
    String factName = "PS-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref;
    // Check that no Foo Service are available
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS already available", ref);
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    ipojoHelper.createComponentInstance(factName, compName);
    // Get a FooService provider
    ref = osgiHelper.getServiceReference(FooService.class.getName(), "(" + "instance.name" + "=" + compName + ")");
    assertNotNull("FS not available", ref);
    // Test foo invocation
    FooService fs = (FooService) osgiHelper.getRawServiceObject(ref);
    assertTrue("FooService invocation failed", fs.foo());
    ipojoHelper.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName(), null);
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 22 with FooService

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

the class CustomStrategy2Test 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 23 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService 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 24 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService 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 25 with FooService

use of org.apache.felix.ipojo.runtime.core.services.FooService 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)

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