Search in sources :

Example 1 with Architecture

use of org.apache.felix.ipojo.architecture.Architecture in project felix by apache.

the class TestProvidedServiceArchitecture method testProps.

@Test
public void testProps() {
    String factName = "PS-FooProviderType-3";
    String compName = "FooProvider";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider", fact);
    Properties props = new Properties();
    props.put("instance.name", compName);
    props.put("foo", "foo");
    props.put("bar", "2");
    props.put("baz", "baz");
    ipojoHelper.createComponentInstance(factName, props);
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
    assertNotNull("Architecture not available", arch_ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
    InstanceDescription id = arch.getInstanceDescription();
    assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
    assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooProviderType1");
    HandlerDescription[] handlers = id.getHandlers();
    assertEquals("Number of handlers", handlers.length, 3);
    // Look for the ProvidedService Handler
    ProvidedServiceHandlerDescription pshd = null;
    pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
    assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
    ProvidedServiceDescription[] ps = pshd.getProvidedServices();
    assertEquals("Check ProvidedService number", ps.length, 1);
    assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 1);
    assertEquals("Check Provided Service Specs - 2", ps[0].getServiceSpecifications()[0], FooService.class.getName());
    assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
    Properties prop = ps[0].getProperties();
    assertNotNull("Check Props", prop);
    assertEquals("Check service properties number (#" + prop + "?=5)", prop.size(), 5);
    assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
    assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
    assertEquals("Check foo property", prop.get("foo"), "foo");
    assertEquals("Check bar property", prop.get("bar"), new Integer(2));
    assertEquals("Check baz property", prop.get("baz"), "baz");
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) ServiceReference(org.osgi.framework.ServiceReference) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) Test(org.junit.Test)

Example 2 with Architecture

use of org.apache.felix.ipojo.architecture.Architecture in project felix by apache.

the class TestProvidedServiceArchitecture method testDoubleProviding.

@Test
public void testDoubleProviding() {
    String factName = "PS-FooBarProviderType-1";
    String compName = "FooProvider";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider", fact);
    ipojoHelper.createComponentInstance(factName, compName);
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
    assertNotNull("Architecture not available", arch_ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
    InstanceDescription id = arch.getInstanceDescription();
    assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
    assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooBarProviderType1");
    HandlerDescription[] handlers = id.getHandlers();
    assertEquals("Number of handlers", handlers.length, 2);
    // Look for the ProvidedService Handler
    ProvidedServiceHandlerDescription pshd = null;
    pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
    // for(int i = 0; i < handlers.length; i++) {
    // if(handlers[i].getHandlerName().equals("org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler")) {
    // pshd = (ProvidedServiceHandlerDescription) handlers[i];
    // }
    // }
    assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
    ProvidedServiceDescription[] ps = pshd.getProvidedServices();
    assertEquals("Check ProvidedService number", ps.length, 1);
    assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 2);
    assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), FooService.class.getName());
    assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), BarService.class.getName());
    assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) Factory(org.apache.felix.ipojo.Factory) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with Architecture

use of org.apache.felix.ipojo.architecture.Architecture in project felix by apache.

the class HandlerTest method testAvailability.

