LocalDiscovery implements BundleListener { public void processExistingBundles(Bundle[] bundles) { if (bundles == null) { return; } for (Bundle b : bundles) { if (b.getState() == Bundle.ACTIVE) { findDeclaredRemoteServices(b); } } } LocalDiscovery(); @Activate void activate(BundleContext context); @Deactivate void deactivate(BundleContext context); void processExistingBundles(Bundle[] bundles); void bundleChanged(BundleEvent be);  }
@Test public void testPreExistingBundles() throws Exception { Bundle b1 = EasyMock.createMock(Bundle.class); EasyMock.expect(b1.getState()).andReturn(Bundle.RESOLVED); EasyMock.replay(b1); Bundle b2 = EasyMock.createMock(Bundle.class); EasyMock.expect(b2.getState()).andReturn(Bundle.ACTIVE); Dictionary<String, String> headers = new Hashtable<>(); headers.put("Remote-Service", "OSGI-INF/remote-service/"); EasyMock.expect(b2.getHeaders()).andReturn(headers); URL rs3URL = getClass().getResource("/ed3.xml"); URL rs4URL = getClass().getResource("/ed4.xml"); List<URL> urls = Arrays.asList(rs3URL, rs4URL); EasyMock.expect(b2.findEntries("OSGI-INF/remote-service", "*.xml", false)) .andReturn(Collections.enumeration(urls)); EasyMock.replay(b2); Bundle[] bundles = {b1, b2}; LocalDiscovery ld = new LocalDiscovery(); ld.processExistingBundles(bundles); assertEquals(3, ld.endpointDescriptions.size()); Set<String> expected = new HashSet<>( Arrays.asList("http: Set<String> actual = new HashSet<>(); for (Map.Entry<EndpointDescription, Bundle> entry : ld.endpointDescriptions.entrySet()) { assertSame(b2, entry.getValue()); actual.add(entry.getKey().getId()); } assertEquals(expected, actual); }