use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestListeners method testNullProvidedServiceListener.
@Test(expected = NullPointerException.class)
public void testNullProvidedServiceListener() {
ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Controller-1-default");
ProvidedServiceHandlerDescription pshd = (ProvidedServiceHandlerDescription) ci.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:provides");
ProvidedServiceDescription ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
// Should fail!
ps.addListener(null);
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestListeners method testProvidedServiceListener.
/**
* Test that the listeners registered on the tested instance are called when the instance provided service is
* registered, updated or unregistered.
*/
@Test
public void testProvidedServiceListener() {
ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Controller-1-default");
ProvidedServiceHandlerDescription pshd = (ProvidedServiceHandlerDescription) ci.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:provides");
ProvidedServiceDescription ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
// Controller set to true.
osgiHelper.waitForService(FooService.class.getName(), null, 5000);
osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
assertNotNull(check);
// Register listeners :
// 1- CountingListener l1
// 2- ThrowingListener bad
// 3- TotalCountingListener l2
// 4- AppendingListener l3
ProvidedServiceListener l1 = new CountingListener();
ps.addListener(l1);
ProvidedServiceListener bad = new ThrowingListener();
ps.addListener(bad);
ProvidedServiceListener l2 = new TotalCountingListener();
ps.addListener(l2);
ProvidedServiceListener l3 = new AppendingListener();
ps.addListener(l3);
// Check initial valued are untouched
assertEquals(0, registrations);
assertEquals(0, unregistrations);
assertEquals(0, updates);
assertEquals(0, total);
// Unregister the service and check.
assertFalse(check.check());
assertEquals(0, registrations);
assertEquals(1, unregistrations);
assertEquals(0, updates);
assertEquals(1, total);
// Modify the service while it is unregistered. Nothing should move.
Hashtable<String, Object> props = new Hashtable<String, Object>();
props.put("change1", "1");
ps.addProperties(props);
assertEquals(0, registrations);
assertEquals(1, unregistrations);
assertEquals(0, updates);
assertEquals(1, total);
// Register the service and check.
assertTrue(check.check());
assertEquals(1, registrations);
assertEquals(1, unregistrations);
assertEquals(0, updates);
assertEquals(2, total);
// Modify the service while it is REGISTERED
props.clear();
props.put("change2", "2");
ps.addProperties(props);
assertEquals(1, registrations);
assertEquals(1, unregistrations);
assertEquals(1, updates);
assertEquals(3, total);
// One more time, just to be sure...
// Unregister
assertFalse(check.check());
assertEquals(1, registrations);
assertEquals(2, unregistrations);
assertEquals(1, updates);
assertEquals(4, total);
// Register
assertTrue(check.check());
assertEquals(2, registrations);
assertEquals(2, unregistrations);
assertEquals(1, updates);
assertEquals(5, total);
// Unregister the listener
ps.removeListener(l1);
ps.removeListener(bad);
ps.removeListener(l2);
ps.removeListener(l3);
// Play with the controller and check that nothing moves
// Unregister
assertFalse(check.check());
assertEquals(2, registrations);
assertEquals(2, unregistrations);
assertEquals(1, updates);
assertEquals(5, total);
// Register
assertTrue(check.check());
assertEquals(2, registrations);
assertEquals(2, unregistrations);
assertEquals(1, updates);
assertEquals(5, total);
// Modify
props.clear();
// Modify
props.put("change3", "3");
// Modify
ps.addProperties(props);
assertEquals(2, registrations);
assertEquals(2, unregistrations);
assertEquals(1, updates);
assertEquals(5, total);
// Check that instances contains $total times the ci component instance, and nothing else.
assertEquals(5, instances.size());
instances.removeAll(Collections.singleton(ci));
assertEquals(0, instances.size());
ci.dispose();
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription 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);
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestProvidedServiceArchitecture method testPropsNoValue.
@Test
public void testPropsNoValue() {
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);
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, 3);
// 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 + "?=5)", prop.size(), 2);
assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestServiceController method testArchitecture.
@Test
public void testArchitecture() {
ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Controller-1-default");
// Controller set to true.
osgiHelper.waitForService(FooService.class.getName(), null, 5000);
osgiHelper.waitForService(CheckService.class.getName(), null, 5000);
ProvidedServiceHandlerDescription pshd = null;
pshd = (ProvidedServiceHandlerDescription) ci.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:provides");
ProvidedServiceDescription ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
assertEquals("true", ps.getController());
CheckService check = (CheckService) osgiHelper.getServiceObject(CheckService.class.getName(), null);
assertNotNull(check);
assertFalse(check.check());
ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
assertEquals("false", ps.getController());
assertTrue(check.check());
ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
assertEquals("true", ps.getController());
}
Aggregations