PackageState implements PackageStateMBean { public TabularData listPackages() throws IOException { Set<PackageData> packages = new HashSet<PackageData>(); for (Bundle bundle : context.getBundles()) { ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle); if (exportedPackages != null) { for (ExportedPackage exportedPackage : exportedPackages) { packages.add(new PackageData(exportedPackage)); } } } return PackageData.tableFrom(packages); } PackageState(BundleContext context, PackageAdmin packageAdmin); long[] getExportingBundles(String packageName, String version); long[] getImportingBundles(String packageName, String version, long exportingBundle); boolean isRemovalPending(String packageName, String version, long exportingBundle); TabularData listPackages();  }
@Test public void testListPackages() throws IOException { Bundle bundle = Mockito.mock(Bundle.class); Bundle impBundle = Mockito.mock(Bundle.class); Mockito.when(context.getBundles()).thenReturn(new Bundle[]{bundle}); ExportedPackage exported = Mockito.mock(ExportedPackage.class); Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0")); Mockito.when(exported.getImportingBundles()).thenReturn(new Bundle[]{impBundle}); Mockito.when(exported.getName()).thenReturn("test"); Mockito.when(exported.getExportingBundle()).thenReturn(bundle); Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(4)); Mockito.when(impBundle.getBundleId()).thenReturn(Long.valueOf(5)); Mockito.when(admin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[]{exported}); TabularData table = mbean.listPackages(); Assert.assertEquals(PackageStateMBean.PACKAGES_TYPE,table.getTabularType()); Collection values = table.values(); Assert.assertEquals(1, values.size()); CompositeData data = (CompositeData) values.iterator().next(); Long[] exportingBundles = (Long[])data.get(PackageStateMBean.EXPORTING_BUNDLES); Assert.assertArrayEquals(new Long[]{Long.valueOf(4)}, exportingBundles); String name = (String) data.get(PackageStateMBean.NAME); Assert.assertEquals("test", name); String version = (String) data.get(PackageStateMBean.VERSION); Assert.assertEquals("1.0.0", version); }