Search in sources :

Example 46 with ServiceContext

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

the class TestOptionalInstantiation method testServiceAvailability.

@Test
public void testServiceAvailability() {
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = bar1Factory.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()));
    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 47 with ServiceContext

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

the class TestOptionalInstantiation method testCreationLevel2.

@Test
public void testCreationLevel2() {
    ServiceContext sc = getServiceContext(empty);
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = bar1Factory.createComponentInstance(props, sc);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    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 48 with ServiceContext

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

the class TestOptionalMultipleInstantiation method testFactoryManagement.

@Test
public void testFactoryManagement() {
    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());
    }
    ServiceContext sc = getServiceContext(under);
    assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
    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(sc, BarService.class.getName(), null).length, 2);
    fact2.stop();
    assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
    fact3.stop();
    assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc, BarService.class.getName(), null).length, 0);
    fact1.start();
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
    assertTrue("Check instance validity - 5", under.getState() == ComponentInstance.VALID);
    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)

Example 49 with ServiceContext

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

the class TestOptionalMultipleInstantiation 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 validity - 4", under.getState() == ComponentInstance.VALID);
    assertEquals("Check service provider number", ipojoHelper.getServiceReferences(sc2, BarService.class.getName(), null).length, 0);
    fact1.start();
    assertTrue("Check instance validity - 5", 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)

Example 50 with ServiceContext

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

the class TestOptionalMultipleInstantiation 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)

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