Search in sources :

Example 26 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.

the class PackageStateTest method testGetExportingBundles.

@Test
public void testGetExportingBundles() throws IOException {
    ExportedPackage exported = Mockito.mock(ExportedPackage.class);
    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    Mockito.when(exported.getExportingBundle()).thenReturn(bundle);
    Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(5));
    ExportedPackage exported2 = Mockito.mock(ExportedPackage.class);
    Bundle bundle2 = Mockito.mock(Bundle.class);
    Mockito.when(exported2.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    Mockito.when(exported2.getExportingBundle()).thenReturn(bundle2);
    Mockito.when(bundle2.getBundleId()).thenReturn(Long.valueOf(6));
    Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[] { exported, exported2 });
    long[] ids = mbean.getExportingBundles("test", "1.0.0");
    Assert.assertNotNull(ids);
    Assert.assertArrayEquals(new long[] { 5, 6 }, ids);
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 27 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.

the class PackageStateTest method testGetImportingBundles.

@Test
public void testGetImportingBundles() throws IOException {
    ExportedPackage exported = Mockito.mock(ExportedPackage.class);
    Bundle bundle = Mockito.mock(Bundle.class);
    Bundle exportingBundle = Mockito.mock(Bundle.class);
    Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    Mockito.when(exported.getExportingBundle()).thenReturn(exportingBundle);
    Mockito.when(exportingBundle.getBundleId()).thenReturn(Long.valueOf(2));
    Mockito.when(exported.getImportingBundles()).thenReturn(new Bundle[] { bundle });
    Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(4));
    Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[] { exported });
    long[] ids = mbean.getImportingBundles("test", "1.0.0", 2);
    Assert.assertArrayEquals(new long[] { 4 }, ids);
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 28 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.

the class PackageStateTest method testIsRemovalPending.

@Test
public void testIsRemovalPending() throws IOException {
    ExportedPackage exported = Mockito.mock(ExportedPackage.class);
    Bundle expBundle = Mockito.mock(Bundle.class);
    Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    Mockito.when(exported.isRemovalPending()).thenReturn(true);
    Mockito.when(exported.getExportingBundle()).thenReturn(expBundle);
    Mockito.when(expBundle.getBundleId()).thenReturn(Long.valueOf(2));
    Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[] { exported });
    boolean isRemoval = mbean.isRemovalPending("test", "1.0.0", Long.valueOf(2));
    Assert.assertTrue(isRemoval);
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) Test(org.junit.Test)

Example 29 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project aries by apache.

the class PackageStateTest method testListPackages.

@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);
}
Also used : Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) CompositeData(javax.management.openmbean.CompositeData) Collection(java.util.Collection) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test)

Example 30 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project sling by apache.

the class PackageAdminClassLoader method findBundlesForPackage.

/**
     * Find the bundle for a given package.
     * @param pckName The package name.
     * @return The bundle or <code>null</code>
     */
private Set<Bundle> findBundlesForPackage(final String pckName) {
    final ExportedPackage[] exportedPackages = this.packageAdmin.getExportedPackages(pckName);
    Set<Bundle> bundles = new LinkedHashSet<>();
    if (exportedPackages != null) {
        for (ExportedPackage exportedPackage : exportedPackages) {
            if (!exportedPackage.isRemovalPending()) {
                Bundle bundle = exportedPackage.getExportingBundle();
                if (isBundleActive(bundle)) {
                    bundles.add(bundle);
                }
            }
        }
    }
    return bundles;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle)

Aggregations

ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)54 Bundle (org.osgi.framework.Bundle)40 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)21 ArrayList (java.util.ArrayList)19 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)14 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 Version (org.osgi.framework.Version)6 LinkedHashSet (java.util.LinkedHashSet)4 TreeMap (java.util.TreeMap)4 Expectations (org.jmock.Expectations)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 File (java.io.File)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3