Search in sources :

Example 41 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestConfiguration method testDefaultInstantiation.

@Test
public void testDefaultInstantiation() throws InvalidSyntaxException {
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = compositeFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(under);
    ServiceReference[] refs = sc.getServiceReferences(FooService.class.getName(), null);
    assertEquals(2, refs.length);
    for (int i = 0; i < refs.length; i++) {
        assertEquals(3, ((Integer) refs[i].getProperty("int")).intValue());
        assertEquals("foo", (String) refs[i].getProperty("string"));
    }
    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) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 42 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestConfiguration method testConfiguredInstantiation.

@Test
public void testConfiguredInstantiation() throws InvalidSyntaxException {
    Properties props = new Properties();
    props.put("instance.name", "under");
    props.put("string", "bar");
    props.put("int", "25");
    ComponentInstance under = null;
    try {
        under = compositeFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(under);
    ServiceReference[] refs = sc.getServiceReferences(FooService.class.getName(), null);
    assertEquals(2, refs.length);
    for (int i = 0; i < refs.length; i++) {
        assertEquals(25, ((Integer) refs[i].getProperty("int")).intValue());
        assertEquals("bar", (String) refs[i].getProperty("string"));
    }
    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) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 43 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestMultipleInstantiation method testServiceAvailability.

@Test
public void testServiceAvailability() {
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = bar2Factory.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(under);
    assertNotNull("Check service availability", sc.getServiceReference(BarService.class.getName()));
    int count = ipojoHelper.getServiceReferences(sc, BarService.class.getName(), null).length;
    assertEquals("Check service provider number : " + count, count, 3);
    under.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) Test(org.junit.Test)

Example 44 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestMultipleInstantiation method testServiceAvailabilityLevel2.

@Test
public void testServiceAvailabilityLevel2() {
    ServiceContext sc = getServiceContext(empty);
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = bar2Factory.createComponentInstance(props, sc);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc2 = getServiceContext(under);
    assertNotNull("Check service availability", sc2.getServiceReference(BarService.class.getName()));
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 3);
    under.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Example 45 with ServiceContext

use of org.apache.felix.ipojo.ServiceContext in project felix by apache.

the class TestMultipleInstantiation method testFactoryManagementLevel2.

@Test
public void testFactoryManagementLevel2() {
    ServiceContext sc = getServiceContext(empty);
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = bar2Factory.createComponentInstance(props, sc);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
    ServiceContext sc2 = getServiceContext(under);
    ComponentFactory fact1 = (ComponentFactory) ipojoHelper.getFactory("COMPO-FooBarProviderType-1");
    ComponentFactory fact2 = (ComponentFactory) ipojoHelper.getFactory("COMPO-FooBarProviderType-2");
    ComponentFactory fact3 = (ComponentFactory) ipojoHelper.getFactory("COMPO-FooBarProviderType-3");
    fact1.stop();
    assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 2);
    fact2.stop();
    assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
    fact3.stop();
    assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 0);
    fact1.start();
    assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
    under.dispose();
    fact2.start();
    fact3.start();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) Test(org.junit.Test)

Aggregations

ServiceContext (org.apache.felix.ipojo.ServiceContext)62 Test (org.junit.Test)60 Properties (java.util.Properties)48 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)47 ServiceReference (org.osgi.framework.ServiceReference)35 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 Factory (org.apache.felix.ipojo.Factory)14 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)10 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)7 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)6 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 Architecture (org.apache.felix.ipojo.architecture.Architecture)2 List (java.util.List)1 ComponentTypeDescription (org.apache.felix.ipojo.architecture.ComponentTypeDescription)1 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)1 Attribute (org.apache.felix.ipojo.metadata.Attribute)1 Element (org.apache.felix.ipojo.metadata.Element)1 Ignore (org.junit.Ignore)1