use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.
the class TestCompositeAPI method testInstantiator.
@Test
public void testInstantiator() {
String type = "composite.instantiator";
Factory fact = ipojoHelper.getFactory(type);
ComponentInstance ci = null;
Properties p = new Properties();
p.put("instance.name", "mon_coeur");
try {
ci = fact.createComponentInstance(p);
} catch (Exception e) {
e.printStackTrace();
}
assertTrue("Check ci", ci.getState() == ComponentInstance.VALID);
ServiceReference ref = ipojoHelper.getServiceReferenceByName(BazService.class.getName(), "mon_coeur");
assertNotNull("Check ref", ref);
BazService bs = (BazService) getContext().getService(ref);
assertTrue("Check invocation", bs.foo());
getContext().ungetService(ref);
ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), "mon_coeur");
assertNotNull("Check ref 2 ", ref);
FooService fs = (FooService) getContext().getService(ref);
assertTrue("Check invocation", fs.foo());
getContext().ungetService(ref);
ci.dispose();
}
use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.
the class TestExceptionHandling method testException.
/**
* Check that the exception is correctly propagated.
*/
@Test
public void testException() {
ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy.getInstanceName());
assertNotNull("Check that a FooService from " + ci_lazzy.getInstanceName() + " is available", ref);
FooProviderType1 fs = (FooProviderType1) osgiHelper.getServiceObject(ref);
try {
fs.testException();
fail("The method must returns an exception");
} catch (Exception e) {
// OK
}
}
use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.
the class TestGenericList method testTypedList.
@Test
public void testTypedList() {
ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), checker.getInstanceName());
CheckService check = (CheckService) osgiHelper.getServiceObject(ref);
assertNotNull("Checker availability", check);
// Check without providers
assertFalse("Empty list", check.check());
// Start the first provider
foo1.start();
assertTrue("List with one element", check.check());
Properties props = check.getProps();
List<FooService> list = (List<FooService>) props.get("list");
assertEquals("Check size - 1", 1, list.size());
// Start the second provider
foo2.start();
assertTrue("List with two element", check.check());
props = check.getProps();
list = (List<FooService>) props.get("list");
assertEquals("Check size - 2", 2, list.size());
// Stop the first one
foo1.stop();
assertTrue("List with one element (2)", check.check());
props = check.getProps();
list = (List<FooService>) props.get("list");
assertEquals("Check size - 3", 1, list.size());
}
use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.
the class TestGetComponentInstance method testGetComponentInstance.
/**
* Check if a pojo can correctly be cast in POJO.
* Check the getComponentInstance method.
*/
@Test
public void testGetComponentInstance() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
String factName = "Manipulation-FooProviderType-1";
String compName = "FooProvider-1";
ServiceReference ref = null;
// Get the factory to create a component instance
Factory fact = ipojoHelper.getFactory(factName);
assertNotNull("Cannot find the factory FooProvider-1", fact);
Properties props = new Properties();
props.put("instance.name", compName);
ComponentInstance ci = null;
try {
ci = fact.createComponentInstance(props);
} catch (Exception e1) {
fail(e1.getMessage());
}
assertEquals("Check instance name", compName, ci.getInstanceName());
// Get a FooService provider
ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), compName);
assertNotNull("FS not available", ref);
// Get foo object
FooService fs = (FooService) osgiHelper.getServiceObject(ref);
// Cast to POJO
Pojo pojo = (Pojo) fs;
Field im = fs.getClass().getDeclaredField("__IM");
assertNotNull(im);
im.setAccessible(true);
assertNotNull(im.get(fs));
Method method = fs.getClass().getMethod("getComponentInstance");
assertNotNull(method);
// GetComponentInstance
ComponentInstance instance = pojo.getComponentInstance();
assertNotNull(instance);
assertEquals("Check component instance name", instance.getInstanceName(), compName);
assertEquals("Check component factory name", instance.getFactory().getName(), factName);
assertNotNull("Instance description not null", instance.getInstanceDescription());
PrimitiveInstanceDescription id = (PrimitiveInstanceDescription) instance.getInstanceDescription();
assertTrue("Check instance state", id.getState() == ComponentInstance.VALID);
assertEquals("Check created pojo count", id.getCreatedObjects().length, 1);
assertEquals("Check instance description name", id.getName(), compName);
ci.dispose();
// Check that there is no more FooService
ref = osgiHelper.getServiceReference(FooService.class.getName());
assertNull("FS available, but component instance stopped", ref);
}
use of org.apache.felix.ipojo.runtime.core.services.FooService in project felix by apache.
the class TestDelayedFilteredImport method testSimple.
@Test
public void testSimple() {
import1.start();
// Two providers
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
ServiceContext sc = getServiceContext(import1);
ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 1", refs);
assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
FooService fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
foo1.stop();
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
sc = getServiceContext(import1);
refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 1", refs);
assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
// Stop the second provider
foo2.stop();
assertTrue("Test component invalidity - 2", import1.getState() == ComponentInstance.INVALID);
foo2.start();
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
sc = getServiceContext(import1);
refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 3", refs);
assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
}
Aggregations