Search in sources :

Example 36 with ServiceContext

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

the class ServiceRangeTest method testLevelOne3.

@Test
public void testLevelOne3() {
    ServiceContext sc2 = getServiceContext(empty);
    Factory fact1 = ipojoHelper.getFactory("COMPO-SimpleCheckServiceProvider");
    Properties props = new Properties();
    props.put("instance.name", "client");
    ComponentInstance client = null;
    try {
        client = fact1.createComponentInstance(props, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the client : " + e.getMessage());
    }
    Factory fact2 = ipojoHelper.getFactory("COMPO-FooProviderType-1");
    Properties props2 = new Properties();
    props2.put("instance.name", "provider");
    ComponentInstance provider = null;
    try {
        provider = fact2.createComponentInstance(props2, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the provider : " + e.getMessage());
    }
    ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
    CheckService check = (CheckService) sc2.getService(ref);
    assertTrue("Check invocation", check.check());
    sc2.ungetService(ref);
    // Check visibility
    assertNotNull("Check foo service visible inside the composite", sc2.getServiceReference(FooService.class.getName()));
    assertNotNull("Check check service visible inside the composite", sc2.getServiceReference(CheckService.class.getName()));
    // Check invisibilty
    assertNull("Check foo service invisible inside the context", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
    client.dispose();
    provider.dispose();
    assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
    assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the global", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 37 with ServiceContext

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

the class ServiceRangeTest method testLevelTwo3.

@Test
public void testLevelTwo3() {
    ServiceContext sc1 = getServiceContext(empty);
    Factory fact = ipojoHelper.getFactory(sc1, "composite.empty");
    Properties p = new Properties();
    p.put("instance.name", "empty2");
    ComponentInstance empty2 = null;
    try {
        empty2 = fact.createComponentInstance(p);
    } catch (Exception e) {
        fail("Cannot instantiate empty2 instance : " + e.getMessage());
    }
    ServiceContext sc2 = getServiceContext(empty2);
    Factory fact1 = ipojoHelper.getFactory("COMPO-SimpleCheckServiceProvider");
    Properties props = new Properties();
    props.put("instance.name", "client");
    ComponentInstance client = null;
    try {
        client = fact1.createComponentInstance(props, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the client : " + e.getMessage());
    }
    Factory fact2 = ipojoHelper.getFactory("COMPO-FooProviderType-1");
    Properties props2 = new Properties();
    props2.put("instance.name", "provider");
    ComponentInstance provider = null;
    try {
        provider = fact2.createComponentInstance(props2, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the provider : " + e.getMessage());
    }
    ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
    CheckService check = (CheckService) sc2.getService(ref);
    assertTrue("Check invocation", check.check());
    sc2.ungetService(ref);
    // Check visibility
    assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
    assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
    // Check invisibilty
    assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
    assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the global", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
    client.dispose();
    provider.dispose();
    assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
    assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
    assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the global", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
    empty2.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 38 with ServiceContext

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

the class ServiceRangeTest method testLevelTwo4.

@Test
public void testLevelTwo4() {
    ServiceContext sc1 = getServiceContext(empty);
    Factory fact = ipojoHelper.getFactory(sc1, "composite.empty");
    Properties p = new Properties();
    p.put("instance.name", "empty2");
    ComponentInstance empty2 = null;
    try {
        empty2 = fact.createComponentInstance(p);
    } catch (Exception e) {
        fail("Cannot instantiate empty2 instance : " + e.getMessage());
    }
    ServiceContext sc2 = getServiceContext(empty2);
    Factory fact1 = ipojoHelper.getFactory(sc2, "COMPO-SimpleCheckServiceProvider");
    Properties props = new Properties();
    props.put("instance.name", "client");
    ComponentInstance client = null;
    try {
        client = fact1.createComponentInstance(props, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the client : " + e.getMessage());
    }
    Factory fact2 = ipojoHelper.getFactory(sc2, "COMPO-FooProviderType-1");
    Properties props2 = new Properties();
    props2.put("instance.name", "provider");
    ComponentInstance provider = null;
    try {
        provider = fact2.createComponentInstance(props2, sc2);
    } catch (Exception e) {
        fail("Cannot instantiate the provider : " + e.getMessage());
    }
    ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
    CheckService check = (CheckService) sc2.getService(ref);
    assertTrue("Check invocation", check.check());
    sc2.ungetService(ref);
    // Check visibility
    assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
    assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
    // Check invisibilty
    assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
    assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the global", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
    client.dispose();
    provider.dispose();
    assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
    assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
    assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
    assertNull("Check foo service invisible inside the global", getContext().getServiceReference(FooService.class.getName()));
    try {
        assertNull("Check check service invisible inside the context", getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e);
    }
    empty2.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 39 with ServiceContext

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

the class TestSimpleInstance method testFactoryManagementLevel2.

@Test
public void testFactoryManagementLevel2() {
    ServiceContext sc = getServiceContext(empty);
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = compoFactory.createComponentInstance(props, sc);
    } catch (Exception e) {
        fail("Cannot instantiate under : " + e.getMessage());
    }
    assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
    assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
    fooFactory1.stop();
    assertTrue("Check instance invalidity - 2", under.getState() == ComponentInstance.INVALID);
    fooFactory1.start();
    assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
    fooFactory2.stop();
    assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
    fooFactory2.start();
    assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
    under.dispose();
    fooFactory1.start();
    fooFactory2.start();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) Test(org.junit.Test)

Example 40 with ServiceContext

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

the class TestSimpleInstance method testCreationLevel2.

@Test
public void testCreationLevel2() {
    ServiceContext sc = getServiceContext(empty);
    Properties props = new Properties();
    props.put("instance.name", "under");
    ComponentInstance under = null;
    try {
        under = compoFactory.createComponentInstance(props, sc);
    } catch (Exception e) {
        e.printStackTrace();
        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)

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