### Question:
SpringContextEventBridge implements OsgiBundleApplicationContextListener { public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent osgiEvent) { if (osgiEvent instanceof BootstrappingDependencyEvent) { OsgiServiceDependencyEvent event = ((BootstrappingDependencyEvent) osgiEvent).getDependencyEvent(); if (log.isDebugEnabled()) { log.debug("Handling osgi application context event: " + event); } String beanName = event.getServiceDependency().getBeanName(); String pluginKey = null; if (event.getSource() != null) { if (event.getSource() instanceof ConfigurableOsgiBundleApplicationContext) { Bundle bundle = ((ConfigurableOsgiBundleApplicationContext) event.getSource()).getBundle(); pluginKey = PluginBundleUtils.getPluginKey(bundle); } else { if (event.getSource() instanceof AbstractOsgiServiceImportFactoryBean) { AbstractOsgiServiceImportFactoryBean bean = ((AbstractOsgiServiceImportFactoryBean) event.getSource()); if (beanName == null) { beanName = bean.getBeanName(); } if (bean.getBundleContext() != null) { pluginKey = PluginBundleUtils.getPluginKey(bean.getBundleContext().getBundle()); } } } } if (pluginKey == null && log.isDebugEnabled()) { log.debug("Cannot determine the plugin key for event: " + event + " and source: " + event.getSource()); } if (event instanceof OsgiServiceDependencyWaitStartingEvent) { pluginEventManager.broadcast(new PluginServiceDependencyWaitStartingEvent( pluginKey, beanName, event.getServiceDependency().getServiceFilter(), ((OsgiServiceDependencyWaitStartingEvent) event).getTimeToWait())); } else { if (event instanceof OsgiServiceDependencyWaitEndedEvent) { pluginEventManager.broadcast(new PluginServiceDependencyWaitEndedEvent( pluginKey, beanName, event.getServiceDependency().getServiceFilter(), ((OsgiServiceDependencyWaitEndedEvent) event).getElapsedTime())); } else { if (event instanceof OsgiServiceDependencyWaitTimedOutEvent) { pluginEventManager.broadcast(new PluginServiceDependencyWaitTimedOutEvent( pluginKey, beanName, event.getServiceDependency().getServiceFilter(), ((OsgiServiceDependencyWaitTimedOutEvent) event).getElapsedTime())); } } } } } SpringContextEventBridge(PluginEventManager pluginEventManager); void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent osgiEvent);  }### Answer:
@Test public void testWaitingEventWithApplicationContext() throws Exception { ConfigurableOsgiBundleApplicationContext source = mock(ConfigurableOsgiBundleApplicationContext.class); when(source.getBundle()).thenReturn(bundle); OsgiServiceDependencyWaitStartingEvent startingEvent = new OsgiServiceDependencyWaitStartingEvent(source, mock(OsgiServiceDependency.class), 1000); BootstrappingDependencyEvent bootstrapEvent = new BootstrappingDependencyEvent(mock(ApplicationContext.class), bundle, startingEvent); bridge.onOsgiApplicationEvent(bootstrapEvent); verify(eventManager).broadcast(isPluginKey("foo")); }
@Test public void testWaitingEventWithServiceFactoryBean() throws Exception { AbstractOsgiServiceImportFactoryBean source = mock(AbstractOsgiServiceImportFactoryBean.class); when(source.getBeanName()).thenReturn("bar"); BundleContext ctx = mock(BundleContext.class); when(ctx.getBundle()).thenReturn(bundle); when(source.getBundleContext()).thenReturn(ctx); OsgiServiceDependencyWaitStartingEvent startingEvent = new OsgiServiceDependencyWaitStartingEvent(source, mock(OsgiServiceDependency.class), 1000); BootstrappingDependencyEvent bootstrapEvent = new BootstrappingDependencyEvent(mock(ApplicationContext.class), bundle, startingEvent); bridge.onOsgiApplicationEvent(bootstrapEvent); verify(eventManager).broadcast(isPluginKey("foo")); }