@Test
public void testAvailability() {
    String name = "HandlerTest-1";
    ServiceReference sr = null;
    ServiceReference[] refs = null;
    String filter = "(" + "instance.name" + "=" + name + ")";
    refs = osgiHelper.getServiceReferences(CheckService.class.getName(), filter);
    if (refs != null) {
        sr = refs[0];
    }
    assertNotNull("Check the check service availability", sr);
    ServiceReference sr_arch = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "HandlerTest-1");
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(sr_arch);
    assertEquals("Check validity", arch.getInstanceDescription().getState(), ComponentInstance.VALID);
    // Kill the handler factory
    HandlerManagerFactory f = (HandlerManagerFactory) ipojoHelper.getHandlerFactory("check");
    f.stop();
    boolean av = ipojoHelper.isServiceAvailableByName(CheckService.class.getName(), "HandlerTest-1");
    assertFalse("Check the check service unavailability", av);
    boolean av2 = ipojoHelper.isServiceAvailableByName(Architecture.class.getName(), "HandlerTest-1");
    assertFalse("Check the architecture unavailability", av2);
    // The instance is disposed, restart the handler
    f.start();
    Properties props = new Properties();
    props.put("instance.name", "HandlerTest-1");
    props.put("csh.simple", "simple");
    Properties p = new Properties();
    p.put("a", "a");
    p.put("b", "b");
    p.put("c", "c");
    props.put("csh.map", p);
    instance = ipojoHelper.createComponentInstance("HANDLER-HandlerTester", props);
    sr = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), "HandlerTest-1");
    assertNotNull("Check the check service availability - 2", sr);
    sr_arch = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "HandlerTest-1");
    arch = (Architecture) osgiHelper.getRawServiceObject(sr_arch);
    assertEquals("Check validity - 2", arch.getInstanceDescription().getState(), ComponentInstance.VALID);
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerManagerFactory(org.apache.felix.ipojo.HandlerManagerFactory) CheckService(org.apache.felix.ipojo.runtime.externalhandlers.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with Architecture

use of org.apache.felix.ipojo.architecture.Architecture in project felix by apache.

the class TestMethodDelayedMultipleDependencies method testRef.

@Test
public void testRef() {
    instance4.start();
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance4.getInstanceName());
    assertNotNull("Check architecture availability", arch_ref);
    InstanceDescription id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id.getState() == ComponentInstance.VALID);
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance4.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    Properties props = cs.getProps();
    // Check properties
    // True, a provider is here
    assertTrue("check CheckService invocation - 1", ((Boolean) props.get("result")).booleanValue());
    assertEquals("check void bind invocation - 1", ((Integer) props.get("voidB")).intValue(), 0);
    assertEquals("check void unbind callback invocation - 1", ((Integer) props.get("voidU")).intValue(), 0);
    assertEquals("check object bind callback invocation - 1", ((Integer) props.get("objectB")).intValue(), 0);
    assertEquals("check object unbind callback invocation - 1", ((Integer) props.get("objectU")).intValue(), 0);
    assertEquals("check ref bind callback invocation - 1", ((Integer) props.get("refB")).intValue(), 2);
    assertEquals("check ref unbind callback invocation - 1", ((Integer) props.get("refU")).intValue(), 0);
    assertEquals("Check FS invocation (int) - 1", ((Integer) props.get("int")).intValue(), 2);
    assertEquals("Check FS invocation (long) - 1", ((Long) props.get("long")).longValue(), 2);
    assertEquals("Check FS invocation (double) - 1", ((Double) props.get("double")).doubleValue(), 2.0, 0);
    fooProvider1.stop();
    id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    props = cs.getProps();
    // Check properties
    // True, two providers are here
    assertTrue("check CheckService invocation - 3", ((Boolean) props.get("result")).booleanValue());
    assertEquals("check void bind invocation - 3", ((Integer) props.get("voidB")).intValue(), 0);
    assertEquals("check void unbind callback invocation - 3", ((Integer) props.get("voidU")).intValue(), 0);
    assertEquals("check object bind callback invocation - 3", ((Integer) props.get("objectB")).intValue(), 0);
    assertEquals("check object unbind callback invocation - 3", ((Integer) props.get("objectU")).intValue(), 0);
    assertEquals("check ref bind callback invocation - 3", ((Integer) props.get("refB")).intValue(), 2);
    assertEquals("check ref unbind callback invocation - 3", ((Integer) props.get("refU")).intValue(), 1);
    assertEquals("Check FS invocation (int) - 3", ((Integer) props.get("int")).intValue(), 1);
    assertEquals("Check FS invocation (long) - 3", ((Long) props.get("long")).longValue(), 1);
    assertEquals("Check FS invocation (double) - 3", ((Double) props.get("double")).doubleValue(), 1.0, 0);
    fooProvider2.stop();
    id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 4", id.getState() == ComponentInstance.INVALID);
    id = null;
    cs = null;
    getContext().ungetService(arch_ref);
    instance4.stop();
    getContext().ungetService(cs_ref);
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with Architecture

use of org.apache.felix.ipojo.architecture.Architecture in project felix by apache.

the class TestMethodDelayedMultipleDependencies method testMap.

