public void stop(int options) throws BundleException { boolean wasActive = false; switch (getState()) { case ACTIVE: wasActive = true; case STARTING: setState(STOPPING); Throwable exception = null; if (wasActive && (bundleActivator != null)) { try { bundleActivator.stop(getBundleContext()); } catch (Throwable t) { if (t instanceof ThreadDeath) throw (ThreadDeath) t; else exception = t; } this.bundleActivator = null; } if (getState() == UNINSTALLED) throw new BundleException("Bundle.UNINSTALLED"); setState(RESOLVED); getFramework().fireBundleEvent(BundleEvent.STOPPED, this); if (exception != null) throw new BundleException("BundleActivator.stop", exception); break; case UNINSTALLED: throw new IllegalStateException("Bundle.UNINSTALLED"); default: break; } }
@Test public void startAndStop() throws Exception { BundleContext bundleContext = new MockBundleContext() { @Override public ServiceRegistration registerService( String clazz, Object service, Dictionary properties) { if (service instanceof ContactDao) { daoRegistered++; return new MockServiceRegistration() { @Override public void unregister() { daoRegistered--; }; }; } else { return super.registerService(clazz, service, properties); } } }; BundleActivator bundleActivator = new DirectoryDaoBundleActivatorSimple(); bundleActivator.start(bundleContext); Assert.assertEquals(1, daoRegistered); bundleActivator.stop(bundleContext); Assert.assertEquals(0, daoRegistered); }