Search in sources :

Example 61 with Factory

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

the class TestInstances method testNoInstances.

@Test
public void testNoInstances() {
    Factory factory = ipojoHelper.getFactory("NoInstances");
    assertNotNull(factory);
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(0, factory.getInstancesNames().size());
    assertEquals(0, factory.getInstances().size());
}
Also used : Factory(org.apache.felix.ipojo.Factory) Test(org.junit.Test)

Example 62 with Factory

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

the class TestDynamicPropsReconfiguration method testFactoryReconfString.

@Test
public void testFactoryReconfString() {
    ServiceReference sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    Integer intProp = (Integer) sr.getProperty("int");
    Boolean boolProp = (Boolean) sr.getProperty("boolean");
    String strProp = (String) sr.getProperty("string");
    String[] strAProp = (String[]) sr.getProperty("strAProp");
    int[] intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(0));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "");
    assertNotNull("Check strAProp not nullity", strAProp);
    String[] v = new String[0];
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    int[] v2 = new int[0];
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Reconfiguration
    ServiceReference fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    Factory fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    Properties p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{1, 2, 3}");
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, new Boolean(true));
    assertEquals("Check strProp equality", strProp, new String("foo"));
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
    // Invoke
    FooService fs = (FooService) osgiHelper.getRawServiceObject(sr);
    assertTrue("invoke fs", fs.foo());
    // Re-check the property (change)
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(2));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNull("Check intAProp hiding (no value)", intAProp);
    // Reconfiguration
    fact_ref = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "PS-FooProviderType-Dyn2");
    fact = (Factory) osgiHelper.getRawServiceObject(fact_ref);
    p3 = new Properties();
    p3.put("instance.name", "FooProvider-3");
    p3.put("int", "1");
    p3.put("boolean", "true");
    p3.put("string", "foo");
    p3.put("strAProp", "{foo, bar, baz}");
    p3.put("intAProp", "{ 1, 2, 3}");
    try {
        fact.reconfigure(p3);
    } catch (Exception e) {
        fail("Unable to reconfigure the instance with : " + p3);
    }
    sr = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "FooProvider-3");
    assertNotNull("Check the availability of the FS service", sr);
    // Check service properties
    intProp = (Integer) sr.getProperty("int");
    boolProp = (Boolean) sr.getProperty("boolean");
    strProp = (String) sr.getProperty("string");
    strAProp = (String[]) sr.getProperty("strAProp");
    intAProp = (int[]) sr.getProperty("intAProp");
    assertEquals("Check intProp equality", intProp, new Integer(1));
    assertEquals("Check longProp equality", boolProp, Boolean.TRUE);
    assertEquals("Check strProp equality", strProp, "foo");
    assertNotNull("Check strAProp not nullity", strAProp);
    v = new String[] { "foo", "bar", "baz" };
    for (int i = 0; i < strAProp.length; i++) {
        if (!strAProp[i].equals(v[i])) {
            fail("Check the strAProp Equality");
        }
    }
    assertNotNull("Check intAProp not nullity", intAProp);
    v2 = new int[] { 1, 2, 3 };
    for (int i = 0; i < intAProp.length; i++) {
        if (intAProp[i] != v2[i]) {
            fail("Check the intAProp Equality");
        }
    }
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 63 with Factory

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

the class TestNullCheck method testNull.

@Test
public void testNull() {
    String factName = "PS-Null";
    String compName = "NullCheck";
    ServiceReference ref = null;
    // Check that no Foo Service are available
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS already available", ref);
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    // Don't give any configuration so, properties are null.
    ipojoHelper.createComponentInstance(factName, compName);
    // Get a FooService provider
    ref = osgiHelper.getServiceReference(FooService.class.getName(), "(" + "instance.name" + "=" + compName + ")");
    assertNotNull("FS not available", ref);
    // Check service properties
    assertNull(ref.getProperty("prop1"));
    assertNotNull(ref.getProperty("prop2"));
    // Test foo invocation
    FooService fs = (FooService) osgiHelper.getRawServiceObject(ref);
    assertTrue("FooService invocation failed", fs.foo());
    ref = osgiHelper.getServiceReference(FooService.class.getName(), "(" + "instance.name" + "=" + compName + ")");
    // Check service properties
    assertNotNull(ref.getProperty("prop1"));
    assertNull(ref.getProperty("prop2"));
    ipojoHelper.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName(), null);
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 64 with Factory

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

the class TestOSGiProperties method testOSGiProperties.

@Test
public void testOSGiProperties() {
    String factName = "PS-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref = null;
    // Check that no Foo Service are available
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS already available", ref);
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    Dictionary conf = new Properties();
    conf.put(Constants.SERVICE_DESCRIPTION, "description");
    conf.put(Constants.SERVICE_RANKING, "10");
    conf.put(Constants.SERVICE_VENDOR, "ASF");
    conf.put(Constants.SERVICE_PID, "my.pid");
    ipojoHelper.createComponentInstance(factName, compName, conf);
    // Get a FooService provider
    ref = osgiHelper.getServiceReference(FooService.class.getName(), "(" + "instance.name" + "=" + compName + ")");
    assertNotNull("FS not available", ref);
    // Check properties
    assertEquals("description", ref.getProperty(Constants.SERVICE_DESCRIPTION));
    assertEquals(new Integer(10), ref.getProperty(Constants.SERVICE_RANKING));
    assertEquals("ASF", ref.getProperty(Constants.SERVICE_VENDOR));
    assertEquals("my.pid", ref.getProperty(Constants.SERVICE_PID));
    // Test foo invocation
    FooService fs = (FooService) osgiHelper.getRawServiceObject(ref);
    assertTrue("FooService invocation failed", fs.foo());
    ipojoHelper.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName(), null);
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Dictionary(java.util.Dictionary) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 65 with Factory

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

the class TestProvidedServiceArchitecture method testExposition.

@Test
public void testExposition() {
    String factName = "PS-FooProviderType-1";
    String compName = "FooProvider-1";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", 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.FooProviderType1");
    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, 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.size(), 2);
    assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
    assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
}
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) Properties(java.util.Properties) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) 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