Search in sources :

Example 26 with ComponentFactory

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

the class TestSimpleInstance method atestArchitecture.

public void atestArchitecture() {
    // TODO fix and reactivate the method.
    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());
    }
    ServiceReference ref = osgiHelper.getServiceReference(Architecture.class.getName(), "(architecture.instance=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 + ")", contained.length, 1);
    assertEquals("Check instance name", id.getName(), "under");
    assertEquals("Check component type name", id.getComponentDescription().getName(), "composite.bar.1");
    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 = osgiHelper.getServiceReference(Architecture.class.getName(), "(architecture.instance=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.1");
    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.1");
    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.1");
    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.1");
    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)

Example 27 with ComponentFactory

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

the class TestSimpleInstance method setUp.

@Before
public void setUp() {
    fooFactory1 = (ComponentFactory) ipojoHelper.getFactory("COMPO-FooProviderType-1");
    fooFactory2 = (ComponentFactory) ipojoHelper.getFactory("COMPO-FooProviderType-Dyn2");
    compoFactory = (ComponentFactory) ipojoHelper.getFactory("composite.inst.1");
    Factory fact = ipojoHelper.getFactory("composite.empty");
    Properties props = new Properties();
    props.put("instance.name", "empty-X");
    try {
        empty = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the empty composite : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

Example 28 with ComponentFactory

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

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

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

the class TestOptionalInstantiation method setUp.

@Before
public void setUp() {
    bar1Factory = (ComponentFactory) ipojoHelper.getFactory("composite.bar.3");
    Factory fact = ipojoHelper.getFactory("composite.empty");
    Properties props = new Properties();
    props.put("instance.name", "empty");
    try {
        empty = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the empty composite : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

Example 30 with ComponentFactory

use of org.apache.felix.ipojo.ComponentFactory 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)

Aggregations

ComponentFactory (org.apache.felix.ipojo.ComponentFactory)35 Properties (java.util.Properties)27 Before (org.junit.Before)15 Test (org.junit.Test)15 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)14 Factory (org.apache.felix.ipojo.Factory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)7 Architecture (org.apache.felix.ipojo.architecture.Architecture)5 ServiceReference (org.osgi.framework.ServiceReference)5 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)4 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)2 InstanceManager (org.apache.felix.ipojo.InstanceManager)2 Element (org.apache.felix.ipojo.metadata.Element)2 ParseException (org.apache.felix.ipojo.parser.ParseException)2 MockBundle (org.apache.felix.ipojo.test.MockBundle)2 Logger (org.apache.felix.ipojo.util.Logger)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2