@Test
public void testMap() {
    instance6.start();
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance6.getInstanceName());
    assertNotNull("Check architecture availability", arch_ref);
    InstanceDescription id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance invalidity - 1", id.getState() == ComponentInstance.VALID);
    ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance6.getInstanceName());
    assertNotNull("Check CheckService availability", cs_ref);
    CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    Properties props = cs.getProps();
    // Check properties
    // True, a provider is here
    assertTrue("check CheckService invocation - 1", ((Boolean) props.get("result")).booleanValue());
    assertEquals("check void bind invocation - 1", ((Integer) props.get("voidB")).intValue(), 0);
    assertEquals("check void unbind callback invocation - 1", ((Integer) props.get("voidU")).intValue(), 0);
    assertEquals("check object bind callback invocation - 1", ((Integer) props.get("objectB")).intValue(), 0);
    assertEquals("check object unbind callback invocation - 1", ((Integer) props.get("objectU")).intValue(), 0);
    assertEquals("check both bind callback invocation - 1", ((Integer) props.get("bothB")).intValue(), 0);
    assertEquals("check both unbind callback invocation - 1", ((Integer) props.get("bothU")).intValue(), 0);
    assertEquals("check map bind callback invocation - 1", ((Integer) props.get("mapB")).intValue(), 2);
    assertEquals("check map unbind callback invocation - 1", ((Integer) props.get("mapU")).intValue(), 0);
    assertEquals("check dict bind callback invocation - 1", ((Integer) props.get("dictB")).intValue(), 0);
    assertEquals("check dict unbind callback invocation - 1", ((Integer) props.get("dictU")).intValue(), 0);
    assertEquals("Check FS invocation (int) - 1", ((Integer) props.get("int")).intValue(), 2);
    assertEquals("Check FS invocation (long) - 1", ((Long) props.get("long")).longValue(), 2);
    assertEquals("Check FS invocation (double) - 1", ((Double) props.get("double")).doubleValue(), 2.0, 0);
    fooProvider1.stop();
    id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
    cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
    props = cs.getProps();
    // Check properties
    // True, two providers are here
    assertTrue("check CheckService invocation - 3", ((Boolean) props.get("result")).booleanValue());
    assertEquals("check void bind invocation - 3", ((Integer) props.get("voidB")).intValue(), 0);
    assertEquals("check void unbind callback invocation - 3", ((Integer) props.get("voidU")).intValue(), 0);
    assertEquals("check object bind callback invocation - 3", ((Integer) props.get("objectB")).intValue(), 0);
    assertEquals("check object unbind callback invocation - 3", ((Integer) props.get("objectU")).intValue(), 0);
    assertEquals("check both bind callback invocation - 3", ((Integer) props.get("bothB")).intValue(), 0);
    assertEquals("check both unbind callback invocation - 3", ((Integer) props.get("bothU")).intValue(), 0);
    assertEquals("check map bind callback invocation - 3", ((Integer) props.get("mapB")).intValue(), 2);
    assertEquals("check map unbind callback invocation - 3", ((Integer) props.get("mapU")).intValue(), 1);
    assertEquals("check dict bind callback invocation - 3", ((Integer) props.get("dictB")).intValue(), 0);
    assertEquals("check dict unbind callback invocation - 3", ((Integer) props.get("dictU")).intValue(), 0);
    assertEquals("Check FS invocation (int) - 3", ((Integer) props.get("int")).intValue(), 1);
    assertEquals("Check FS invocation (long) - 3", ((Long) props.get("long")).longValue(), 1);
    assertEquals("Check FS invocation (double) - 3", ((Double) props.get("double")).doubleValue(), 1.0, 0);
    fooProvider2.stop();
    id = ((Architecture) osgiHelper.getRawServiceObject(arch_ref)).getInstanceDescription();
    assertTrue("Check instance validity - 4", id.getState() == ComponentInstance.INVALID);
    id = null;
    cs = null;
    getContext().ungetService(arch_ref);
    instance6.stop();
    getContext().ungetService(cs_ref);
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

Architecture (org.apache.felix.ipojo.architecture.Architecture)276 Test (org.junit.Test)267 ServiceReference (org.osgi.framework.ServiceReference)261 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)231 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)218 Properties (java.util.Properties)205 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)20 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)15 Dictionary (java.util.Dictionary)14 IOException (java.io.IOException)13 Configuration (org.osgi.service.cm.Configuration)13 FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)12 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)9 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)8 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)7 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)6 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)5 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)4 Factory (org.apache.felix.ipojo.Factory)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4