use of org.osgi.framework.ServiceEvent 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());
}
Aggregations