BlueprintMetadata implements BlueprintMetadataMBean { public long getBlueprintContainerServiceId(long bundleId) throws IOException { Bundle bpBundle = bundleContext.getBundle(bundleId); if (null == bpBundle) throw new IllegalArgumentException("Invalid bundle id " + bundleId); String filter = "(&(osgi.blueprint.container.symbolicname=" + bpBundle.getSymbolicName() + ")(osgi.blueprint.container.version=" + bpBundle.getVersion() + "))"; ServiceReference[] serviceReferences = null; try { serviceReferences = bundleContext.getServiceReferences(BlueprintContainer.class.getName(), filter); } catch (InvalidSyntaxException e) { throw new RuntimeException(e); } if (serviceReferences == null || serviceReferences.length < 1) return -1; else return (Long) serviceReferences[0].getProperty(Constants.SERVICE_ID); } BlueprintMetadata(BundleContext bundleContext); long getBlueprintContainerServiceId(long bundleId); long[] getBlueprintContainerServiceIds(); String[] getComponentIds(long containerServiceId); String[] getComponentIdsByType(long containerServiceId, String type); CompositeData getComponentMetadata(long containerServiceId, String componentId);  }
@Test public void validGetBlueprintContainerServiceId() throws Exception { final long bundleId = 12; final long serviceId = 7117; mockery.checking(new Expectations() { { allowing(mockContext).getBundle(bundleId); will(returnValue(mockBundle)); oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class))); will(returnValue(mockServiceReferences)); } }); mockery.checking(new Expectations() { { allowing(mockBundle).getSymbolicName(); will(returnValue("org.apache.geronimo.blueprint.testXXX")); allowing(mockBundle).getVersion(); will(returnValue(Version.emptyVersion)); } }); mockery.checking(new Expectations() { { allowing(mockServiceReferences[0]).getProperty(Constants.SERVICE_ID); will(returnValue(serviceId)); } }); assertEquals(serviceId, metadata.getBlueprintContainerServiceId(bundleId)); }
@Test public void invalidParaInGetBlueprintContainerServiceId() throws Exception { mockery.checking(new Expectations() { { allowing(mockContext).getBundle(with(any(Long.class))); will(returnValue(null)); } }); try{ metadata.getBlueprintContainerServiceId(-10); } catch(Exception ex) { assertTrue(ex instanceof IllegalArgumentException); } }
@Test public void cannotFindAssociatedContainerServiceId() throws Exception { final long bundleId = 12; mockery.checking(new Expectations() { { allowing(mockContext).getBundle(bundleId); will(returnValue(mockBundle)); oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class))); will(returnValue(null)); } }); mockery.checking(new Expectations() { { allowing(mockBundle).getSymbolicName(); will(returnValue("org.apache.geronimo.blueprint.testXXX")); allowing(mockBundle).getVersion(); will(returnValue(Version.emptyVersion)); } }); assertEquals(-1, metadata.getBlueprintContainerServiceId(bundleId)); }