Search in sources :

Example 56 with ServiceEvent

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());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) Logger(org.apache.aries.jmx.Logger) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ServiceReference(org.osgi.framework.ServiceReference) ObjectName(javax.management.ObjectName) AllServiceListener(org.osgi.framework.AllServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) ExecutorService(java.util.concurrent.ExecutorService) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Aggregations

ServiceEvent (org.osgi.framework.ServiceEvent)56 ServiceReference (org.osgi.framework.ServiceReference)30 Test (org.junit.Test)24 BundleContext (org.osgi.framework.BundleContext)23 ServiceListener (org.osgi.framework.ServiceListener)20 Bundle (org.osgi.framework.Bundle)18 Hashtable (java.util.Hashtable)14 ServiceRegistration (org.osgi.framework.ServiceRegistration)12 Collection (java.util.Collection)6 IOException (java.io.IOException)5 Dictionary (java.util.Dictionary)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 HashMap (java.util.HashMap)4 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3