Search in sources :

Example 1 with BatchInstallResult

use of org.apache.aries.jmx.codec.BatchInstallResult in project aries by apache.

the class FrameworkTest method testInstallBundles.

@Test
public void testInstallBundles() throws Exception {
    String[] locations = new String[] { "file:test.jar" };
    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
    Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
    CompositeData data = mbean.installBundles(locations);
    BatchInstallResult batch = BatchInstallResult.from(data);
    Assert.assertNotNull(batch);
    Assert.assertEquals(2, batch.getCompleted()[0]);
    Assert.assertTrue(batch.isSuccess());
    Assert.assertNull(batch.getError());
    Assert.assertNull(batch.getRemainingLocationItems());
    Mockito.reset(context);
    Mockito.when(context.installBundle("file:test.jar")).thenThrow(new BundleException("location doesn't exist"));
    CompositeData data2 = mbean.installBundles(locations);
    BatchInstallResult batch2 = BatchInstallResult.from(data2);
    Assert.assertNotNull(batch2);
    Assert.assertNotNull(batch2.getCompleted());
    Assert.assertEquals(0, batch2.getCompleted().length);
    Assert.assertFalse(batch2.isSuccess());
    Assert.assertNotNull(batch2.getError());
    Assert.assertEquals("file:test.jar", batch2.getBundleInError());
    Assert.assertNotNull(batch2.getRemainingLocationItems());
    Assert.assertEquals(0, batch2.getRemainingLocationItems().length);
}
Also used : BatchInstallResult(org.apache.aries.jmx.codec.BatchInstallResult) Bundle(org.osgi.framework.Bundle) CompositeData(javax.management.openmbean.CompositeData) BundleException(org.osgi.framework.BundleException) Test(org.junit.Test)

Example 2 with BatchInstallResult

use of org.apache.aries.jmx.codec.BatchInstallResult in project aries by apache.

the class Framework method installBundlesFromURL.

/**
     * @see org.osgi.jmx.framework.FrameworkMBean#installBundlesFromURL(String[], String[])
     */
public CompositeData installBundlesFromURL(String[] locations, String[] urls) throws IOException {
    if (locations == null || urls == null) {
        return new BatchInstallResult("Failed to install bundles arguments can't be null").toCompositeData();
    }
    if (locations.length != urls.length) {
        return new BatchInstallResult("Failed to install bundles size of arguments should be same").toCompositeData();
    }
    long[] ids = new long[locations.length];
    for (int i = 0; i < locations.length; i++) {
        try {
            long id = installBundleFromURL(locations[i], urls[i]);
            ids[i] = id;
        } catch (Throwable t) {
            long[] completed = new long[i];
            System.arraycopy(ids, 0, completed, 0, i);
            String[] remaining = new String[locations.length - i - 1];
            System.arraycopy(locations, i + 1, remaining, 0, remaining.length);
            return new BatchInstallResult(completed, t.toString(), remaining, locations[i]).toCompositeData();
        }
    }
    return new BatchInstallResult(ids).toCompositeData();
}
Also used : BatchInstallResult(org.apache.aries.jmx.codec.BatchInstallResult)

Example 3 with BatchInstallResult

use of org.apache.aries.jmx.codec.BatchInstallResult in project aries by apache.

the class Framework method installBundles.

/**
     * @see org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
     */
public CompositeData installBundles(String[] locations) throws IOException {
    if (locations == null) {
        return new BatchInstallResult("Failed to install bundles locations can't be null").toCompositeData();
    }
    long[] ids = new long[locations.length];
    for (int i = 0; i < locations.length; i++) {
        try {
            long id = installBundle(locations[i]);
            ids[i] = id;
        } catch (Throwable t) {
            long[] completed = new long[i];
            System.arraycopy(ids, 0, completed, 0, i);
            String[] remaining = new String[locations.length - i - 1];
            System.arraycopy(locations, i + 1, remaining, 0, remaining.length);
            return new BatchInstallResult(completed, t.toString(), remaining, locations[i]).toCompositeData();
        }
    }
    return new BatchInstallResult(ids).toCompositeData();
}
Also used : BatchInstallResult(org.apache.aries.jmx.codec.BatchInstallResult)

Example 4 with BatchInstallResult

use of org.apache.aries.jmx.codec.BatchInstallResult in project aries by apache.

the class FrameworkTest method testInstallBundlesFromURL.

@Test
public void testInstallBundlesFromURL() throws Exception {
    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
    Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
    Framework spiedMBean = Mockito.spy(mbean);
    InputStream stream = Mockito.mock(InputStream.class);
    Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
    CompositeData data = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
    Assert.assertNotNull(data);
    BatchInstallResult batch = BatchInstallResult.from(data);
    Assert.assertEquals(2, batch.getCompleted()[0]);
    Assert.assertTrue(batch.isSuccess());
    Assert.assertNull(batch.getError());
    Assert.assertNull(batch.getRemainingLocationItems());
    Mockito.reset(context);
    Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
    Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenThrow(new BundleException("location doesn't exist"));
    CompositeData data2 = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
    BatchInstallResult batch2 = BatchInstallResult.from(data2);
    Assert.assertNotNull(batch2);
    Assert.assertNotNull(batch2.getCompleted());
    Assert.assertEquals(0, batch2.getCompleted().length);
    Assert.assertFalse(batch2.isSuccess());
    Assert.assertNotNull(batch2.getError());
    Assert.assertEquals("file:test.jar", batch2.getBundleInError());
    Assert.assertNotNull(batch2.getRemainingLocationItems());
    Assert.assertEquals(0, batch2.getRemainingLocationItems().length);
}
Also used : BatchInstallResult(org.apache.aries.jmx.codec.BatchInstallResult) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) CompositeData(javax.management.openmbean.CompositeData) BundleException(org.osgi.framework.BundleException) Test(org.junit.Test)

Aggregations

BatchInstallResult (org.apache.aries.jmx.codec.BatchInstallResult)4 CompositeData (javax.management.openmbean.CompositeData)2 Test (org.junit.Test)2 Bundle (org.osgi.framework.Bundle)2 BundleException (org.osgi.framework.BundleException)2 InputStream (java.io.InputStream)1