use of org.apache.felix.ipojo.PrimitiveInstanceDescription in project felix by apache.
the class TestManagedServiceTestForService method testConfigurationPushedBeforeInstantiationUsingFactoryAndReconfiguration.
@Test
public void testConfigurationPushedBeforeInstantiationUsingFactoryAndReconfiguration() {
// The configuration exists before the instance creation.
// Update
Configuration configuration;
try {
configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
Dictionary prc = configuration.getProperties();
if (prc == null) {
prc = new Properties();
}
prc.put("message", "message2");
configuration.update(prc);
grace();
} catch (Exception e) {
fail(e.getMessage());
}
Properties props = new Properties();
props.put("managed.service.pid", msp);
props.put("message", "message");
ComponentInstance instance = null;
try {
instance = factSvc.createComponentInstance(props);
grace();
} catch (Exception e) {
fail(e.getMessage());
}
ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
assertNotNull("FS availability", ref);
FooService fs = (FooService) bc.getService(ref);
Properties p = fs.fooProps();
String mes = p.getProperty("message");
int count = (Integer) p.get("count");
assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
// Already reconfigured.
assertEquals("Check message", "message2", mes);
assertEquals("Check count", 1, count);
// Reconfiguration
try {
configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
Dictionary prc = configuration.getProperties();
if (prc == null) {
prc = new Properties();
}
prc.put("message", "message3");
configuration.update(prc);
grace();
} catch (Exception e) {
fail(e.getMessage());
}
instance.dispose();
// Recreation of the instance.
props = new Properties();
props.put("managed.service.pid", msp);
props.put("message", "message");
instance = null;
try {
instance = factSvc.createComponentInstance(props);
grace();
} catch (Exception e) {
fail(e.getMessage());
}
ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
assertNotNull("FS availability", ref);
fs = (FooService) bc.getService(ref);
p = fs.fooProps();
mes = p.getProperty("message");
count = (Integer) p.get("count");
assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
// Already reconfigured.
assertEquals("Check message", "message3", mes);
assertEquals("Check count", 1, count);
instance.dispose();
}
use of org.apache.felix.ipojo.PrimitiveInstanceDescription 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.PrimitiveInstanceDescription in project felix by apache.
the class TestPOJOCreation method testImmediateCreation.
/**
* Check immediate creation.
*/
@Test
public void testImmediateCreation() {
assertEquals("Check that one object is created ", 1, ((PrimitiveInstanceDescription) immeArch.getInstanceDescription()).getCreatedObjects().length);
ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_immediate.getInstanceName());
assertNotNull("Check that a FooService from " + ci_immediate.getInstanceName() + " is available", ref);
FooService fs = (FooService) osgiHelper.getServiceObject(ref);
assertTrue("Check the FooService invocation", fs.foo());
assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) immeArch.getInstanceDescription()).getCreatedObjects().length);
}
use of org.apache.felix.ipojo.PrimitiveInstanceDescription in project felix by apache.
the class TestPOJOCreation method testLazyCreationSingletonM.
/**
* Check creation through a factory method.
* (lazy & singleton creation)
*/
@Test
public void testLazyCreationSingletonM() {
assertEquals("Check that no objects are created ", 0, ((PrimitiveInstanceDescription) lazzyArchSingM.getInstanceDescription()).getCreatedObjects().length);
ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy_singM.getInstanceName());
assertNotNull("Check that a FooService from " + ci_lazzy_singM.getInstanceName() + " is available", ref);
FooService fs = (FooService) osgiHelper.getServiceObject(ref);
FooService fs2 = (FooService) osgiHelper.getServiceObject(ref);
assertTrue("Check the FooService invocation", fs.foo());
assertTrue("Check the FooService invocation", fs2.foo());
assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) lazzyArchSingM.getInstanceDescription()).getCreatedObjects().length);
}
use of org.apache.felix.ipojo.PrimitiveInstanceDescription in project felix by apache.
the class TestPOJOCreation method testLazyCreationSeveral.
/**
* Check lazy and "several" creation.
*/
@Test
public void testLazyCreationSeveral() {
assertEquals("Check that no objects are created ", 0, ((PrimitiveInstanceDescription) lazzyArchSev.getInstanceDescription()).getCreatedObjects().length);
ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), ci_lazzy_sev.getInstanceName());
assertNotNull("Check that a FooService from " + ci_lazzy_sev.getInstanceName() + " is available", ref);
FooService fs = (FooService) osgiHelper.getServiceObject(ref);
FooService fs2 = (FooService) osgiHelper.getServiceObject(ref);
assertTrue("Check the FooService invocation", fs.foo());
assertTrue("Check the FooService invocation-2", fs2.foo());
assertEquals("Check the creation of 1 object", 1, ((PrimitiveInstanceDescription) lazzyArchSev.getInstanceDescription()).getCreatedObjects().length);
}
Aggregations