Search in sources :

Example 1 with CompositeInstanceDescription

use of org.apache.felix.ipojo.composite.CompositeInstanceDescription in project felix by apache.

the class TestMultipleInstantiation method testArchitecture.

@Test
public void testArchitecture() {
    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());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    Architecture arch = (Architecture) getContext().getService(ref);
    CompositeInstanceDescription id = (CompositeInstanceDescription) arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    InstanceDescription[] contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 3);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.2");
    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);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 2);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.2");
    fact2.stop();
    assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.2");
    fact3.stop();
    assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.INVALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 0);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.2");
    fact1.start();
    assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.2");
    getContext().ungetService(ref);
    under.dispose();
    fact2.start();
    fact3.start();
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CompositeInstanceDescription(org.apache.felix.ipojo.composite.CompositeInstanceDescription) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) CompositeInstanceDescription(org.apache.felix.ipojo.composite.CompositeInstanceDescription) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 2 with CompositeInstanceDescription

use of org.apache.felix.ipojo.composite.CompositeInstanceDescription in project felix by apache.

the class TestOptionalInstantiation method testArchitecture.

@Test
public void testArchitecture() {
    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());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    Architecture arch = (Architecture) getContext().getService(ref);
    assertNotNull("Check architecture", arch);
    CompositeInstanceDescription id = (CompositeInstanceDescription) arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    InstanceDescription[] contained = id.getContainedInstances();
    assertNotNull("Check contained not null", contained);
    assertEquals("Check contained instances count (" + contained.length + ") - 1", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.3");
    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);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.3");
    fact2.stop();
    assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.3");
    fact3.stop();
    assertTrue("Check instance invalidity", under.getState() == ComponentInstance.VALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 0);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.3");
    fact1.start();
    assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
    ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "under");
    assertNotNull("Check architecture availability", ref);
    arch = (Architecture) getContext().getService(ref);
    // id = arch.getInstanceDescription();
    assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
    contained = id.getContainedInstances();
    assertEquals("Check contained instances count", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.3");
    getContext().ungetService(ref);
    under.dispose();
    fact2.start();
    fact3.start();
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CompositeInstanceDescription(org.apache.felix.ipojo.composite.CompositeInstanceDescription) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) CompositeInstanceDescription(org.apache.felix.ipojo.composite.CompositeInstanceDescription) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with CompositeInstanceDescription

use of org.apache.felix.ipojo.composite.CompositeInstanceDescription in project felix by apache.

the class EmptyCompositeTest method testEmptyCompositeCreation.

@Test
public void testEmptyCompositeCreation() {
    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());
    }
    ComponentTypeDescription cd = ci.getFactory().getComponentDescription();
    assertEquals("Check component type name", cd.getName(), "composite.empty");
    // assertEquals("Check class name (" + cd.getClassName() + ")", cd.getClassName(), "composite");
    assertEquals("Check offered service", cd.getprovidedServiceSpecification().length, 0);
    assertEquals("Check configurable properties", cd.getProperties().length, 0);
    CompositeInstanceDescription id = (CompositeInstanceDescription) ci.getInstanceDescription();
    assertEquals("Check composite instance name", id.getName(), "empty");
    assertEquals("Check composite instance state (" + id.getState() + ")", id.getState(), ComponentInstance.VALID);
    assertEquals("Check contained instance", id.getContainedInstances().length, 0);
    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());
    }
    ci.dispose();
}
Also used : ComponentTypeDescription(org.apache.felix.ipojo.architecture.ComponentTypeDescription) 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 4 with CompositeInstanceDescription

use of org.apache.felix.ipojo.composite.CompositeInstanceDescription in project felix by apache.

the class EmptyCompositeTest method testInstanceCreation3.

@Test
public void testInstanceCreation3() {
    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) {
        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());
    }
    Factory factory2 = ipojoHelper.getFactory(sc, "composite.empty");
    assertNotNull("Check factory2 not null", factory2);
    Properties props2 = new Properties();
    props2.put("instance.name", "empty2");
    ComponentInstance ci2 = null;
    try {
        ci2 = factory2.createComponentInstance(props2, sc);
    } catch (Exception e) {
        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 5 with CompositeInstanceDescription

use of org.apache.felix.ipojo.composite.CompositeInstanceDescription in project felix by apache.

the class EmptyCompositeTest method testInstanceCreation2.

@Test
public void testInstanceCreation2() {
    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) {
        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());
    }
    Factory factory2 = ipojoHelper.getFactory(sc, "composite.empty");
    assertNotNull("Check factory2 not null", factory2);
    Properties props2 = new Properties();
    props2.put("instance.name", "empty2");
    ComponentInstance ci2 = null;
    try {
        ci2 = factory2.createComponentInstance(props2);
    } catch (Exception e) {
        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)

Aggregations

Properties (java.util.Properties)8 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)8 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)8 Test (org.junit.Test)7 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)4 Factory (org.apache.felix.ipojo.Factory)4 ServiceContext (org.apache.felix.ipojo.ServiceContext)4 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 ComponentTypeDescription (org.apache.felix.ipojo.architecture.ComponentTypeDescription)1