Here are the examples of the java api class org.osgi.framework.BundleListener taken from open source projects.
1. MockBundleContextTest#testBundleListener()
Project: sling
File: MockBundleContextTest.java
File: MockBundleContextTest.java
@Test public void testBundleListener() throws Exception { BundleListener bundleListener = mock(BundleListener.class); BundleEvent bundleEvent = mock(BundleEvent.class); bundleContext.addBundleListener(bundleListener); MockOsgi.sendBundleEvent(bundleContext, bundleEvent); verify(bundleListener).bundleChanged(bundleEvent); bundleContext.removeBundleListener(bundleListener); }
2. BundleStateTest#createBundle()
Project: aries
File: BundleStateTest.java
File: BundleStateTest.java
private void createBundle(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception { BundleContext context = mock(BundleContext.class); when(context.getBundles()).thenReturn(new Bundle[] {}); PackageAdmin admin = mock(PackageAdmin.class); StartLevel startLevel = mock(StartLevel.class); Logger logger = mock(Logger.class); BundleState bundleState = new BundleState(context, admin, startLevel, stateConfig, logger); Bundle b1 = mock(Bundle.class); when(b1.getBundleId()).thenReturn(new Long(9)); when(b1.getSymbolicName()).thenReturn("bundle"); when(b1.getLocation()).thenReturn("file:/location"); BundleEvent installedEvent = mock(BundleEvent.class); when(installedEvent.getBundle()).thenReturn(b1); when(installedEvent.getType()).thenReturn(BundleEvent.INSTALLED); BundleEvent resolvedEvent = mock(BundleEvent.class); when(resolvedEvent.getBundle()).thenReturn(b1); when(resolvedEvent.getType()).thenReturn(BundleEvent.RESOLVED); MBeanServer server = mock(MBeanServer.class); //setup for notification ObjectName objectName = new ObjectName(OBJECTNAME); bundleState.preRegister(server, objectName); bundleState.postRegister(true); //add NotificationListener to receive the events bundleState.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 BundleListener registered with BundleContext to issue BundleEvents ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class); verify(context).addBundleListener(argument.capture()); //send events BundleListener listener = argument.getValue(); listener.bundleChanged(installedEvent); listener.bundleChanged(resolvedEvent); //shutdown dispatcher via unregister callback bundleState.postDeregister(); //check the BundleListener is cleaned up verify(context).removeBundleListener(listener); ExecutorService dispatcher = bundleState.getEventDispatcher(); assertTrue(dispatcher.isShutdown()); dispatcher.awaitTermination(2, TimeUnit.SECONDS); assertTrue(dispatcher.isTerminated()); }
3. BundleStateTest#createBundle()
Project: apache-aries
File: BundleStateTest.java
File: BundleStateTest.java
private void createBundle(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception { BundleContext context = mock(BundleContext.class); when(context.getBundles()).thenReturn(new Bundle[] {}); PackageAdmin admin = mock(PackageAdmin.class); StartLevel startLevel = mock(StartLevel.class); Logger logger = mock(Logger.class); BundleState bundleState = new BundleState(context, admin, startLevel, stateConfig, logger); Bundle b1 = mock(Bundle.class); when(b1.getBundleId()).thenReturn(new Long(9)); when(b1.getSymbolicName()).thenReturn("bundle"); when(b1.getLocation()).thenReturn("file:/location"); BundleEvent installedEvent = mock(BundleEvent.class); when(installedEvent.getBundle()).thenReturn(b1); when(installedEvent.getType()).thenReturn(BundleEvent.INSTALLED); BundleEvent resolvedEvent = mock(BundleEvent.class); when(resolvedEvent.getBundle()).thenReturn(b1); when(resolvedEvent.getType()).thenReturn(BundleEvent.RESOLVED); MBeanServer server = mock(MBeanServer.class); //setup for notification ObjectName objectName = new ObjectName(OBJECTNAME); bundleState.preRegister(server, objectName); bundleState.postRegister(true); //add NotificationListener to receive the events bundleState.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 BundleListener registered with BundleContext to issue BundleEvents ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class); verify(context).addBundleListener(argument.capture()); //send events BundleListener listener = argument.getValue(); listener.bundleChanged(installedEvent); listener.bundleChanged(resolvedEvent); //shutdown dispatcher via unregister callback bundleState.postDeregister(); //check the BundleListener is cleaned up verify(context).removeBundleListener(listener); ExecutorService dispatcher = bundleState.getEventDispatcher(); assertTrue(dispatcher.isShutdown()); dispatcher.awaitTermination(2, TimeUnit.SECONDS); assertTrue(dispatcher.isTerminated()); }
4. MockBundleContext#sendBundleEvent()
Project: sling
File: MockBundleContext.java
File: MockBundleContext.java
void sendBundleEvent(BundleEvent bundleEvent) { for (BundleListener bundleListener : bundleListeners) { bundleListener.bundleChanged(bundleEvent); } }
5. OSGiManagement#start()
Project: communote-server
File: OSGiManagement.java
File: OSGiManagement.java
/** * Starts the framework. There is usually no reason to call this method manually because it will * be invoked automatically by the {@link #initializationComplete()} callback. * * @throws BundleException * Exception. */ public synchronized void start() throws BundleException { if (this.framework == null) { this.prepareFramework(); } for (BundleListener listener : listeners) { framework.getBundleContext().addBundleListener(listener); } framework.start(); fileInstall.updated("initial", propertiesDictionary); }
6. BundleStateTest#testLifeCycleOfNotificationSupport()
Project: aries
File: BundleStateTest.java
File: BundleStateTest.java
@Test public void testLifeCycleOfNotificationSupport() throws Exception { BundleContext context = mock(BundleContext.class); PackageAdmin admin = mock(PackageAdmin.class); StartLevel startLevel = mock(StartLevel.class); Logger logger = mock(Logger.class); BundleState bundleState = new BundleState(context, admin, startLevel, new StateConfig(), logger); MBeanServer server1 = mock(MBeanServer.class); MBeanServer server2 = mock(MBeanServer.class); ObjectName objectName = new ObjectName(OBJECTNAME); bundleState.preRegister(server1, objectName); bundleState.postRegister(true); // capture the BundleListener registered with BundleContext ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class); verify(context).addBundleListener(argument.capture()); assertEquals(1, argument.getAllValues().size()); BundleListener listener = argument.getValue(); assertNotNull(listener); ExecutorService dispatcher = bundleState.getEventDispatcher(); //do registration with another server bundleState.preRegister(server2, objectName); bundleState.postRegister(true); // check no more actions on BundleContext argument = ArgumentCaptor.forClass(BundleListener.class); verify(context, atMost(1)).addBundleListener(argument.capture()); assertEquals(1, argument.getAllValues().size()); //do one unregister bundleState.postDeregister(); //verify bundleListener not invoked verify(context, never()).removeBundleListener(listener); assertFalse(dispatcher.isShutdown()); //do second unregister and check cleanup bundleState.postDeregister(); verify(context).removeBundleListener(listener); assertTrue(dispatcher.isShutdown()); dispatcher.awaitTermination(2, TimeUnit.SECONDS); assertTrue(dispatcher.isTerminated()); }
7. BundleStateTest#testLifeCycleOfNotificationSupport()
Project: apache-aries
File: BundleStateTest.java
File: BundleStateTest.java
@Test public void testLifeCycleOfNotificationSupport() throws Exception { BundleContext context = mock(BundleContext.class); PackageAdmin admin = mock(PackageAdmin.class); StartLevel startLevel = mock(StartLevel.class); Logger logger = mock(Logger.class); BundleState bundleState = new BundleState(context, admin, startLevel, new StateConfig(), logger); MBeanServer server1 = mock(MBeanServer.class); MBeanServer server2 = mock(MBeanServer.class); ObjectName objectName = new ObjectName(OBJECTNAME); bundleState.preRegister(server1, objectName); bundleState.postRegister(true); // capture the BundleListener registered with BundleContext ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class); verify(context).addBundleListener(argument.capture()); assertEquals(1, argument.getAllValues().size()); BundleListener listener = argument.getValue(); assertNotNull(listener); ExecutorService dispatcher = bundleState.getEventDispatcher(); //do registration with another server bundleState.preRegister(server2, objectName); bundleState.postRegister(true); // check no more actions on BundleContext argument = ArgumentCaptor.forClass(BundleListener.class); verify(context, atMost(1)).addBundleListener(argument.capture()); assertEquals(1, argument.getAllValues().size()); //do one unregister bundleState.postDeregister(); //verify bundleListener not invoked verify(context, never()).removeBundleListener(listener); assertFalse(dispatcher.isShutdown()); //do second unregister and check cleanup bundleState.postDeregister(); verify(context).removeBundleListener(listener); assertTrue(dispatcher.isShutdown()); dispatcher.awaitTermination(2, TimeUnit.SECONDS); assertTrue(dispatcher.isTerminated()); }