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());
}
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");
}
}
}
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);
}
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);
}
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);
}
Aggregations