FrameworkUtils { public static boolean isBundleRequiredByOthers(Bundle bundle, PackageAdmin packageAdmin) throws IllegalArgumentException { if (bundle == null) { throw new IllegalArgumentException("Argument bundle cannot be null"); } if (packageAdmin == null) { throw new IllegalArgumentException("Argument packageAdmin cannot be null"); } boolean result = false; ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle); if (exportedPackages != null) { for (ExportedPackage exportedPackage : exportedPackages) { Bundle[] importingBundles = exportedPackage.getImportingBundles(); if (importingBundles != null && importingBundles.length > 0) { result = true; break; } } } if (!result) { RequiredBundle[] requiredBundles = packageAdmin.getRequiredBundles(bundle.getSymbolicName()); if (requiredBundles != null) { for (RequiredBundle requiredBundle : requiredBundles) { Bundle required = requiredBundle.getBundle(); if (required == bundle) { Bundle[] requiring = requiredBundle.getRequiringBundles(); if (requiring != null && requiring.length > 0) { result = true; break; } } } } } if (!result) { Bundle[] fragments = packageAdmin.getFragments(bundle); if (fragments != null && fragments.length > 0) { result = true; } } return result; } private  FrameworkUtils(); static Bundle resolveBundle(BundleContext bundleContext, long bundleId); static long[] getBundleIds(Bundle[] bundles); static long[] getBundleIds(List<Bundle> bundles); static ServiceReference resolveService(BundleContext bundleContext, long serviceId); static long[] getServiceIds(ServiceReference[] serviceReferences); static String[] getBundleExportedPackages(Bundle bundle, PackageAdmin packageAdmin); static long[] getFragmentIds(Bundle bundle, PackageAdmin packageAdmin); static long[] getHostIds(Bundle fragment, PackageAdmin packageAdmin); static String[] getBundleImportedPackages(BundleContext localBundleContext, Bundle bundle,
            PackageAdmin packageAdmin); static long[] getRegisteredServiceIds(Bundle bundle); static long[] getServicesInUseByBundle(Bundle bundle); static boolean isBundlePendingRemoval(Bundle bundle, PackageAdmin packageAdmin); static boolean isBundleRequiredByOthers(Bundle bundle, PackageAdmin packageAdmin); @SuppressWarnings("unchecked") static long[] getBundleDependencies(BundleContext localBundleContext, 
                                               Bundle bundle,
                                               PackageAdmin packageAdmin); static long[] getDependentBundles(Bundle bundle, PackageAdmin packageAdmin); static String getBundleState(Bundle bundle); static boolean arrayContains(Object[] array, Object value); static List<String> extractHeaderDeclaration(String headerStatement);  }
@Test public void testIsBundleRequiredByOthers() throws Exception { Bundle bundle = mock(Bundle.class); when(bundle.getSymbolicName()).thenReturn("org.apache.testb"); RequiredBundle reqBundle = mock(RequiredBundle.class); when(reqBundle.getBundle()).thenReturn(bundle); when(reqBundle.getRequiringBundles()).thenReturn(new Bundle[0]); PackageAdmin admin = mock(PackageAdmin.class); when(admin.getRequiredBundles("org.apache.testb")).thenReturn(new RequiredBundle[] { reqBundle }); assertFalse(isBundleRequiredByOthers(bundle, admin)); Bundle user = mock(Bundle.class); when(reqBundle.getRequiringBundles()).thenReturn(new Bundle[] { user }); assertTrue(isBundleRequiredByOthers(bundle, admin)); }