use of org.osgi.framework.AllServiceListener in project aries by apache.
the class ServiceStateTest method createService.
private void createService(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
BundleContext context = mock(BundleContext.class);
Logger logger = mock(Logger.class);
ServiceState serviceState = new ServiceState(context, stateConfig, logger);
ServiceReference reference = mock(ServiceReference.class);
Bundle b1 = mock(Bundle.class);
when(b1.getBundleId()).thenReturn(new Long(9));
when(b1.getSymbolicName()).thenReturn("bundle");
when(b1.getLocation()).thenReturn("file:/location");
when(reference.getBundle()).thenReturn(b1);
when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { "org.apache.aries.jmx.Mock" });
when(context.getAllServiceReferences(null, null)).thenReturn(new ServiceReference[] { reference });
ServiceEvent registeredEvent = mock(ServiceEvent.class);
when(registeredEvent.getServiceReference()).thenReturn(reference);
when(registeredEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
ServiceEvent modifiedEvent = mock(ServiceEvent.class);
when(modifiedEvent.getServiceReference()).thenReturn(reference);
when(modifiedEvent.getType()).thenReturn(ServiceEvent.MODIFIED);
MBeanServer server = mock(MBeanServer.class);
//setup for notification
ObjectName objectName = new ObjectName(OBJECTNAME);
serviceState.preRegister(server, objectName);
serviceState.postRegister(true);
//add NotificationListener to receive the events
serviceState.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
} else {
received.add(notification);
}
}
}, null, null);
// capture the ServiceListener registered with BundleContext to issue ServiceEvents
ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
verify(context).addServiceListener(argument.capture());
//send events
AllServiceListener serviceListener = argument.getValue();
serviceListener.serviceChanged(registeredEvent);
serviceListener.serviceChanged(modifiedEvent);
//shutdown dispatcher via unregister callback
serviceState.postDeregister();
//check the ServiceListener is cleaned up
verify(context).removeServiceListener(serviceListener);
ExecutorService dispatcher = serviceState.getEventDispatcher();
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
use of org.osgi.framework.AllServiceListener in project aries by apache.
the class ServiceStateTest method testLifeCycleOfNotificationSupport.
@Test
public void testLifeCycleOfNotificationSupport() throws Exception {
BundleContext context = mock(BundleContext.class);
Logger logger = mock(Logger.class);
ServiceState serviceState = new ServiceState(context, new StateConfig(), logger);
MBeanServer server1 = mock(MBeanServer.class);
MBeanServer server2 = mock(MBeanServer.class);
ObjectName objectName = new ObjectName(OBJECTNAME);
serviceState.preRegister(server1, objectName);
serviceState.postRegister(true);
// capture the ServiceListener registered with BundleContext to issue ServiceEvents
ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
verify(context).addServiceListener(argument.capture());
AllServiceListener serviceListener = argument.getValue();
assertNotNull(serviceListener);
ExecutorService dispatcher = serviceState.getEventDispatcher();
//do registration with another server
serviceState.preRegister(server2, objectName);
serviceState.postRegister(true);
// check no more actions on BundleContext
argument = ArgumentCaptor.forClass(AllServiceListener.class);
verify(context, atMost(1)).addServiceListener(argument.capture());
assertEquals(1, argument.getAllValues().size());
//do one unregister
serviceState.postDeregister();
//verify bundleListener not invoked
verify(context, never()).removeServiceListener(serviceListener);
assertFalse(dispatcher.isShutdown());
//do second unregister and check cleanup
serviceState.postDeregister();
verify(context).removeServiceListener(serviceListener);
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
Aggregations