Search in sources :

Example 16 with ServiceContext

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

the class TestSimpleInstance method testServiceAvailability.

@Test
public void testServiceAvailability() {
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = compoFactory.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(FooService.class.getName()));
    assertEquals("Check service provider", ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null).length, 2);
    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) Test(org.junit.Test)

Example 17 with ServiceContext

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

the class TestConfigurableInstantiation method testAccept.

@Test
public void testAccept() {
    Properties props = new Properties();
    props.put("instance.name", "under-A");
    ComponentInstance under = null;
    try {
        under = acceptF.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
    ServiceContext sc = getServiceContext(under);
    ServiceReference ref = sc.getServiceReference(FooService.class.getName());
    assertNotNull("Check refs not null", ref);
    FooService foo = (FooService) sc.getService(ref);
    Properties p = foo.fooProps();
    boolean b = ((Boolean) p.get("boolProp")).booleanValue();
    String s = (String) p.get("strProp");
    int i = ((Integer) p.get("intProp")).intValue();
    assertTrue("Test boolean", b);
    assertEquals("Test string", s, "foo");
    // TODO See why it fails...
    // assertEquals("Test int", i, 5); // The code fix to 5.
    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) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 18 with ServiceContext

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

the class TestMultipleInstantiation 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 invalidity", under.getState() == ComponentInstance.INVALID);
    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 - 4", 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 19 with ServiceContext

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

the class TestMultipleInstantiation method testCreationLevel2.

@Test
public void testCreationLevel2() {
    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);
    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 20 with ServiceContext

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

the class TestOptionalInstantiation method testServiceAvailabilityLevel2.

@Test
public void testServiceAvailabilityLevel2() {
    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);
    ServiceContext sc2 = getServiceContext(under);
    assertNotNull("Check service availability", sc2.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)

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