LocalDiscovery implements BundleListener { public void bundleChanged(BundleEvent be) { switch (be.getType()) { case BundleEvent.STARTED: findDeclaredRemoteServices(be.getBundle()); break; case BundleEvent.STOPPED: removeServicesDeclaredInBundle(be.getBundle()); break; default: } } LocalDiscovery(); @Activate void activate(BundleContext context); @Deactivate void deactivate(BundleContext context); void processExistingBundles(Bundle[] bundles); void bundleChanged(BundleEvent be);  }
@Test public void testBundleChanged() throws Exception { LocalDiscovery ld = new LocalDiscovery(); Bundle bundle = EasyMock.createMock(Bundle.class); EasyMock.expect(bundle.getSymbolicName()).andReturn("testing.bundle").anyTimes(); EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE); Dictionary<String, String> headers = new Hashtable<>(); headers.put("Remote-Service", "OSGI-INF/rsa/"); EasyMock.expect(bundle.getHeaders()).andReturn(headers); EasyMock.expect(bundle.findEntries("OSGI-INF/rsa", "*.xml", false)) .andReturn(Collections.enumeration( Collections.singleton(getClass().getResource("/ed3.xml")))); EasyMock.replay(bundle); BundleEvent be0 = new BundleEvent(BundleEvent.INSTALLED, bundle); ld.bundleChanged(be0); assertEquals(0, ld.endpointDescriptions.size()); ServiceReference<EndpointEventListener> sr = epListenerWithScope("(objectClass=*)"); EndpointEventListener endpointListener = EasyMock.createMock(EndpointEventListener.class); endpointListener.endpointChanged(EasyMock.anyObject(EndpointEvent.class), EasyMock.eq("(objectClass=*)")); EasyMock.expectLastCall(); EasyMock.replay(endpointListener); ld.bindListener(sr, endpointListener); BundleEvent be = new BundleEvent(BundleEvent.STARTED, bundle); ld.bundleChanged(be); assertEquals(1, ld.endpointDescriptions.size()); EndpointDescription endpoint = ld.endpointDescriptions.keySet().iterator().next(); assertEquals("http: assertSame(bundle, ld.endpointDescriptions.get(endpoint)); EasyMock.verify(endpointListener); EasyMock.reset(endpointListener); endpointListener.endpointChanged(EasyMock.anyObject(EndpointEvent.class), EasyMock.eq("(objectClass=*)")); EasyMock.expectLastCall(); EasyMock.replay(endpointListener); BundleEvent be1 = new BundleEvent(BundleEvent.STOPPED, bundle); ld.bundleChanged(be1); assertEquals(0, ld.endpointDescriptions.size()); EasyMock.verify(endpointListener); }