OSGIRuntimeImpl implements OSGIRuntime { @Override public void stopOSGI() throws BundleException, InterruptedException { if (bc != null) { Bundle[] bundles = bc.getBundles(); for (int i = bundles.length - 1; i >= 0; i--) { Bundle bundle = bundles[i]; if (!isFragment(bundle) && bundle.getState() == Bundle.ACTIVE) { try { bundle.stop(); logger.debug("Stopped bundle " + bundle.getLocation()); } catch (BundleException ex) { logger.error(ex, "Error stopping bundle {0}", bundle.getLocation()); } } } } bc = null; }  static OSGIRuntimeImpl getOSGIRuntime(); void setBundleContext(BundleContext context); @Override void startExternalBundles(); @Override synchronized BundleContext getBundleContext(); T getService(Class<T> serviceClass, String filter); @Override void stopOSGI();  }
@Test public void testStop() throws Exception { runtimeImpl.stopOSGI(); verifyInOrder(false); }
@Test public void testStopSkipInactive() throws Exception { Mockito.when(mockBundles[skipIndex].getState()).thenReturn(Bundle.STOPPING); runtimeImpl.stopOSGI(); verifyInOrder(true); }
@Test public void testStopSkipsFragments() throws Exception { Dictionary<String,String> mockFragmentDictionary = Mockito.mock(Dictionary.class); Mockito.when(mockFragmentDictionary.get("Fragment-Host")) .thenReturn("host"); Mockito.when(mockBundles[skipIndex].getHeaders()) .thenReturn(mockFragmentDictionary); runtimeImpl.stopOSGI(); verifyInOrder(true); }
@Test public void testStopHandlesBundleExceptions() throws Exception { Mockito.doThrow(Mockito.mock(BundleException.class)) .when(mockBundles[skipIndex]).stop(); runtimeImpl.stopOSGI(); verifyInOrder(false); }