Search in sources :

Example 86 with Factory

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

the class EmptyCompositeTest method testInstanceCreation1.

@Test
public void testInstanceCreation1() {
    Factory factory = ipojoHelper.getFactory("composite.empty");
    Properties props = new Properties();
    props.put("instance.name", "empty");
    ComponentInstance ci = null;
    try {
        ci = factory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unacceptable configuration : " + e.getMessage());
    }
    assertTrue("Check composite manager", ci instanceof CompositeManager);
    CompositeManager cm = (CompositeManager) ci;
    ServiceContext sc = cm.getServiceContext();
    try {
        assertEquals("Check number of factories imported", sc.getServiceReferences(Factory.class.getName(), null).length, getContext().getServiceReferences(Factory.class.getName(), null).length);
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e.getMessage());
    }
    Properties props2 = new Properties();
    props2.put("instance.name", "empty2");
    ComponentInstance ci2 = null;
    try {
        ci2 = factory.createComponentInstance(props2, sc);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unacceptable configuration : " + e.getMessage());
    }
    CompositeInstanceDescription id = (CompositeInstanceDescription) ci.getInstanceDescription();
    assertEquals("Check composite instance name", id.getName(), "empty");
    assertEquals("Check composite instance state", id.getState(), ComponentInstance.VALID);
    assertEquals("Check contained instance", id.getContainedInstances().length, 1);
    CompositeInstanceDescription id2 = (CompositeInstanceDescription) id.getContainedInstances()[0];
    assertEquals("Check composite instance name", id2.getName(), "empty2");
    assertEquals("Check composite instance state", id2.getState(), ComponentInstance.VALID);
    assertEquals("Check contained instance", id2.getContainedInstances().length, 0);
    ci2.dispose();
    // id = ci.getInstanceDescription();
    assertEquals("Check composite instance name", id.getName(), "empty");
    assertEquals("Check composite instance state", id.getState(), ComponentInstance.VALID);
    assertEquals("Check contained instance", id.getContainedInstances().length, 0);
    ci.dispose();
}
Also used : ServiceContext(org.apache.felix.ipojo.ServiceContext) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CompositeManager(org.apache.felix.ipojo.composite.CompositeManager) Factory(org.apache.felix.ipojo.Factory) CompositeInstanceDescription(org.apache.felix.ipojo.composite.CompositeInstanceDescription) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Properties(java.util.Properties) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Test(org.junit.Test)

Example 87 with Factory

use of org.apache.felix.ipojo.Factory 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 88 with Factory

use of org.apache.felix.ipojo.Factory 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 89 with Factory

use of org.apache.felix.ipojo.Factory 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 90 with Factory

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

the class TestCompositeAPI method testApplication.

@Test
public void testApplication() {
    Factory factory = ipojoHelper.getFactory("composite.test.1");
    ComponentInstance ci = null;
    Properties props = new Properties();
    props.put("instance.name", "Test");
    try {
        ci = factory.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot instantiate Test " + e.getMessage());
    }
    assertTrue("Check ci state", ci.getState() == ComponentInstance.VALID);
    ServiceReference[] refs = null;
    try {
        refs = getContext().getServiceReferences(CheckService.class.getName(), "(instance.name=Test)");
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter : " + e.getMessage());
    }
    assertNotNull("Check refs not null", refs);
    CheckService cs = (CheckService) getContext().getService(refs[0]);
    assertTrue("Check invocation", cs.check());
    ci.dispose();
}
Also used : 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)

Aggregations

Factory (org.apache.felix.ipojo.Factory)111 Properties (java.util.Properties)88 Test (org.junit.Test)71 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)53 Before (org.junit.Before)33 ServiceReference (org.osgi.framework.ServiceReference)29 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)17 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)13 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)11 IOException (java.io.IOException)6 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4