You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by aw...@apache.org on 2009/11/30 23:41:08 UTC

svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Author: awojtuniak
Date: Mon Nov 30 22:41:08 2009
New Revision: 885603

URL: http://svn.apache.org/viewvc?rev=885603&view=rev
Log:
ARIES-30 Framework MBean test case

Added:
    incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java   (with props)
Modified:
    incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java

Modified: incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
==============================================================================
--- incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java (original)
+++ incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java Mon Nov 30 22:41:08 2009
@@ -88,16 +88,27 @@
      * @see org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String, java.lang.String)
      */
     public long installBundle(String location, String url) throws IOException {
-        InputStream inputStream;
+        InputStream inputStream = null;
         try {
-            inputStream = new URL(url).openStream();
+            inputStream = createStream(url);
             Bundle bundle = context.installBundle(location, inputStream);
             return bundle.getBundleId();
         } catch (BundleException e) {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException ioe) {
+
+                }
+            }
             throw new IOException("Can't install bundle with location" + location);
         }
     }
 
+    public InputStream createStream(String url) throws IOException {
+        return new URL(url).openStream();
+    }
+
     /**
      * @see org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
      */
@@ -170,12 +181,7 @@
             try {
                 refreshPackages(bundleIdentifiers[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -241,12 +247,7 @@
             try {
                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -304,12 +305,7 @@
             try {
                 startBundle(bundleIdentifiers[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -340,12 +336,7 @@
             try {
                 stopBundle(bundleIdentifiers[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -355,7 +346,7 @@
      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
      */
     public void uninstallBundle(long bundleIdentifier) throws IOException {
-        Bundle bundle = context.getBundle(bundleIdentifier);
+        Bundle bundle = getBundle(bundleIdentifier);
         if (bundle != null) {
             try {
                 bundle.uninstall();
@@ -364,8 +355,6 @@
             }
         }
 
-        throw new IllegalArgumentException("Can't find bundle with id " + bundleIdentifier);
-
     }
 
     /**
@@ -379,12 +368,7 @@
             try {
                 uninstallBundle(bundleIdentifiers[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -408,10 +392,10 @@
      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long, java.lang.String)
      */
     public void updateBundle(long bundleIdentifier, String url) throws IOException {
-        Bundle bundle = getBundle(0);
+        Bundle bundle = getBundle(bundleIdentifier);
         InputStream inputStream = null;
         try {
-            inputStream = new URL(url).openStream();
+            inputStream = createStream(url);
             bundle.update(inputStream);
         } catch (BundleException be) {
             if (inputStream != null) {
@@ -437,12 +421,7 @@
             try {
                 updateBundle(bundleIdentifiers[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -463,12 +442,7 @@
             try {
                 updateBundle(bundleIdentifiers[i], urls[i]);
             } catch (Throwable t) {
-                long[] completed = new long[i];
-                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
-                long[] remaining = new long[completed.length - i - 1];
-                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
-                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
-                        .toCompositeData();
+                return createFailedBatchActionResult(bundleIdentifiers, i, t);
             }
         }
         return new BatchActionResult(bundleIdentifiers).toCompositeData();
@@ -488,6 +462,22 @@
     }
 
     /**
+     * Create {@link BatchActionResult}, when the operation fail.
+     * 
+     * @param bundleIdentifiers bundle ids for operation.
+     * @param i index of loop pointing on which operation fails.
+     * @param t Throwable thrown by failed operation.
+     * @return created BatchActionResult instance.
+     */
+    private CompositeData createFailedBatchActionResult(long[] bundleIdentifiers, int i, Throwable t) {
+        long[] completed = new long[i];
+        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
+        long[] remaining = new long[bundleIdentifiers.length - i - 1];
+        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
+        return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i]).toCompositeData();
+    }
+    
+    /**
      * Gets bundle with provided bundleId.
      * 
      * @param bundleIdentifier bundle id.

Added: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
==============================================================================
--- incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java (added)
+++ incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java Mon Nov 30 22:41:08 2009
@@ -0,0 +1,510 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.aries.jmx.framework;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.management.openmbean.CompositeData;
+
+import org.junit.Assert;
+
+import org.apache.aries.jmx.codec.BatchActionResult;
+import org.apache.aries.jmx.codec.BatchInstallResult;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.jmx.framework.FrameworkMBean;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.startlevel.StartLevel;
+
+/**
+ * {@link FrameworkMBean} test case.
+ * 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class FrameworkTest {
+
+    @Mock
+    private StartLevel startLevel;
+    @Mock
+    private PackageAdmin admin;
+    @Mock
+    private BundleContext context;
+    private Framework mbean;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        mbean = new Framework(context, startLevel, admin);
+    }
+
+    @Test
+    public void testGetFrameworkStartLevel() throws IOException {
+        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
+        int level = mbean.getFrameworkStartLevel();
+        Assert.assertEquals(1, level);
+    }
+
+    @Test
+    public void testGetInitialBundleStartLevel() throws IOException {
+        Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
+        int level = mbean.getInitialBundleStartLevel();
+        Mockito.verify(startLevel).getInitialBundleStartLevel();
+        Assert.assertEquals(2, level);
+    }
+
+    @Test
+    public void testInstallBundleString() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
+        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
+        long bundleId = mbean.installBundle("file:test.jar");
+        Assert.assertEquals(2, bundleId);
+        Mockito.reset(context);
+        Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new BundleException("location doesn't exist"));
+
+        try {
+            mbean.installBundle("file:test2.jar");
+            Assert.fail("Shouldn't go to this stage, location doesn't exist");
+        } catch (IOException e) {
+            // ok
+        }
+
+    }
+
+    @Test
+    public void testInstallBundleStringString() throws Exception {
+        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
+        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
+        Framework spiedMBean = Mockito.spy(partialMock);
+        InputStream stream = Mockito.mock(InputStream.class);
+        Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
+        long bundleId = spiedMBean.installBundle("file:test.jar", "test.jar");
+        Assert.assertEquals(2, bundleId);
+        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"));
+
+        try {
+            spiedMBean.installBundle("file:test2.jar", "test.jar");
+            Assert.fail("Shouldn't go to this stage, location doesn't exist");
+        } catch (IOException e) {
+            // ok
+        }
+    }
+
+    @Test
+    public void testInstallBundlesStringArray() 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(new Long(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"));
+
+        /*
+         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
+         * mbean.installBundles(locations); BatchInstallResult batch2 = BatchInstallResult.from(data2);
+         * Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
+         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
+         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
+         * Assert.assertNull(batch2.getRemainingLocationItems());
+         */
+
+    }
+
+    @Test
+    public void testInstallBundlesStringArrayStringArray() throws Exception {
+        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
+        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
+        Framework spiedMBean = Mockito.spy(partialMock);
+        InputStream stream = Mockito.mock(InputStream.class);
+        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
+        CompositeData data = spiedMBean.installBundles(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"));
+
+        /*
+         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
+         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new String[]{"test.jar"}); BatchInstallResult batch2
+         * = BatchInstallResult.from(data2); Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
+         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
+         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
+         * Assert.assertNull(batch2.getRemainingLocationItems());
+         */
+
+    }
+
+    @Test
+    public void testRefreshPackagesLong() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+        mbean.refreshPackages(1);
+        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
+
+        try {
+            mbean.refreshPackages(2);
+            Assert.fail("Shouldn't happen illegal argument");
+        } catch (IllegalArgumentException iae) {
+            // expected
+        }
+    }
+
+    @Test
+    public void testRefreshPackagesLongArray() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+        mbean.refreshPackages(new long[] { 1 });
+        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
+
+        CompositeData data = mbean.refreshPackages(new long[] { 2 });
+        // shouldn't found bundle whith id 2
+        Assert.assertNotNull(data);
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(0, batch.getCompleted().length);
+        Assert.assertFalse(batch.isSuccess());
+        Assert.assertNotNull(batch.getError());
+        Assert.assertNotNull(batch.getRemainingItems());
+        Assert.assertEquals(2, batch.getBundleInError());
+
+    }
+
+    @Test
+    public void testResolveBundle() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+        mbean.resolveBundle(1);
+        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
+    }
+
+    @Test
+    public void testResolveBundles() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+        mbean.resolveBundles(new long[] { 1 });
+        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
+    }
+
+    @Test
+    public void testRestartFramework() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(0)).thenReturn(bundle);
+        mbean.restartFramework();
+        Mockito.verify(bundle).update();
+    }
+
+    @Test
+    public void testSetBundleStartLevel() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(2)).thenReturn(bundle);
+        mbean.setBundleStartLevel(2, 1);
+        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
+    }
+
+    @Test
+    public void testSetBundleStartLevels() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(2)).thenReturn(bundle);
+        CompositeData data = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2 });
+        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(2, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2, 4 });
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertNull(batch2.getCompleted());
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+
+    }
+
+    @Test
+    public void testSetFrameworkStartLevel() throws IOException {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(0)).thenReturn(bundle);
+        mbean.setFrameworkStartLevel(1);
+        Mockito.verify(startLevel).setStartLevel(1);
+
+    }
+
+    @Test
+    public void testSetInitialBundleStartLevel() throws IOException {
+        mbean.setInitialBundleStartLevel(5);
+        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
+    }
+
+    @Test
+    public void testShutdownFramework() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(0)).thenReturn(bundle);
+        mbean.shutdownFramework();
+        Mockito.verify(bundle).stop();
+    }
+
+    @Test
+    public void testStartBundle() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        mbean.startBundle(5);
+        Mockito.verify(bundle).start();
+        
+        Mockito.reset(context);
+        Mockito.when(context.getBundle(6)).thenReturn(bundle);
+        Mockito.doThrow(new BundleException("")).when(bundle).start();
+        
+        try {
+            mbean.startBundle(6);
+            Assert.fail("Shouldn't go to this stage, BundleException was thrown");
+        } catch (IOException ioe) {
+            //expected
+        }
+    }
+
+    @Test
+    public void testStartBundles() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        CompositeData data = mbean.startBundles(new long[] { 5 });
+        Mockito.verify(bundle).start();
+
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(5, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = mbean.startBundles(null);
+
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertNull(batch2.getCompleted());
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+    }
+
+    @Test
+    public void testStopBundle() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        mbean.stopBundle(5);
+        Mockito.verify(bundle).stop();
+    }
+
+    @Test
+    public void testStopBundles() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        CompositeData data = mbean.stopBundles(new long[] { 5 });
+        Mockito.verify(bundle).stop();
+
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(5, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = mbean.stopBundles(null);
+
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertNull(batch2.getCompleted());
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+    }
+
+    @Test
+    public void testUninstallBundle() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        mbean.uninstallBundle(5);
+        Mockito.verify(bundle).uninstall();
+    }
+
+    @Test
+    public void testUninstallBundles() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
+        Mockito.verify(bundle).uninstall();
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(5, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = mbean.uninstallBundles(null);
+
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertNull(batch2.getCompleted());
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+    }
+
+    @Test
+    public void testUpdateBundleLong() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        mbean.updateBundle(5);
+        Mockito.verify(bundle).update();
+    }
+
+    @Test
+    public void testUpdateBundleLongString() throws Exception {
+        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
+        Framework spiedMBean = Mockito.spy(partialMock);
+        InputStream stream = Mockito.mock(InputStream.class);
+        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        spiedMBean.updateBundle(5, "file:test.jar");
+        Mockito.verify(bundle).update(stream);
+    }
+
+    @Test
+    public void testUpdateBundlesLongArray() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        CompositeData data = mbean.updateBundles(new long[] { 5 });
+        Mockito.verify(bundle).update();
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(5, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = mbean.updateBundles(null);
+
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertNull(batch2.getCompleted());
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+
+        Mockito.reset(bundle);
+        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
+        Mockito.when(context.getBundle(6)).thenReturn(bundle);
+        Mockito.doThrow(new BundleException("")).when(bundle).update();
+        BatchActionResult batch3 = BatchActionResult.from(data3);
+        Assert.assertEquals(0, batch3.getCompleted().length);
+        Assert.assertFalse(batch3.isSuccess());
+        Assert.assertNotNull(batch3.getError());
+        Assert.assertEquals(6, batch3.getBundleInError());
+
+        Bundle bundle6 = Mockito.mock(Bundle.class);
+        Bundle bundle8 = Mockito.mock(Bundle.class);
+        Bundle bundle7 = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
+        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
+        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
+        Mockito.doThrow(new BundleException("")).when(bundle8).update();
+        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7 });
+        BatchActionResult batch4 = BatchActionResult.from(data4);
+        Mockito.verify(bundle6).update();
+        Assert.assertEquals(1, batch4.getCompleted().length);
+        // should contain only bundleid 6
+        Assert.assertEquals(6, batch4.getCompleted()[0]);
+        Assert.assertFalse(batch4.isSuccess());
+        Assert.assertNotNull(batch4.getError());
+        Assert.assertEquals(8, batch4.getBundleInError());
+        Assert.assertEquals(1, batch4.getRemainingItems().length);
+        // should contain only bundleid 7
+        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
+    }
+
+    @Test
+    public void testUpdateBundlesLongArrayStringArray() throws Exception {
+        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
+        Framework spiedMBean = Mockito.spy(partialMock);
+        InputStream stream = Mockito.mock(InputStream.class);
+        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(5)).thenReturn(bundle);
+        CompositeData data = spiedMBean.updateBundles(new long[] { 5 }, new String[] { "file:test.jar" });
+        Mockito.verify(bundle).update(stream);
+        BatchActionResult batch = BatchActionResult.from(data);
+        Assert.assertEquals(5, batch.getCompleted()[0]);
+        Assert.assertTrue(batch.isSuccess());
+        Assert.assertNull(batch.getError());
+        Assert.assertNull(batch.getRemainingItems());
+
+        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4 }, new String[] { "file:test.jar" });
+        BatchActionResult batch2 = BatchActionResult.from(data2);
+        Assert.assertFalse(batch2.isSuccess());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNotNull(batch2.getError());
+        Assert.assertNull(batch2.getRemainingItems());
+    }
+
+    @Test
+    public void testUpdateFramework() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(context.getBundle(0)).thenReturn(bundle);
+        mbean.restartFramework();
+        Mockito.verify(bundle).update();
+    }
+
+    /**
+     * Mocking {@link Framework} method createStream.
+     */
+    private class FrameworkPartialMock extends Framework {
+
+        public FrameworkPartialMock(BundleContext context, StartLevel startLevel, PackageAdmin packageAdmin) {
+            super(context, startLevel, packageAdmin);
+        }
+
+        public InputStream createStream(String url) throws IOException {
+            return null;
+        }
+    }
+
+}

Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
------------------------------------------------------------------------------
    svn:keywords = Revision Date



Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by Alasdair Nottingham <al...@gmail.com>.
I'm really confused now, but the problem I thought was there isn't
there now, so either I misread the code, or it has been fixed.

I wasn't asking about tests, but the MBean implementation.

Problem closed.
Alasdair

2009/12/3 adam wojtuniak <ad...@gmail.com>:
> Its not because test are written this is because implementation of MBean is
> wirtten this way.
> It has to be this way. Implementation needs to use
> installBundle(location,stream). In the implemetation
> we have to create stream from url and put the stream to
> installBundle(..,..). In test I don't want to use real stream so I mocked
> it.
> I don't see anything odd in that. Later on I will provide integarationtest
> for JMX as well.
>
> Cheers,
> Adam
>
> On Thu, Dec 3, 2009 at 2:06 PM, Alasdair Nottingham <
> alasdair.nottingham@gmail.com> wrote:
>
>> It still seems strange to me that it has to be done the way it is done
>> because the tests are written this way.
>>
>> It just seems odd.
>>
>> Alasdair
>>
>> 2009/12/1 Lin Sun <li...@gmail.com>:
>>  > Hi,
>> >
>> > I wonder if the inputStream is created from a file directory, equinox
>> > will actually ignore the inputstream and use the content of the
>> > directory.   This was what I observed when I last time used this
>> > method(context.installBundle(string, inputStream)).
>> >
>> > It would be great if we could have some itest on this. :)
>> >
>> > Lin
>> >
>> >
>> > On Tue, Dec 1, 2009 at 11:18 AM, Alasdair Nottingham
>> > <al...@gmail.com> wrote:
>> >> Hi,
>> >>
>> >> Looking at this change the installBundle method does this:
>> >>
>> >>            inputStream = createStream(url);
>> >>            Bundle bundle = context.installBundle(location, inputStream);
>> >>
>> >> when I think it could just do this:
>> >>
>> >>            Bundle bundle = context.installBundle(location);
>> >>
>> >> By doing it the following way you could install a directory as a
>> >> bundle if the framework is equinox.
>> >>
>> >> What do you think?
>> >> Alasdair
>> >>
>> >> 2009/11/30  <aw...@apache.org>:
>> >>> Author: awojtuniak
>> >>> Date: Mon Nov 30 22:41:08 2009
>> >>> New Revision: 885603
>> >>>
>> >>> URL: http://svn.apache.org/viewvc?rev=885603&view=rev
>> >>> Log:
>> >>> ARIES-30 Framework MBean test case
>> >>>
>> >>> Added:
>> >>>
>>  incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>>   (with props)
>> >>> Modified:
>> >>>
>>  incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> >>>
>> >>> Modified:
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> >>> URL:
>> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
>> >>>
>> ==============================================================================
>> >>> ---
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> (original)
>> >>> +++
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> Mon Nov 30 22:41:08 2009
>> >>> @@ -88,16 +88,27 @@
>> >>>      * @see
>> org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String,
>> java.lang.String)
>> >>>      */
>> >>>     public long installBundle(String location, String url) throws
>> IOException {
>> >>> -        InputStream inputStream;
>> >>> +        InputStream inputStream = null;
>> >>>         try {
>> >>> -            inputStream = new URL(url).openStream();
>> >>> +            inputStream = createStream(url);
>> >>>             Bundle bundle = context.installBundle(location,
>> inputStream);
>> >>>             return bundle.getBundleId();
>> >>>         } catch (BundleException e) {
>> >>> +            if (inputStream != null) {
>> >>> +                try {
>> >>> +                    inputStream.close();
>> >>> +                } catch (IOException ioe) {
>> >>> +
>> >>> +                }
>> >>> +            }
>> >>>             throw new IOException("Can't install bundle with location"
>> + location);
>> >>>         }
>> >>>     }
>> >>>
>> >>> +    public InputStream createStream(String url) throws IOException {
>> >>> +        return new URL(url).openStream();
>> >>> +    }
>> >>> +
>> >>>     /**
>> >>>      * @see
>> org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
>> >>>      */
>> >>> @@ -170,12 +181,7 @@
>> >>>             try {
>> >>>                 refreshPackages(bundleIdentifiers[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -241,12 +247,7 @@
>> >>>             try {
>> >>>                 setBundleStartLevel(bundleIdentifiers[i],
>> newlevels[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -304,12 +305,7 @@
>> >>>             try {
>> >>>                 startBundle(bundleIdentifiers[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -340,12 +336,7 @@
>> >>>             try {
>> >>>                 stopBundle(bundleIdentifiers[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -355,7 +346,7 @@
>> >>>      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
>> >>>      */
>> >>>     public void uninstallBundle(long bundleIdentifier) throws
>> IOException {
>> >>> -        Bundle bundle = context.getBundle(bundleIdentifier);
>> >>> +        Bundle bundle = getBundle(bundleIdentifier);
>> >>>         if (bundle != null) {
>> >>>             try {
>> >>>                 bundle.uninstall();
>> >>> @@ -364,8 +355,6 @@
>> >>>             }
>> >>>         }
>> >>>
>> >>> -        throw new IllegalArgumentException("Can't find bundle with id
>> " + bundleIdentifier);
>> >>> -
>> >>>     }
>> >>>
>> >>>     /**
>> >>> @@ -379,12 +368,7 @@
>> >>>             try {
>> >>>                 uninstallBundle(bundleIdentifiers[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -408,10 +392,10 @@
>> >>>      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long,
>> java.lang.String)
>> >>>      */
>> >>>     public void updateBundle(long bundleIdentifier, String url) throws
>> IOException {
>> >>> -        Bundle bundle = getBundle(0);
>> >>> +        Bundle bundle = getBundle(bundleIdentifier);
>> >>>         InputStream inputStream = null;
>> >>>         try {
>> >>> -            inputStream = new URL(url).openStream();
>> >>> +            inputStream = createStream(url);
>> >>>             bundle.update(inputStream);
>> >>>         } catch (BundleException be) {
>> >>>             if (inputStream != null) {
>> >>> @@ -437,12 +421,7 @@
>> >>>             try {
>> >>>                 updateBundle(bundleIdentifiers[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -463,12 +442,7 @@
>> >>>             try {
>> >>>                 updateBundle(bundleIdentifiers[i], urls[i]);
>> >>>             } catch (Throwable t) {
>> >>> -                long[] completed = new long[i];
>> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
>> i);
>> >>> -                long[] remaining = new long[completed.length - i - 1];
>> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
>> 0, remaining.length);
>> >>> -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> >>> -                        .toCompositeData();
>> >>> +                return
>> createFailedBatchActionResult(bundleIdentifiers, i, t);
>> >>>             }
>> >>>         }
>> >>>         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> >>> @@ -488,6 +462,22 @@
>> >>>     }
>> >>>
>> >>>     /**
>> >>> +     * Create {@link BatchActionResult}, when the operation fail.
>> >>> +     *
>> >>> +     * @param bundleIdentifiers bundle ids for operation.
>> >>> +     * @param i index of loop pointing on which operation fails.
>> >>> +     * @param t Throwable thrown by failed operation.
>> >>> +     * @return created BatchActionResult instance.
>> >>> +     */
>> >>> +    private CompositeData createFailedBatchActionResult(long[]
>> bundleIdentifiers, int i, Throwable t) {
>> >>> +        long[] completed = new long[i];
>> >>> +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> >>> +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
>> >>> +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> >>> +        return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i]).toCompositeData();
>> >>> +    }
>> >>> +
>> >>> +    /**
>> >>>      * Gets bundle with provided bundleId.
>> >>>      *
>> >>>      * @param bundleIdentifier bundle id.
>> >>>
>> >>> Added:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> >>> URL:
>> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
>> >>>
>> ==============================================================================
>> >>> ---
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> (added)
>> >>> +++
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> Mon Nov 30 22:41:08 2009
>> >>> @@ -0,0 +1,510 @@
>> >>> +/**
>> >>> + *  Licensed to the Apache Software Foundation (ASF) under one or more
>> >>> + *  contributor license agreements.  See the NOTICE file distributed
>> with
>> >>> + *  this work for additional information regarding copyright
>> ownership.
>> >>> + *  The ASF licenses this file to You under the Apache License,
>> Version 2.0
>> >>> + *  (the "License"); you may not use this file except in compliance
>> with
>> >>> + *  the License.  You may obtain a copy of the License at
>> >>> + *
>> >>> + *     http://www.apache.org/licenses/LICENSE-2.0
>> >>> + *
>> >>> + *  Unless required by applicable law or agreed to in writing,
>> software
>> >>> + *  distributed under the License is distributed on an "AS IS" BASIS,
>> >>> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> >>> + *  See the License for the specific language governing permissions
>> and
>> >>> + *  limitations under the License.
>> >>> + */
>> >>> +package org.apache.aries.jmx.framework;
>> >>> +
>> >>> +import java.io.IOException;
>> >>> +import java.io.InputStream;
>> >>> +
>> >>> +import javax.management.openmbean.CompositeData;
>> >>> +
>> >>> +import org.junit.Assert;
>> >>> +
>> >>> +import org.apache.aries.jmx.codec.BatchActionResult;
>> >>> +import org.apache.aries.jmx.codec.BatchInstallResult;
>> >>> +import org.junit.Before;
>> >>> +import org.junit.Test;
>> >>> +import org.mockito.Mock;
>> >>> +import org.mockito.Mockito;
>> >>> +import org.mockito.MockitoAnnotations;
>> >>> +import org.osgi.framework.Bundle;
>> >>> +import org.osgi.framework.BundleContext;
>> >>> +import org.osgi.framework.BundleException;
>> >>> +import org.osgi.jmx.framework.FrameworkMBean;
>> >>> +import org.osgi.service.packageadmin.PackageAdmin;
>> >>> +import org.osgi.service.startlevel.StartLevel;
>> >>> +
>> >>> +/**
>> >>> + * {@link FrameworkMBean} test case.
>> >>> + *
>> >>> + *
>> >>> + * @version $Rev$ $Date$
>> >>> + */
>> >>> +public class FrameworkTest {
>> >>> +
>> >>> +    @Mock
>> >>> +    private StartLevel startLevel;
>> >>> +    @Mock
>> >>> +    private PackageAdmin admin;
>> >>> +    @Mock
>> >>> +    private BundleContext context;
>> >>> +    private Framework mbean;
>> >>> +
>> >>> +    @Before
>> >>> +    public void setUp() throws Exception {
>> >>> +        MockitoAnnotations.initMocks(this);
>> >>> +        mbean = new Framework(context, startLevel, admin);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testGetFrameworkStartLevel() throws IOException {
>> >>> +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
>> >>> +        int level = mbean.getFrameworkStartLevel();
>> >>> +        Assert.assertEquals(1, level);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testGetInitialBundleStartLevel() throws IOException {
>> >>> +
>>  Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
>> >>> +        int level = mbean.getInitialBundleStartLevel();
>> >>> +        Mockito.verify(startLevel).getInitialBundleStartLevel();
>> >>> +        Assert.assertEquals(2, level);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testInstallBundleString() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +
>>  Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
>> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> >>> +        long bundleId = mbean.installBundle("file:test.jar");
>> >>> +        Assert.assertEquals(2, bundleId);
>> >>> +        Mockito.reset(context);
>> >>> +
>>  Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new
>> BundleException("location doesn't exist"));
>> >>> +
>> >>> +        try {
>> >>> +            mbean.installBundle("file:test2.jar");
>> >>> +            Assert.fail("Shouldn't go to this stage, location doesn't
>> exist");
>> >>> +        } catch (IOException e) {
>> >>> +            // ok
>> >>> +        }
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testInstallBundleStringString() throws Exception {
>> >>> +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.installBundle(Mockito.anyString(),
>> Mockito.any(InputStream.class))).thenReturn(bundle);
>> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> >>> +        InputStream stream = Mockito.mock(InputStream.class);
>> >>> +
>>  Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
>> >>> +        long bundleId = spiedMBean.installBundle("file:test.jar",
>> "test.jar");
>> >>> +        Assert.assertEquals(2, bundleId);
>> >>> +        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"));
>> >>> +
>> >>> +        try {
>> >>> +            spiedMBean.installBundle("file:test2.jar", "test.jar");
>> >>> +            Assert.fail("Shouldn't go to this stage, location doesn't
>> exist");
>> >>> +        } catch (IOException e) {
>> >>> +            // ok
>> >>> +        }
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testInstallBundlesStringArray() 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(new Long(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"));
>> >>> +
>> >>> +        /*
>> >>> +         * this one is not passing bug in api bundleInError should be
>> String CompositeData data2 =
>> >>> +         * mbean.installBundles(locations); BatchInstallResult batch2
>> = BatchInstallResult.from(data2);
>> >>> +         * Assert.assertNotNull(batch2);
>> Assert.assertNull(batch2.getCompleted());
>> >>> +         * Assert.assertFalse(batch2.isSuccess());
>> Assert.assertNotNull(batch2.getError());
>> >>> +         * Assert.assertEquals("file:test.jar",
>> batch2.getBundleInError());
>> >>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> >>> +         */
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testInstallBundlesStringArrayStringArray() throws
>> Exception {
>> >>> +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.installBundle(Mockito.anyString(),
>> Mockito.any(InputStream.class))).thenReturn(bundle);
>> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> >>> +        InputStream stream = Mockito.mock(InputStream.class);
>> >>> +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> >>> +        CompositeData data = spiedMBean.installBundles(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"));
>> >>> +
>> >>> +        /*
>> >>> +         * this one is not passing bug in api bundleInError should be
>> String CompositeData data2 =
>> >>> +         * spiedMBean.installBundles(new String[]{"file:test.jar"},
>> new String[]{"test.jar"}); BatchInstallResult batch2
>> >>> +         * = BatchInstallResult.from(data2);
>> Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>> >>> +         * Assert.assertFalse(batch2.isSuccess());
>> Assert.assertNotNull(batch2.getError());
>> >>> +         * Assert.assertEquals("file:test.jar",
>> batch2.getBundleInError());
>> >>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> >>> +         */
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testRefreshPackagesLong() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> >>> +
>> >>> +        mbean.refreshPackages(1);
>> >>> +        Mockito.verify(admin).refreshPackages((Bundle[])
>> Mockito.any());
>> >>> +
>> >>> +        try {
>> >>> +            mbean.refreshPackages(2);
>> >>> +            Assert.fail("Shouldn't happen illegal argument");
>> >>> +        } catch (IllegalArgumentException iae) {
>> >>> +            // expected
>> >>> +        }
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testRefreshPackagesLongArray() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> >>> +
>> >>> +        mbean.refreshPackages(new long[] { 1 });
>> >>> +        Mockito.verify(admin).refreshPackages((Bundle[])
>> Mockito.any());
>> >>> +
>> >>> +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
>> >>> +        // shouldn't found bundle whith id 2
>> >>> +        Assert.assertNotNull(data);
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(0, batch.getCompleted().length);
>> >>> +        Assert.assertFalse(batch.isSuccess());
>> >>> +        Assert.assertNotNull(batch.getError());
>> >>> +        Assert.assertNotNull(batch.getRemainingItems());
>> >>> +        Assert.assertEquals(2, batch.getBundleInError());
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testResolveBundle() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> >>> +
>> >>> +        mbean.resolveBundle(1);
>> >>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testResolveBundles() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> >>> +
>> >>> +        mbean.resolveBundles(new long[] { 1 });
>> >>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testRestartFramework() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> >>> +        mbean.restartFramework();
>> >>> +        Mockito.verify(bundle).update();
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testSetBundleStartLevel() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> >>> +        mbean.setBundleStartLevel(2, 1);
>> >>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testSetBundleStartLevels() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> >>> +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2
>> }, new int[] { 2 });
>> >>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(2, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = mbean.setBundleStartLevels(new long[] {
>> 2 }, new int[] { 2, 4 });
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertNull(batch2.getCompleted());
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testSetFrameworkStartLevel() throws IOException {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> >>> +        mbean.setFrameworkStartLevel(1);
>> >>> +        Mockito.verify(startLevel).setStartLevel(1);
>> >>> +
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testSetInitialBundleStartLevel() throws IOException {
>> >>> +        mbean.setInitialBundleStartLevel(5);
>> >>> +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testShutdownFramework() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> >>> +        mbean.shutdownFramework();
>> >>> +        Mockito.verify(bundle).stop();
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testStartBundle() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        mbean.startBundle(5);
>> >>> +        Mockito.verify(bundle).start();
>> >>> +
>> >>> +        Mockito.reset(context);
>> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> >>> +        Mockito.doThrow(new BundleException("")).when(bundle).start();
>> >>> +
>> >>> +        try {
>> >>> +            mbean.startBundle(6);
>> >>> +            Assert.fail("Shouldn't go to this stage, BundleException
>> was thrown");
>> >>> +        } catch (IOException ioe) {
>> >>> +            //expected
>> >>> +        }
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testStartBundles() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        CompositeData data = mbean.startBundles(new long[] { 5 });
>> >>> +        Mockito.verify(bundle).start();
>> >>> +
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = mbean.startBundles(null);
>> >>> +
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertNull(batch2.getCompleted());
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testStopBundle() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        mbean.stopBundle(5);
>> >>> +        Mockito.verify(bundle).stop();
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testStopBundles() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        CompositeData data = mbean.stopBundles(new long[] { 5 });
>> >>> +        Mockito.verify(bundle).stop();
>> >>> +
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = mbean.stopBundles(null);
>> >>> +
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertNull(batch2.getCompleted());
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUninstallBundle() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        mbean.uninstallBundle(5);
>> >>> +        Mockito.verify(bundle).uninstall();
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUninstallBundles() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
>> >>> +        Mockito.verify(bundle).uninstall();
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = mbean.uninstallBundles(null);
>> >>> +
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertNull(batch2.getCompleted());
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUpdateBundleLong() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        mbean.updateBundle(5);
>> >>> +        Mockito.verify(bundle).update();
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUpdateBundleLongString() throws Exception {
>> >>> +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> >>> +        InputStream stream = Mockito.mock(InputStream.class);
>> >>> +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        spiedMBean.updateBundle(5, "file:test.jar");
>> >>> +        Mockito.verify(bundle).update(stream);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUpdateBundlesLongArray() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        CompositeData data = mbean.updateBundles(new long[] { 5 });
>> >>> +        Mockito.verify(bundle).update();
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = mbean.updateBundles(null);
>> >>> +
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertNull(batch2.getCompleted());
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +
>> >>> +        Mockito.reset(bundle);
>> >>> +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
>> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> >>> +        Mockito.doThrow(new
>> BundleException("")).when(bundle).update();
>> >>> +        BatchActionResult batch3 = BatchActionResult.from(data3);
>> >>> +        Assert.assertEquals(0, batch3.getCompleted().length);
>> >>> +        Assert.assertFalse(batch3.isSuccess());
>> >>> +        Assert.assertNotNull(batch3.getError());
>> >>> +        Assert.assertEquals(6, batch3.getBundleInError());
>> >>> +
>> >>> +        Bundle bundle6 = Mockito.mock(Bundle.class);
>> >>> +        Bundle bundle8 = Mockito.mock(Bundle.class);
>> >>> +        Bundle bundle7 = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
>> >>> +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
>> >>> +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
>> >>> +        Mockito.doThrow(new
>> BundleException("")).when(bundle8).update();
>> >>> +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7
>> });
>> >>> +        BatchActionResult batch4 = BatchActionResult.from(data4);
>> >>> +        Mockito.verify(bundle6).update();
>> >>> +        Assert.assertEquals(1, batch4.getCompleted().length);
>> >>> +        // should contain only bundleid 6
>> >>> +        Assert.assertEquals(6, batch4.getCompleted()[0]);
>> >>> +        Assert.assertFalse(batch4.isSuccess());
>> >>> +        Assert.assertNotNull(batch4.getError());
>> >>> +        Assert.assertEquals(8, batch4.getBundleInError());
>> >>> +        Assert.assertEquals(1, batch4.getRemainingItems().length);
>> >>> +        // should contain only bundleid 7
>> >>> +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUpdateBundlesLongArrayStringArray() throws
>> Exception {
>> >>> +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> >>> +        InputStream stream = Mockito.mock(InputStream.class);
>> >>> +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> >>> +        CompositeData data = spiedMBean.updateBundles(new long[] { 5
>> }, new String[] { "file:test.jar" });
>> >>> +        Mockito.verify(bundle).update(stream);
>> >>> +        BatchActionResult batch = BatchActionResult.from(data);
>> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> >>> +        Assert.assertTrue(batch.isSuccess());
>> >>> +        Assert.assertNull(batch.getError());
>> >>> +        Assert.assertNull(batch.getRemainingItems());
>> >>> +
>> >>> +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2,
>> 4 }, new String[] { "file:test.jar" });
>> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> >>> +        Assert.assertFalse(batch2.isSuccess());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNotNull(batch2.getError());
>> >>> +        Assert.assertNull(batch2.getRemainingItems());
>> >>> +    }
>> >>> +
>> >>> +    @Test
>> >>> +    public void testUpdateFramework() throws Exception {
>> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> >>> +        mbean.restartFramework();
>> >>> +        Mockito.verify(bundle).update();
>> >>> +    }
>> >>> +
>> >>> +    /**
>> >>> +     * Mocking {@link Framework} method createStream.
>> >>> +     */
>> >>> +    private class FrameworkPartialMock extends Framework {
>> >>> +
>> >>> +        public FrameworkPartialMock(BundleContext context, StartLevel
>> startLevel, PackageAdmin packageAdmin) {
>> >>> +            super(context, startLevel, packageAdmin);
>> >>> +        }
>> >>> +
>> >>> +        public InputStream createStream(String url) throws IOException
>> {
>> >>> +            return null;
>> >>> +        }
>> >>> +    }
>> >>> +
>> >>> +}
>> >>>
>> >>> Propchange:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> >>>
>> ------------------------------------------------------------------------------
>> >>>    svn:eol-style = native
>> >>>
>> >>> Propchange:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> >>>
>> ------------------------------------------------------------------------------
>> >>>    svn:keywords = Revision Date
>> >>>
>> >>>
>> >>>
>> >>
>> >>
>> >>
>> >> --
>> >> Alasdair Nottingham
>> >> alasdair.nottingham@gmail.com
>> >>
>> >
>>
>>
>>
>> --
>>  Alasdair Nottingham
>> alasdair.nottingham@gmail.com
>>
>



-- 
Alasdair Nottingham
alasdair.nottingham@gmail.com

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by adam wojtuniak <ad...@gmail.com>.
Its not because test are written this is because implementation of MBean is
wirtten this way.
It has to be this way. Implementation needs to use
installBundle(location,stream). In the implemetation
we have to create stream from url and put the stream to
installBundle(..,..). In test I don't want to use real stream so I mocked
it.
I don't see anything odd in that. Later on I will provide integarationtest
for JMX as well.

Cheers,
Adam

On Thu, Dec 3, 2009 at 2:06 PM, Alasdair Nottingham <
alasdair.nottingham@gmail.com> wrote:

> It still seems strange to me that it has to be done the way it is done
> because the tests are written this way.
>
> It just seems odd.
>
> Alasdair
>
> 2009/12/1 Lin Sun <li...@gmail.com>:
>  > Hi,
> >
> > I wonder if the inputStream is created from a file directory, equinox
> > will actually ignore the inputstream and use the content of the
> > directory.   This was what I observed when I last time used this
> > method(context.installBundle(string, inputStream)).
> >
> > It would be great if we could have some itest on this. :)
> >
> > Lin
> >
> >
> > On Tue, Dec 1, 2009 at 11:18 AM, Alasdair Nottingham
> > <al...@gmail.com> wrote:
> >> Hi,
> >>
> >> Looking at this change the installBundle method does this:
> >>
> >>            inputStream = createStream(url);
> >>            Bundle bundle = context.installBundle(location, inputStream);
> >>
> >> when I think it could just do this:
> >>
> >>            Bundle bundle = context.installBundle(location);
> >>
> >> By doing it the following way you could install a directory as a
> >> bundle if the framework is equinox.
> >>
> >> What do you think?
> >> Alasdair
> >>
> >> 2009/11/30  <aw...@apache.org>:
> >>> Author: awojtuniak
> >>> Date: Mon Nov 30 22:41:08 2009
> >>> New Revision: 885603
> >>>
> >>> URL: http://svn.apache.org/viewvc?rev=885603&view=rev
> >>> Log:
> >>> ARIES-30 Framework MBean test case
> >>>
> >>> Added:
> >>>
>  incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>   (with props)
> >>> Modified:
> >>>
>  incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >>>
> >>> Modified:
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >>> URL:
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
> >>>
> ==============================================================================
> >>> ---
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> (original)
> >>> +++
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> Mon Nov 30 22:41:08 2009
> >>> @@ -88,16 +88,27 @@
> >>>      * @see
> org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String,
> java.lang.String)
> >>>      */
> >>>     public long installBundle(String location, String url) throws
> IOException {
> >>> -        InputStream inputStream;
> >>> +        InputStream inputStream = null;
> >>>         try {
> >>> -            inputStream = new URL(url).openStream();
> >>> +            inputStream = createStream(url);
> >>>             Bundle bundle = context.installBundle(location,
> inputStream);
> >>>             return bundle.getBundleId();
> >>>         } catch (BundleException e) {
> >>> +            if (inputStream != null) {
> >>> +                try {
> >>> +                    inputStream.close();
> >>> +                } catch (IOException ioe) {
> >>> +
> >>> +                }
> >>> +            }
> >>>             throw new IOException("Can't install bundle with location"
> + location);
> >>>         }
> >>>     }
> >>>
> >>> +    public InputStream createStream(String url) throws IOException {
> >>> +        return new URL(url).openStream();
> >>> +    }
> >>> +
> >>>     /**
> >>>      * @see
> org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
> >>>      */
> >>> @@ -170,12 +181,7 @@
> >>>             try {
> >>>                 refreshPackages(bundleIdentifiers[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -241,12 +247,7 @@
> >>>             try {
> >>>                 setBundleStartLevel(bundleIdentifiers[i],
> newlevels[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -304,12 +305,7 @@
> >>>             try {
> >>>                 startBundle(bundleIdentifiers[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -340,12 +336,7 @@
> >>>             try {
> >>>                 stopBundle(bundleIdentifiers[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -355,7 +346,7 @@
> >>>      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
> >>>      */
> >>>     public void uninstallBundle(long bundleIdentifier) throws
> IOException {
> >>> -        Bundle bundle = context.getBundle(bundleIdentifier);
> >>> +        Bundle bundle = getBundle(bundleIdentifier);
> >>>         if (bundle != null) {
> >>>             try {
> >>>                 bundle.uninstall();
> >>> @@ -364,8 +355,6 @@
> >>>             }
> >>>         }
> >>>
> >>> -        throw new IllegalArgumentException("Can't find bundle with id
> " + bundleIdentifier);
> >>> -
> >>>     }
> >>>
> >>>     /**
> >>> @@ -379,12 +368,7 @@
> >>>             try {
> >>>                 uninstallBundle(bundleIdentifiers[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -408,10 +392,10 @@
> >>>      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long,
> java.lang.String)
> >>>      */
> >>>     public void updateBundle(long bundleIdentifier, String url) throws
> IOException {
> >>> -        Bundle bundle = getBundle(0);
> >>> +        Bundle bundle = getBundle(bundleIdentifier);
> >>>         InputStream inputStream = null;
> >>>         try {
> >>> -            inputStream = new URL(url).openStream();
> >>> +            inputStream = createStream(url);
> >>>             bundle.update(inputStream);
> >>>         } catch (BundleException be) {
> >>>             if (inputStream != null) {
> >>> @@ -437,12 +421,7 @@
> >>>             try {
> >>>                 updateBundle(bundleIdentifiers[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -463,12 +442,7 @@
> >>>             try {
> >>>                 updateBundle(bundleIdentifiers[i], urls[i]);
> >>>             } catch (Throwable t) {
> >>> -                long[] completed = new long[i];
> >>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >>> -                long[] remaining = new long[completed.length - i - 1];
> >>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0, remaining.length);
> >>> -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> >>> -                        .toCompositeData();
> >>> +                return
> createFailedBatchActionResult(bundleIdentifiers, i, t);
> >>>             }
> >>>         }
> >>>         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> >>> @@ -488,6 +462,22 @@
> >>>     }
> >>>
> >>>     /**
> >>> +     * Create {@link BatchActionResult}, when the operation fail.
> >>> +     *
> >>> +     * @param bundleIdentifiers bundle ids for operation.
> >>> +     * @param i index of loop pointing on which operation fails.
> >>> +     * @param t Throwable thrown by failed operation.
> >>> +     * @return created BatchActionResult instance.
> >>> +     */
> >>> +    private CompositeData createFailedBatchActionResult(long[]
> bundleIdentifiers, int i, Throwable t) {
> >>> +        long[] completed = new long[i];
> >>> +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> >>> +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
> >>> +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> >>> +        return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i]).toCompositeData();
> >>> +    }
> >>> +
> >>> +    /**
> >>>      * Gets bundle with provided bundleId.
> >>>      *
> >>>      * @param bundleIdentifier bundle id.
> >>>
> >>> Added:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >>> URL:
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
> >>>
> ==============================================================================
> >>> ---
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> (added)
> >>> +++
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> Mon Nov 30 22:41:08 2009
> >>> @@ -0,0 +1,510 @@
> >>> +/**
> >>> + *  Licensed to the Apache Software Foundation (ASF) under one or more
> >>> + *  contributor license agreements.  See the NOTICE file distributed
> with
> >>> + *  this work for additional information regarding copyright
> ownership.
> >>> + *  The ASF licenses this file to You under the Apache License,
> Version 2.0
> >>> + *  (the "License"); you may not use this file except in compliance
> with
> >>> + *  the License.  You may obtain a copy of the License at
> >>> + *
> >>> + *     http://www.apache.org/licenses/LICENSE-2.0
> >>> + *
> >>> + *  Unless required by applicable law or agreed to in writing,
> software
> >>> + *  distributed under the License is distributed on an "AS IS" BASIS,
> >>> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> >>> + *  See the License for the specific language governing permissions
> and
> >>> + *  limitations under the License.
> >>> + */
> >>> +package org.apache.aries.jmx.framework;
> >>> +
> >>> +import java.io.IOException;
> >>> +import java.io.InputStream;
> >>> +
> >>> +import javax.management.openmbean.CompositeData;
> >>> +
> >>> +import org.junit.Assert;
> >>> +
> >>> +import org.apache.aries.jmx.codec.BatchActionResult;
> >>> +import org.apache.aries.jmx.codec.BatchInstallResult;
> >>> +import org.junit.Before;
> >>> +import org.junit.Test;
> >>> +import org.mockito.Mock;
> >>> +import org.mockito.Mockito;
> >>> +import org.mockito.MockitoAnnotations;
> >>> +import org.osgi.framework.Bundle;
> >>> +import org.osgi.framework.BundleContext;
> >>> +import org.osgi.framework.BundleException;
> >>> +import org.osgi.jmx.framework.FrameworkMBean;
> >>> +import org.osgi.service.packageadmin.PackageAdmin;
> >>> +import org.osgi.service.startlevel.StartLevel;
> >>> +
> >>> +/**
> >>> + * {@link FrameworkMBean} test case.
> >>> + *
> >>> + *
> >>> + * @version $Rev$ $Date$
> >>> + */
> >>> +public class FrameworkTest {
> >>> +
> >>> +    @Mock
> >>> +    private StartLevel startLevel;
> >>> +    @Mock
> >>> +    private PackageAdmin admin;
> >>> +    @Mock
> >>> +    private BundleContext context;
> >>> +    private Framework mbean;
> >>> +
> >>> +    @Before
> >>> +    public void setUp() throws Exception {
> >>> +        MockitoAnnotations.initMocks(this);
> >>> +        mbean = new Framework(context, startLevel, admin);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testGetFrameworkStartLevel() throws IOException {
> >>> +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
> >>> +        int level = mbean.getFrameworkStartLevel();
> >>> +        Assert.assertEquals(1, level);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testGetInitialBundleStartLevel() throws IOException {
> >>> +
>  Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
> >>> +        int level = mbean.getInitialBundleStartLevel();
> >>> +        Mockito.verify(startLevel).getInitialBundleStartLevel();
> >>> +        Assert.assertEquals(2, level);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testInstallBundleString() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +
>  Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >>> +        long bundleId = mbean.installBundle("file:test.jar");
> >>> +        Assert.assertEquals(2, bundleId);
> >>> +        Mockito.reset(context);
> >>> +
>  Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new
> BundleException("location doesn't exist"));
> >>> +
> >>> +        try {
> >>> +            mbean.installBundle("file:test2.jar");
> >>> +            Assert.fail("Shouldn't go to this stage, location doesn't
> exist");
> >>> +        } catch (IOException e) {
> >>> +            // ok
> >>> +        }
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testInstallBundleStringString() throws Exception {
> >>> +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.installBundle(Mockito.anyString(),
> Mockito.any(InputStream.class))).thenReturn(bundle);
> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
> >>> +        InputStream stream = Mockito.mock(InputStream.class);
> >>> +
>  Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
> >>> +        long bundleId = spiedMBean.installBundle("file:test.jar",
> "test.jar");
> >>> +        Assert.assertEquals(2, bundleId);
> >>> +        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"));
> >>> +
> >>> +        try {
> >>> +            spiedMBean.installBundle("file:test2.jar", "test.jar");
> >>> +            Assert.fail("Shouldn't go to this stage, location doesn't
> exist");
> >>> +        } catch (IOException e) {
> >>> +            // ok
> >>> +        }
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testInstallBundlesStringArray() 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(new Long(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"));
> >>> +
> >>> +        /*
> >>> +         * this one is not passing bug in api bundleInError should be
> String CompositeData data2 =
> >>> +         * mbean.installBundles(locations); BatchInstallResult batch2
> = BatchInstallResult.from(data2);
> >>> +         * Assert.assertNotNull(batch2);
> Assert.assertNull(batch2.getCompleted());
> >>> +         * Assert.assertFalse(batch2.isSuccess());
> Assert.assertNotNull(batch2.getError());
> >>> +         * Assert.assertEquals("file:test.jar",
> batch2.getBundleInError());
> >>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
> >>> +         */
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testInstallBundlesStringArrayStringArray() throws
> Exception {
> >>> +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.installBundle(Mockito.anyString(),
> Mockito.any(InputStream.class))).thenReturn(bundle);
> >>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
> >>> +        InputStream stream = Mockito.mock(InputStream.class);
> >>> +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >>> +        CompositeData data = spiedMBean.installBundles(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"));
> >>> +
> >>> +        /*
> >>> +         * this one is not passing bug in api bundleInError should be
> String CompositeData data2 =
> >>> +         * spiedMBean.installBundles(new String[]{"file:test.jar"},
> new String[]{"test.jar"}); BatchInstallResult batch2
> >>> +         * = BatchInstallResult.from(data2);
> Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
> >>> +         * Assert.assertFalse(batch2.isSuccess());
> Assert.assertNotNull(batch2.getError());
> >>> +         * Assert.assertEquals("file:test.jar",
> batch2.getBundleInError());
> >>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
> >>> +         */
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testRefreshPackagesLong() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >>> +
> >>> +        mbean.refreshPackages(1);
> >>> +        Mockito.verify(admin).refreshPackages((Bundle[])
> Mockito.any());
> >>> +
> >>> +        try {
> >>> +            mbean.refreshPackages(2);
> >>> +            Assert.fail("Shouldn't happen illegal argument");
> >>> +        } catch (IllegalArgumentException iae) {
> >>> +            // expected
> >>> +        }
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testRefreshPackagesLongArray() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >>> +
> >>> +        mbean.refreshPackages(new long[] { 1 });
> >>> +        Mockito.verify(admin).refreshPackages((Bundle[])
> Mockito.any());
> >>> +
> >>> +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
> >>> +        // shouldn't found bundle whith id 2
> >>> +        Assert.assertNotNull(data);
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(0, batch.getCompleted().length);
> >>> +        Assert.assertFalse(batch.isSuccess());
> >>> +        Assert.assertNotNull(batch.getError());
> >>> +        Assert.assertNotNull(batch.getRemainingItems());
> >>> +        Assert.assertEquals(2, batch.getBundleInError());
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testResolveBundle() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >>> +
> >>> +        mbean.resolveBundle(1);
> >>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testResolveBundles() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >>> +
> >>> +        mbean.resolveBundles(new long[] { 1 });
> >>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testRestartFramework() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >>> +        mbean.restartFramework();
> >>> +        Mockito.verify(bundle).update();
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testSetBundleStartLevel() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> >>> +        mbean.setBundleStartLevel(2, 1);
> >>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testSetBundleStartLevels() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> >>> +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2
> }, new int[] { 2 });
> >>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(2, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = mbean.setBundleStartLevels(new long[] {
> 2 }, new int[] { 2, 4 });
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertNull(batch2.getCompleted());
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testSetFrameworkStartLevel() throws IOException {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >>> +        mbean.setFrameworkStartLevel(1);
> >>> +        Mockito.verify(startLevel).setStartLevel(1);
> >>> +
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testSetInitialBundleStartLevel() throws IOException {
> >>> +        mbean.setInitialBundleStartLevel(5);
> >>> +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testShutdownFramework() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >>> +        mbean.shutdownFramework();
> >>> +        Mockito.verify(bundle).stop();
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testStartBundle() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        mbean.startBundle(5);
> >>> +        Mockito.verify(bundle).start();
> >>> +
> >>> +        Mockito.reset(context);
> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> >>> +        Mockito.doThrow(new BundleException("")).when(bundle).start();
> >>> +
> >>> +        try {
> >>> +            mbean.startBundle(6);
> >>> +            Assert.fail("Shouldn't go to this stage, BundleException
> was thrown");
> >>> +        } catch (IOException ioe) {
> >>> +            //expected
> >>> +        }
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testStartBundles() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        CompositeData data = mbean.startBundles(new long[] { 5 });
> >>> +        Mockito.verify(bundle).start();
> >>> +
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = mbean.startBundles(null);
> >>> +
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertNull(batch2.getCompleted());
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testStopBundle() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        mbean.stopBundle(5);
> >>> +        Mockito.verify(bundle).stop();
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testStopBundles() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        CompositeData data = mbean.stopBundles(new long[] { 5 });
> >>> +        Mockito.verify(bundle).stop();
> >>> +
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = mbean.stopBundles(null);
> >>> +
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertNull(batch2.getCompleted());
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUninstallBundle() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        mbean.uninstallBundle(5);
> >>> +        Mockito.verify(bundle).uninstall();
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUninstallBundles() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
> >>> +        Mockito.verify(bundle).uninstall();
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = mbean.uninstallBundles(null);
> >>> +
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertNull(batch2.getCompleted());
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUpdateBundleLong() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        mbean.updateBundle(5);
> >>> +        Mockito.verify(bundle).update();
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUpdateBundleLongString() throws Exception {
> >>> +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
> >>> +        InputStream stream = Mockito.mock(InputStream.class);
> >>> +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        spiedMBean.updateBundle(5, "file:test.jar");
> >>> +        Mockito.verify(bundle).update(stream);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUpdateBundlesLongArray() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        CompositeData data = mbean.updateBundles(new long[] { 5 });
> >>> +        Mockito.verify(bundle).update();
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = mbean.updateBundles(null);
> >>> +
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertNull(batch2.getCompleted());
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +
> >>> +        Mockito.reset(bundle);
> >>> +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> >>> +        Mockito.doThrow(new
> BundleException("")).when(bundle).update();
> >>> +        BatchActionResult batch3 = BatchActionResult.from(data3);
> >>> +        Assert.assertEquals(0, batch3.getCompleted().length);
> >>> +        Assert.assertFalse(batch3.isSuccess());
> >>> +        Assert.assertNotNull(batch3.getError());
> >>> +        Assert.assertEquals(6, batch3.getBundleInError());
> >>> +
> >>> +        Bundle bundle6 = Mockito.mock(Bundle.class);
> >>> +        Bundle bundle8 = Mockito.mock(Bundle.class);
> >>> +        Bundle bundle7 = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
> >>> +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
> >>> +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
> >>> +        Mockito.doThrow(new
> BundleException("")).when(bundle8).update();
> >>> +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7
> });
> >>> +        BatchActionResult batch4 = BatchActionResult.from(data4);
> >>> +        Mockito.verify(bundle6).update();
> >>> +        Assert.assertEquals(1, batch4.getCompleted().length);
> >>> +        // should contain only bundleid 6
> >>> +        Assert.assertEquals(6, batch4.getCompleted()[0]);
> >>> +        Assert.assertFalse(batch4.isSuccess());
> >>> +        Assert.assertNotNull(batch4.getError());
> >>> +        Assert.assertEquals(8, batch4.getBundleInError());
> >>> +        Assert.assertEquals(1, batch4.getRemainingItems().length);
> >>> +        // should contain only bundleid 7
> >>> +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUpdateBundlesLongArrayStringArray() throws
> Exception {
> >>> +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> >>> +        Framework spiedMBean = Mockito.spy(partialMock);
> >>> +        InputStream stream = Mockito.mock(InputStream.class);
> >>> +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >>> +        CompositeData data = spiedMBean.updateBundles(new long[] { 5
> }, new String[] { "file:test.jar" });
> >>> +        Mockito.verify(bundle).update(stream);
> >>> +        BatchActionResult batch = BatchActionResult.from(data);
> >>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >>> +        Assert.assertTrue(batch.isSuccess());
> >>> +        Assert.assertNull(batch.getError());
> >>> +        Assert.assertNull(batch.getRemainingItems());
> >>> +
> >>> +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2,
> 4 }, new String[] { "file:test.jar" });
> >>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >>> +        Assert.assertFalse(batch2.isSuccess());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNotNull(batch2.getError());
> >>> +        Assert.assertNull(batch2.getRemainingItems());
> >>> +    }
> >>> +
> >>> +    @Test
> >>> +    public void testUpdateFramework() throws Exception {
> >>> +        Bundle bundle = Mockito.mock(Bundle.class);
> >>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >>> +        mbean.restartFramework();
> >>> +        Mockito.verify(bundle).update();
> >>> +    }
> >>> +
> >>> +    /**
> >>> +     * Mocking {@link Framework} method createStream.
> >>> +     */
> >>> +    private class FrameworkPartialMock extends Framework {
> >>> +
> >>> +        public FrameworkPartialMock(BundleContext context, StartLevel
> startLevel, PackageAdmin packageAdmin) {
> >>> +            super(context, startLevel, packageAdmin);
> >>> +        }
> >>> +
> >>> +        public InputStream createStream(String url) throws IOException
> {
> >>> +            return null;
> >>> +        }
> >>> +    }
> >>> +
> >>> +}
> >>>
> >>> Propchange:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >>>
> ------------------------------------------------------------------------------
> >>>    svn:eol-style = native
> >>>
> >>> Propchange:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >>>
> ------------------------------------------------------------------------------
> >>>    svn:keywords = Revision Date
> >>>
> >>>
> >>>
> >>
> >>
> >>
> >> --
> >> Alasdair Nottingham
> >> alasdair.nottingham@gmail.com
> >>
> >
>
>
>
> --
>  Alasdair Nottingham
> alasdair.nottingham@gmail.com
>

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by Alasdair Nottingham <al...@gmail.com>.
It still seems strange to me that it has to be done the way it is done
because the tests are written this way.

It just seems odd.

Alasdair

2009/12/1 Lin Sun <li...@gmail.com>:
> Hi,
>
> I wonder if the inputStream is created from a file directory, equinox
> will actually ignore the inputstream and use the content of the
> directory.   This was what I observed when I last time used this
> method(context.installBundle(string, inputStream)).
>
> It would be great if we could have some itest on this. :)
>
> Lin
>
>
> On Tue, Dec 1, 2009 at 11:18 AM, Alasdair Nottingham
> <al...@gmail.com> wrote:
>> Hi,
>>
>> Looking at this change the installBundle method does this:
>>
>>            inputStream = createStream(url);
>>            Bundle bundle = context.installBundle(location, inputStream);
>>
>> when I think it could just do this:
>>
>>            Bundle bundle = context.installBundle(location);
>>
>> By doing it the following way you could install a directory as a
>> bundle if the framework is equinox.
>>
>> What do you think?
>> Alasdair
>>
>> 2009/11/30  <aw...@apache.org>:
>>> Author: awojtuniak
>>> Date: Mon Nov 30 22:41:08 2009
>>> New Revision: 885603
>>>
>>> URL: http://svn.apache.org/viewvc?rev=885603&view=rev
>>> Log:
>>> ARIES-30 Framework MBean test case
>>>
>>> Added:
>>>    incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java   (with props)
>>> Modified:
>>>    incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>>>
>>> Modified: incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>>> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
>>> ==============================================================================
>>> --- incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java (original)
>>> +++ incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java Mon Nov 30 22:41:08 2009
>>> @@ -88,16 +88,27 @@
>>>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String, java.lang.String)
>>>      */
>>>     public long installBundle(String location, String url) throws IOException {
>>> -        InputStream inputStream;
>>> +        InputStream inputStream = null;
>>>         try {
>>> -            inputStream = new URL(url).openStream();
>>> +            inputStream = createStream(url);
>>>             Bundle bundle = context.installBundle(location, inputStream);
>>>             return bundle.getBundleId();
>>>         } catch (BundleException e) {
>>> +            if (inputStream != null) {
>>> +                try {
>>> +                    inputStream.close();
>>> +                } catch (IOException ioe) {
>>> +
>>> +                }
>>> +            }
>>>             throw new IOException("Can't install bundle with location" + location);
>>>         }
>>>     }
>>>
>>> +    public InputStream createStream(String url) throws IOException {
>>> +        return new URL(url).openStream();
>>> +    }
>>> +
>>>     /**
>>>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
>>>      */
>>> @@ -170,12 +181,7 @@
>>>             try {
>>>                 refreshPackages(bundleIdentifiers[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -241,12 +247,7 @@
>>>             try {
>>>                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -304,12 +305,7 @@
>>>             try {
>>>                 startBundle(bundleIdentifiers[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -340,12 +336,7 @@
>>>             try {
>>>                 stopBundle(bundleIdentifiers[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -355,7 +346,7 @@
>>>      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
>>>      */
>>>     public void uninstallBundle(long bundleIdentifier) throws IOException {
>>> -        Bundle bundle = context.getBundle(bundleIdentifier);
>>> +        Bundle bundle = getBundle(bundleIdentifier);
>>>         if (bundle != null) {
>>>             try {
>>>                 bundle.uninstall();
>>> @@ -364,8 +355,6 @@
>>>             }
>>>         }
>>>
>>> -        throw new IllegalArgumentException("Can't find bundle with id " + bundleIdentifier);
>>> -
>>>     }
>>>
>>>     /**
>>> @@ -379,12 +368,7 @@
>>>             try {
>>>                 uninstallBundle(bundleIdentifiers[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -408,10 +392,10 @@
>>>      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long, java.lang.String)
>>>      */
>>>     public void updateBundle(long bundleIdentifier, String url) throws IOException {
>>> -        Bundle bundle = getBundle(0);
>>> +        Bundle bundle = getBundle(bundleIdentifier);
>>>         InputStream inputStream = null;
>>>         try {
>>> -            inputStream = new URL(url).openStream();
>>> +            inputStream = createStream(url);
>>>             bundle.update(inputStream);
>>>         } catch (BundleException be) {
>>>             if (inputStream != null) {
>>> @@ -437,12 +421,7 @@
>>>             try {
>>>                 updateBundle(bundleIdentifiers[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -463,12 +442,7 @@
>>>             try {
>>>                 updateBundle(bundleIdentifiers[i], urls[i]);
>>>             } catch (Throwable t) {
>>> -                long[] completed = new long[i];
>>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> -                long[] remaining = new long[completed.length - i - 1];
>>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>>> -                        .toCompositeData();
>>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>>             }
>>>         }
>>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>>> @@ -488,6 +462,22 @@
>>>     }
>>>
>>>     /**
>>> +     * Create {@link BatchActionResult}, when the operation fail.
>>> +     *
>>> +     * @param bundleIdentifiers bundle ids for operation.
>>> +     * @param i index of loop pointing on which operation fails.
>>> +     * @param t Throwable thrown by failed operation.
>>> +     * @return created BatchActionResult instance.
>>> +     */
>>> +    private CompositeData createFailedBatchActionResult(long[] bundleIdentifiers, int i, Throwable t) {
>>> +        long[] completed = new long[i];
>>> +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>>> +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
>>> +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>>> +        return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i]).toCompositeData();
>>> +    }
>>> +
>>> +    /**
>>>      * Gets bundle with provided bundleId.
>>>      *
>>>      * @param bundleIdentifier bundle id.
>>>
>>> Added: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>>> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
>>> ==============================================================================
>>> --- incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java (added)
>>> +++ incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java Mon Nov 30 22:41:08 2009
>>> @@ -0,0 +1,510 @@
>>> +/**
>>> + *  Licensed to the Apache Software Foundation (ASF) under one or more
>>> + *  contributor license agreements.  See the NOTICE file distributed with
>>> + *  this work for additional information regarding copyright ownership.
>>> + *  The ASF licenses this file to You under the Apache License, Version 2.0
>>> + *  (the "License"); you may not use this file except in compliance with
>>> + *  the License.  You may obtain a copy of the License at
>>> + *
>>> + *     http://www.apache.org/licenses/LICENSE-2.0
>>> + *
>>> + *  Unless required by applicable law or agreed to in writing, software
>>> + *  distributed under the License is distributed on an "AS IS" BASIS,
>>> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> + *  See the License for the specific language governing permissions and
>>> + *  limitations under the License.
>>> + */
>>> +package org.apache.aries.jmx.framework;
>>> +
>>> +import java.io.IOException;
>>> +import java.io.InputStream;
>>> +
>>> +import javax.management.openmbean.CompositeData;
>>> +
>>> +import org.junit.Assert;
>>> +
>>> +import org.apache.aries.jmx.codec.BatchActionResult;
>>> +import org.apache.aries.jmx.codec.BatchInstallResult;
>>> +import org.junit.Before;
>>> +import org.junit.Test;
>>> +import org.mockito.Mock;
>>> +import org.mockito.Mockito;
>>> +import org.mockito.MockitoAnnotations;
>>> +import org.osgi.framework.Bundle;
>>> +import org.osgi.framework.BundleContext;
>>> +import org.osgi.framework.BundleException;
>>> +import org.osgi.jmx.framework.FrameworkMBean;
>>> +import org.osgi.service.packageadmin.PackageAdmin;
>>> +import org.osgi.service.startlevel.StartLevel;
>>> +
>>> +/**
>>> + * {@link FrameworkMBean} test case.
>>> + *
>>> + *
>>> + * @version $Rev$ $Date$
>>> + */
>>> +public class FrameworkTest {
>>> +
>>> +    @Mock
>>> +    private StartLevel startLevel;
>>> +    @Mock
>>> +    private PackageAdmin admin;
>>> +    @Mock
>>> +    private BundleContext context;
>>> +    private Framework mbean;
>>> +
>>> +    @Before
>>> +    public void setUp() throws Exception {
>>> +        MockitoAnnotations.initMocks(this);
>>> +        mbean = new Framework(context, startLevel, admin);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testGetFrameworkStartLevel() throws IOException {
>>> +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
>>> +        int level = mbean.getFrameworkStartLevel();
>>> +        Assert.assertEquals(1, level);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testGetInitialBundleStartLevel() throws IOException {
>>> +        Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
>>> +        int level = mbean.getInitialBundleStartLevel();
>>> +        Mockito.verify(startLevel).getInitialBundleStartLevel();
>>> +        Assert.assertEquals(2, level);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testInstallBundleString() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
>>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>>> +        long bundleId = mbean.installBundle("file:test.jar");
>>> +        Assert.assertEquals(2, bundleId);
>>> +        Mockito.reset(context);
>>> +        Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new BundleException("location doesn't exist"));
>>> +
>>> +        try {
>>> +            mbean.installBundle("file:test2.jar");
>>> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
>>> +        } catch (IOException e) {
>>> +            // ok
>>> +        }
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testInstallBundleStringString() throws Exception {
>>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
>>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>>> +        Framework spiedMBean = Mockito.spy(partialMock);
>>> +        InputStream stream = Mockito.mock(InputStream.class);
>>> +        Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
>>> +        long bundleId = spiedMBean.installBundle("file:test.jar", "test.jar");
>>> +        Assert.assertEquals(2, bundleId);
>>> +        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"));
>>> +
>>> +        try {
>>> +            spiedMBean.installBundle("file:test2.jar", "test.jar");
>>> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
>>> +        } catch (IOException e) {
>>> +            // ok
>>> +        }
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testInstallBundlesStringArray() 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(new Long(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"));
>>> +
>>> +        /*
>>> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
>>> +         * mbean.installBundles(locations); BatchInstallResult batch2 = BatchInstallResult.from(data2);
>>> +         * Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>>> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
>>> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
>>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>>> +         */
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testInstallBundlesStringArrayStringArray() throws Exception {
>>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
>>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>>> +        Framework spiedMBean = Mockito.spy(partialMock);
>>> +        InputStream stream = Mockito.mock(InputStream.class);
>>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>>> +        CompositeData data = spiedMBean.installBundles(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"));
>>> +
>>> +        /*
>>> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
>>> +         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new String[]{"test.jar"}); BatchInstallResult batch2
>>> +         * = BatchInstallResult.from(data2); Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>>> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
>>> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
>>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>>> +         */
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testRefreshPackagesLong() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>>> +
>>> +        mbean.refreshPackages(1);
>>> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>>> +
>>> +        try {
>>> +            mbean.refreshPackages(2);
>>> +            Assert.fail("Shouldn't happen illegal argument");
>>> +        } catch (IllegalArgumentException iae) {
>>> +            // expected
>>> +        }
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testRefreshPackagesLongArray() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>>> +
>>> +        mbean.refreshPackages(new long[] { 1 });
>>> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>>> +
>>> +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
>>> +        // shouldn't found bundle whith id 2
>>> +        Assert.assertNotNull(data);
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(0, batch.getCompleted().length);
>>> +        Assert.assertFalse(batch.isSuccess());
>>> +        Assert.assertNotNull(batch.getError());
>>> +        Assert.assertNotNull(batch.getRemainingItems());
>>> +        Assert.assertEquals(2, batch.getBundleInError());
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testResolveBundle() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>>> +
>>> +        mbean.resolveBundle(1);
>>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testResolveBundles() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>>> +
>>> +        mbean.resolveBundles(new long[] { 1 });
>>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testRestartFramework() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>>> +        mbean.restartFramework();
>>> +        Mockito.verify(bundle).update();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testSetBundleStartLevel() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>>> +        mbean.setBundleStartLevel(2, 1);
>>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testSetBundleStartLevels() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>>> +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2 });
>>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(2, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2, 4 });
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertNull(batch2.getCompleted());
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testSetFrameworkStartLevel() throws IOException {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>>> +        mbean.setFrameworkStartLevel(1);
>>> +        Mockito.verify(startLevel).setStartLevel(1);
>>> +
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testSetInitialBundleStartLevel() throws IOException {
>>> +        mbean.setInitialBundleStartLevel(5);
>>> +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testShutdownFramework() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>>> +        mbean.shutdownFramework();
>>> +        Mockito.verify(bundle).stop();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testStartBundle() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        mbean.startBundle(5);
>>> +        Mockito.verify(bundle).start();
>>> +
>>> +        Mockito.reset(context);
>>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>>> +        Mockito.doThrow(new BundleException("")).when(bundle).start();
>>> +
>>> +        try {
>>> +            mbean.startBundle(6);
>>> +            Assert.fail("Shouldn't go to this stage, BundleException was thrown");
>>> +        } catch (IOException ioe) {
>>> +            //expected
>>> +        }
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testStartBundles() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        CompositeData data = mbean.startBundles(new long[] { 5 });
>>> +        Mockito.verify(bundle).start();
>>> +
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = mbean.startBundles(null);
>>> +
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertNull(batch2.getCompleted());
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testStopBundle() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        mbean.stopBundle(5);
>>> +        Mockito.verify(bundle).stop();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testStopBundles() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        CompositeData data = mbean.stopBundles(new long[] { 5 });
>>> +        Mockito.verify(bundle).stop();
>>> +
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = mbean.stopBundles(null);
>>> +
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertNull(batch2.getCompleted());
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUninstallBundle() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        mbean.uninstallBundle(5);
>>> +        Mockito.verify(bundle).uninstall();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUninstallBundles() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
>>> +        Mockito.verify(bundle).uninstall();
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = mbean.uninstallBundles(null);
>>> +
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertNull(batch2.getCompleted());
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUpdateBundleLong() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        mbean.updateBundle(5);
>>> +        Mockito.verify(bundle).update();
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUpdateBundleLongString() throws Exception {
>>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>>> +        Framework spiedMBean = Mockito.spy(partialMock);
>>> +        InputStream stream = Mockito.mock(InputStream.class);
>>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        spiedMBean.updateBundle(5, "file:test.jar");
>>> +        Mockito.verify(bundle).update(stream);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUpdateBundlesLongArray() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        CompositeData data = mbean.updateBundles(new long[] { 5 });
>>> +        Mockito.verify(bundle).update();
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = mbean.updateBundles(null);
>>> +
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertNull(batch2.getCompleted());
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +
>>> +        Mockito.reset(bundle);
>>> +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
>>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>>> +        Mockito.doThrow(new BundleException("")).when(bundle).update();
>>> +        BatchActionResult batch3 = BatchActionResult.from(data3);
>>> +        Assert.assertEquals(0, batch3.getCompleted().length);
>>> +        Assert.assertFalse(batch3.isSuccess());
>>> +        Assert.assertNotNull(batch3.getError());
>>> +        Assert.assertEquals(6, batch3.getBundleInError());
>>> +
>>> +        Bundle bundle6 = Mockito.mock(Bundle.class);
>>> +        Bundle bundle8 = Mockito.mock(Bundle.class);
>>> +        Bundle bundle7 = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
>>> +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
>>> +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
>>> +        Mockito.doThrow(new BundleException("")).when(bundle8).update();
>>> +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7 });
>>> +        BatchActionResult batch4 = BatchActionResult.from(data4);
>>> +        Mockito.verify(bundle6).update();
>>> +        Assert.assertEquals(1, batch4.getCompleted().length);
>>> +        // should contain only bundleid 6
>>> +        Assert.assertEquals(6, batch4.getCompleted()[0]);
>>> +        Assert.assertFalse(batch4.isSuccess());
>>> +        Assert.assertNotNull(batch4.getError());
>>> +        Assert.assertEquals(8, batch4.getBundleInError());
>>> +        Assert.assertEquals(1, batch4.getRemainingItems().length);
>>> +        // should contain only bundleid 7
>>> +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUpdateBundlesLongArrayStringArray() throws Exception {
>>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>>> +        Framework spiedMBean = Mockito.spy(partialMock);
>>> +        InputStream stream = Mockito.mock(InputStream.class);
>>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>>> +        CompositeData data = spiedMBean.updateBundles(new long[] { 5 }, new String[] { "file:test.jar" });
>>> +        Mockito.verify(bundle).update(stream);
>>> +        BatchActionResult batch = BatchActionResult.from(data);
>>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>>> +        Assert.assertTrue(batch.isSuccess());
>>> +        Assert.assertNull(batch.getError());
>>> +        Assert.assertNull(batch.getRemainingItems());
>>> +
>>> +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4 }, new String[] { "file:test.jar" });
>>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>>> +        Assert.assertFalse(batch2.isSuccess());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNotNull(batch2.getError());
>>> +        Assert.assertNull(batch2.getRemainingItems());
>>> +    }
>>> +
>>> +    @Test
>>> +    public void testUpdateFramework() throws Exception {
>>> +        Bundle bundle = Mockito.mock(Bundle.class);
>>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>>> +        mbean.restartFramework();
>>> +        Mockito.verify(bundle).update();
>>> +    }
>>> +
>>> +    /**
>>> +     * Mocking {@link Framework} method createStream.
>>> +     */
>>> +    private class FrameworkPartialMock extends Framework {
>>> +
>>> +        public FrameworkPartialMock(BundleContext context, StartLevel startLevel, PackageAdmin packageAdmin) {
>>> +            super(context, startLevel, packageAdmin);
>>> +        }
>>> +
>>> +        public InputStream createStream(String url) throws IOException {
>>> +            return null;
>>> +        }
>>> +    }
>>> +
>>> +}
>>>
>>> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>>> ------------------------------------------------------------------------------
>>>    svn:eol-style = native
>>>
>>> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>>> ------------------------------------------------------------------------------
>>>    svn:keywords = Revision Date
>>>
>>>
>>>
>>
>>
>>
>> --
>> Alasdair Nottingham
>> alasdair.nottingham@gmail.com
>>
>



-- 
Alasdair Nottingham
alasdair.nottingham@gmail.com

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by Lin Sun <li...@gmail.com>.
Hi,

I wonder if the inputStream is created from a file directory, equinox
will actually ignore the inputstream and use the content of the
directory.   This was what I observed when I last time used this
method(context.installBundle(string, inputStream)).

It would be great if we could have some itest on this. :)

Lin


On Tue, Dec 1, 2009 at 11:18 AM, Alasdair Nottingham
<al...@gmail.com> wrote:
> Hi,
>
> Looking at this change the installBundle method does this:
>
>            inputStream = createStream(url);
>            Bundle bundle = context.installBundle(location, inputStream);
>
> when I think it could just do this:
>
>            Bundle bundle = context.installBundle(location);
>
> By doing it the following way you could install a directory as a
> bundle if the framework is equinox.
>
> What do you think?
> Alasdair
>
> 2009/11/30  <aw...@apache.org>:
>> Author: awojtuniak
>> Date: Mon Nov 30 22:41:08 2009
>> New Revision: 885603
>>
>> URL: http://svn.apache.org/viewvc?rev=885603&view=rev
>> Log:
>> ARIES-30 Framework MBean test case
>>
>> Added:
>>    incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java   (with props)
>> Modified:
>>    incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>>
>> Modified: incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
>> ==============================================================================
>> --- incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java (original)
>> +++ incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java Mon Nov 30 22:41:08 2009
>> @@ -88,16 +88,27 @@
>>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String, java.lang.String)
>>      */
>>     public long installBundle(String location, String url) throws IOException {
>> -        InputStream inputStream;
>> +        InputStream inputStream = null;
>>         try {
>> -            inputStream = new URL(url).openStream();
>> +            inputStream = createStream(url);
>>             Bundle bundle = context.installBundle(location, inputStream);
>>             return bundle.getBundleId();
>>         } catch (BundleException e) {
>> +            if (inputStream != null) {
>> +                try {
>> +                    inputStream.close();
>> +                } catch (IOException ioe) {
>> +
>> +                }
>> +            }
>>             throw new IOException("Can't install bundle with location" + location);
>>         }
>>     }
>>
>> +    public InputStream createStream(String url) throws IOException {
>> +        return new URL(url).openStream();
>> +    }
>> +
>>     /**
>>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
>>      */
>> @@ -170,12 +181,7 @@
>>             try {
>>                 refreshPackages(bundleIdentifiers[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -241,12 +247,7 @@
>>             try {
>>                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -304,12 +305,7 @@
>>             try {
>>                 startBundle(bundleIdentifiers[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -340,12 +336,7 @@
>>             try {
>>                 stopBundle(bundleIdentifiers[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -355,7 +346,7 @@
>>      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
>>      */
>>     public void uninstallBundle(long bundleIdentifier) throws IOException {
>> -        Bundle bundle = context.getBundle(bundleIdentifier);
>> +        Bundle bundle = getBundle(bundleIdentifier);
>>         if (bundle != null) {
>>             try {
>>                 bundle.uninstall();
>> @@ -364,8 +355,6 @@
>>             }
>>         }
>>
>> -        throw new IllegalArgumentException("Can't find bundle with id " + bundleIdentifier);
>> -
>>     }
>>
>>     /**
>> @@ -379,12 +368,7 @@
>>             try {
>>                 uninstallBundle(bundleIdentifiers[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -408,10 +392,10 @@
>>      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long, java.lang.String)
>>      */
>>     public void updateBundle(long bundleIdentifier, String url) throws IOException {
>> -        Bundle bundle = getBundle(0);
>> +        Bundle bundle = getBundle(bundleIdentifier);
>>         InputStream inputStream = null;
>>         try {
>> -            inputStream = new URL(url).openStream();
>> +            inputStream = createStream(url);
>>             bundle.update(inputStream);
>>         } catch (BundleException be) {
>>             if (inputStream != null) {
>> @@ -437,12 +421,7 @@
>>             try {
>>                 updateBundle(bundleIdentifiers[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -463,12 +442,7 @@
>>             try {
>>                 updateBundle(bundleIdentifiers[i], urls[i]);
>>             } catch (Throwable t) {
>> -                long[] completed = new long[i];
>> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> -                long[] remaining = new long[completed.length - i - 1];
>> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
>> -                        .toCompositeData();
>> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>>             }
>>         }
>>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
>> @@ -488,6 +462,22 @@
>>     }
>>
>>     /**
>> +     * Create {@link BatchActionResult}, when the operation fail.
>> +     *
>> +     * @param bundleIdentifiers bundle ids for operation.
>> +     * @param i index of loop pointing on which operation fails.
>> +     * @param t Throwable thrown by failed operation.
>> +     * @return created BatchActionResult instance.
>> +     */
>> +    private CompositeData createFailedBatchActionResult(long[] bundleIdentifiers, int i, Throwable t) {
>> +        long[] completed = new long[i];
>> +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
>> +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
>> +        return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i]).toCompositeData();
>> +    }
>> +
>> +    /**
>>      * Gets bundle with provided bundleId.
>>      *
>>      * @param bundleIdentifier bundle id.
>>
>> Added: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
>> ==============================================================================
>> --- incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java (added)
>> +++ incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java Mon Nov 30 22:41:08 2009
>> @@ -0,0 +1,510 @@
>> +/**
>> + *  Licensed to the Apache Software Foundation (ASF) under one or more
>> + *  contributor license agreements.  See the NOTICE file distributed with
>> + *  this work for additional information regarding copyright ownership.
>> + *  The ASF licenses this file to You under the Apache License, Version 2.0
>> + *  (the "License"); you may not use this file except in compliance with
>> + *  the License.  You may obtain a copy of the License at
>> + *
>> + *     http://www.apache.org/licenses/LICENSE-2.0
>> + *
>> + *  Unless required by applicable law or agreed to in writing, software
>> + *  distributed under the License is distributed on an "AS IS" BASIS,
>> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> + *  See the License for the specific language governing permissions and
>> + *  limitations under the License.
>> + */
>> +package org.apache.aries.jmx.framework;
>> +
>> +import java.io.IOException;
>> +import java.io.InputStream;
>> +
>> +import javax.management.openmbean.CompositeData;
>> +
>> +import org.junit.Assert;
>> +
>> +import org.apache.aries.jmx.codec.BatchActionResult;
>> +import org.apache.aries.jmx.codec.BatchInstallResult;
>> +import org.junit.Before;
>> +import org.junit.Test;
>> +import org.mockito.Mock;
>> +import org.mockito.Mockito;
>> +import org.mockito.MockitoAnnotations;
>> +import org.osgi.framework.Bundle;
>> +import org.osgi.framework.BundleContext;
>> +import org.osgi.framework.BundleException;
>> +import org.osgi.jmx.framework.FrameworkMBean;
>> +import org.osgi.service.packageadmin.PackageAdmin;
>> +import org.osgi.service.startlevel.StartLevel;
>> +
>> +/**
>> + * {@link FrameworkMBean} test case.
>> + *
>> + *
>> + * @version $Rev$ $Date$
>> + */
>> +public class FrameworkTest {
>> +
>> +    @Mock
>> +    private StartLevel startLevel;
>> +    @Mock
>> +    private PackageAdmin admin;
>> +    @Mock
>> +    private BundleContext context;
>> +    private Framework mbean;
>> +
>> +    @Before
>> +    public void setUp() throws Exception {
>> +        MockitoAnnotations.initMocks(this);
>> +        mbean = new Framework(context, startLevel, admin);
>> +    }
>> +
>> +    @Test
>> +    public void testGetFrameworkStartLevel() throws IOException {
>> +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
>> +        int level = mbean.getFrameworkStartLevel();
>> +        Assert.assertEquals(1, level);
>> +    }
>> +
>> +    @Test
>> +    public void testGetInitialBundleStartLevel() throws IOException {
>> +        Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
>> +        int level = mbean.getInitialBundleStartLevel();
>> +        Mockito.verify(startLevel).getInitialBundleStartLevel();
>> +        Assert.assertEquals(2, level);
>> +    }
>> +
>> +    @Test
>> +    public void testInstallBundleString() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> +        long bundleId = mbean.installBundle("file:test.jar");
>> +        Assert.assertEquals(2, bundleId);
>> +        Mockito.reset(context);
>> +        Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new BundleException("location doesn't exist"));
>> +
>> +        try {
>> +            mbean.installBundle("file:test2.jar");
>> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
>> +        } catch (IOException e) {
>> +            // ok
>> +        }
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testInstallBundleStringString() throws Exception {
>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> +        InputStream stream = Mockito.mock(InputStream.class);
>> +        Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
>> +        long bundleId = spiedMBean.installBundle("file:test.jar", "test.jar");
>> +        Assert.assertEquals(2, bundleId);
>> +        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"));
>> +
>> +        try {
>> +            spiedMBean.installBundle("file:test2.jar", "test.jar");
>> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
>> +        } catch (IOException e) {
>> +            // ok
>> +        }
>> +    }
>> +
>> +    @Test
>> +    public void testInstallBundlesStringArray() 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(new Long(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"));
>> +
>> +        /*
>> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
>> +         * mbean.installBundles(locations); BatchInstallResult batch2 = BatchInstallResult.from(data2);
>> +         * Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
>> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> +         */
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testInstallBundlesStringArrayStringArray() throws Exception {
>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
>> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> +        InputStream stream = Mockito.mock(InputStream.class);
>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> +        CompositeData data = spiedMBean.installBundles(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"));
>> +
>> +        /*
>> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
>> +         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new String[]{"test.jar"}); BatchInstallResult batch2
>> +         * = BatchInstallResult.from(data2); Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
>> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
>> +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> +         */
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testRefreshPackagesLong() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> +
>> +        mbean.refreshPackages(1);
>> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>> +
>> +        try {
>> +            mbean.refreshPackages(2);
>> +            Assert.fail("Shouldn't happen illegal argument");
>> +        } catch (IllegalArgumentException iae) {
>> +            // expected
>> +        }
>> +    }
>> +
>> +    @Test
>> +    public void testRefreshPackagesLongArray() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> +
>> +        mbean.refreshPackages(new long[] { 1 });
>> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>> +
>> +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
>> +        // shouldn't found bundle whith id 2
>> +        Assert.assertNotNull(data);
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(0, batch.getCompleted().length);
>> +        Assert.assertFalse(batch.isSuccess());
>> +        Assert.assertNotNull(batch.getError());
>> +        Assert.assertNotNull(batch.getRemainingItems());
>> +        Assert.assertEquals(2, batch.getBundleInError());
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testResolveBundle() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> +
>> +        mbean.resolveBundle(1);
>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> +    }
>> +
>> +    @Test
>> +    public void testResolveBundles() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> +
>> +        mbean.resolveBundles(new long[] { 1 });
>> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> +    }
>> +
>> +    @Test
>> +    public void testRestartFramework() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> +        mbean.restartFramework();
>> +        Mockito.verify(bundle).update();
>> +    }
>> +
>> +    @Test
>> +    public void testSetBundleStartLevel() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> +        mbean.setBundleStartLevel(2, 1);
>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
>> +    }
>> +
>> +    @Test
>> +    public void testSetBundleStartLevels() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2 });
>> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(2, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2, 4 });
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertNull(batch2.getCompleted());
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testSetFrameworkStartLevel() throws IOException {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> +        mbean.setFrameworkStartLevel(1);
>> +        Mockito.verify(startLevel).setStartLevel(1);
>> +
>> +    }
>> +
>> +    @Test
>> +    public void testSetInitialBundleStartLevel() throws IOException {
>> +        mbean.setInitialBundleStartLevel(5);
>> +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
>> +    }
>> +
>> +    @Test
>> +    public void testShutdownFramework() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> +        mbean.shutdownFramework();
>> +        Mockito.verify(bundle).stop();
>> +    }
>> +
>> +    @Test
>> +    public void testStartBundle() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        mbean.startBundle(5);
>> +        Mockito.verify(bundle).start();
>> +
>> +        Mockito.reset(context);
>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> +        Mockito.doThrow(new BundleException("")).when(bundle).start();
>> +
>> +        try {
>> +            mbean.startBundle(6);
>> +            Assert.fail("Shouldn't go to this stage, BundleException was thrown");
>> +        } catch (IOException ioe) {
>> +            //expected
>> +        }
>> +    }
>> +
>> +    @Test
>> +    public void testStartBundles() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        CompositeData data = mbean.startBundles(new long[] { 5 });
>> +        Mockito.verify(bundle).start();
>> +
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = mbean.startBundles(null);
>> +
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertNull(batch2.getCompleted());
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +    }
>> +
>> +    @Test
>> +    public void testStopBundle() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        mbean.stopBundle(5);
>> +        Mockito.verify(bundle).stop();
>> +    }
>> +
>> +    @Test
>> +    public void testStopBundles() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        CompositeData data = mbean.stopBundles(new long[] { 5 });
>> +        Mockito.verify(bundle).stop();
>> +
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = mbean.stopBundles(null);
>> +
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertNull(batch2.getCompleted());
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +    }
>> +
>> +    @Test
>> +    public void testUninstallBundle() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        mbean.uninstallBundle(5);
>> +        Mockito.verify(bundle).uninstall();
>> +    }
>> +
>> +    @Test
>> +    public void testUninstallBundles() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
>> +        Mockito.verify(bundle).uninstall();
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = mbean.uninstallBundles(null);
>> +
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertNull(batch2.getCompleted());
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +    }
>> +
>> +    @Test
>> +    public void testUpdateBundleLong() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        mbean.updateBundle(5);
>> +        Mockito.verify(bundle).update();
>> +    }
>> +
>> +    @Test
>> +    public void testUpdateBundleLongString() throws Exception {
>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> +        InputStream stream = Mockito.mock(InputStream.class);
>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        spiedMBean.updateBundle(5, "file:test.jar");
>> +        Mockito.verify(bundle).update(stream);
>> +    }
>> +
>> +    @Test
>> +    public void testUpdateBundlesLongArray() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        CompositeData data = mbean.updateBundles(new long[] { 5 });
>> +        Mockito.verify(bundle).update();
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = mbean.updateBundles(null);
>> +
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertNull(batch2.getCompleted());
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +
>> +        Mockito.reset(bundle);
>> +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> +        Mockito.doThrow(new BundleException("")).when(bundle).update();
>> +        BatchActionResult batch3 = BatchActionResult.from(data3);
>> +        Assert.assertEquals(0, batch3.getCompleted().length);
>> +        Assert.assertFalse(batch3.isSuccess());
>> +        Assert.assertNotNull(batch3.getError());
>> +        Assert.assertEquals(6, batch3.getBundleInError());
>> +
>> +        Bundle bundle6 = Mockito.mock(Bundle.class);
>> +        Bundle bundle8 = Mockito.mock(Bundle.class);
>> +        Bundle bundle7 = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
>> +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
>> +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
>> +        Mockito.doThrow(new BundleException("")).when(bundle8).update();
>> +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7 });
>> +        BatchActionResult batch4 = BatchActionResult.from(data4);
>> +        Mockito.verify(bundle6).update();
>> +        Assert.assertEquals(1, batch4.getCompleted().length);
>> +        // should contain only bundleid 6
>> +        Assert.assertEquals(6, batch4.getCompleted()[0]);
>> +        Assert.assertFalse(batch4.isSuccess());
>> +        Assert.assertNotNull(batch4.getError());
>> +        Assert.assertEquals(8, batch4.getBundleInError());
>> +        Assert.assertEquals(1, batch4.getRemainingItems().length);
>> +        // should contain only bundleid 7
>> +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
>> +    }
>> +
>> +    @Test
>> +    public void testUpdateBundlesLongArrayStringArray() throws Exception {
>> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
>> +        Framework spiedMBean = Mockito.spy(partialMock);
>> +        InputStream stream = Mockito.mock(InputStream.class);
>> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> +        CompositeData data = spiedMBean.updateBundles(new long[] { 5 }, new String[] { "file:test.jar" });
>> +        Mockito.verify(bundle).update(stream);
>> +        BatchActionResult batch = BatchActionResult.from(data);
>> +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> +        Assert.assertTrue(batch.isSuccess());
>> +        Assert.assertNull(batch.getError());
>> +        Assert.assertNull(batch.getRemainingItems());
>> +
>> +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4 }, new String[] { "file:test.jar" });
>> +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> +        Assert.assertFalse(batch2.isSuccess());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNotNull(batch2.getError());
>> +        Assert.assertNull(batch2.getRemainingItems());
>> +    }
>> +
>> +    @Test
>> +    public void testUpdateFramework() throws Exception {
>> +        Bundle bundle = Mockito.mock(Bundle.class);
>> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> +        mbean.restartFramework();
>> +        Mockito.verify(bundle).update();
>> +    }
>> +
>> +    /**
>> +     * Mocking {@link Framework} method createStream.
>> +     */
>> +    private class FrameworkPartialMock extends Framework {
>> +
>> +        public FrameworkPartialMock(BundleContext context, StartLevel startLevel, PackageAdmin packageAdmin) {
>> +            super(context, startLevel, packageAdmin);
>> +        }
>> +
>> +        public InputStream createStream(String url) throws IOException {
>> +            return null;
>> +        }
>> +    }
>> +
>> +}
>>
>> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> ------------------------------------------------------------------------------
>>    svn:eol-style = native
>>
>> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> ------------------------------------------------------------------------------
>>    svn:keywords = Revision Date
>>
>>
>>
>
>
>
> --
> Alasdair Nottingham
> alasdair.nottingham@gmail.com
>

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by adam wojtuniak <ad...@gmail.com>.
See this. Is testing mbean.installBundle(location,url) for example.
I spied mbean to replace createStream method and return mocked stream
for testing purpose.
FrameworkMbean is calling context.installBundle(location,stream) inside
medthod installBundle(location,url).
see the snipet (from Framework mbean) below:

public long installBundle(String location, String url) throws IOException {
        InputStream inputStream = null;
        try {
            inputStream = createStream(url);
            Bundle bundle = context.installBundle(location, inputStream);
            return bundle.getBundleId();


@Test
    public void testInstallBundleStringString() throws Exception {
        // Framework partialMock = new FrameworkPartialMock(context,
startLevel, admin);
        Bundle bundle = Mockito.mock(Bundle.class);
        Mockito.when(context.installBundle(Mockito.anyString(),
Mockito.any(InputStream.class))).thenReturn(bundle);
        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
        Framework spiedMBean = Mockito.spy(mbean);
        InputStream stream = Mockito.mock(InputStream.class);
        Mockito.doReturn(stream).when(spiedMBean).createStream("test.jar");
        long bundleId = spiedMBean.installBundle("file:test.jar",
"test.jar");
        Assert.assertEquals(2, bundleId);
        Mockito.reset(context);

Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
        Mockito.when(context.installBundle(Mockito.anyString(),
Mockito.any(InputStream.class))).thenThrow(
                new BundleException("location doesn't exist"));

        try {
            spiedMBean.installBundle("file:test2.jar", "test.jar");
            Assert.fail("Shouldn't go to this stage, location doesn't
exist");
        } catch (IOException e) {
            // ok
        }
    }

Cheers,
Adam

On Tue, Dec 1, 2009 at 6:03 PM, Alasdair Nottingham <
alasdair.nottingham@gmail.com> wrote:

> I'm not sure I follow. What test does this?
>
> Alasdair
>
> 2009/12/1 adam wojtuniak <ad...@gmail.com>:
> > Yes but test is testing installBundle with location and inputstream.
> >
> > *FrameworkMBean.*installBundle(String location, String url),
> > So I have to test it with inputstream.
> > Cheers,
> > Adam
> > On Tue, Dec 1, 2009 at 4:18 PM, Alasdair Nottingham <
> > alasdair.nottingham@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> Looking at this change the installBundle method does this:
> >>
> >>            inputStream = createStream(url);
> >>            Bundle bundle = context.installBundle(location, inputStream);
> >>
> >> when I think it could just do this:
> >>
> >>            Bundle bundle = context.installBundle(location);
> >>
> >> By doing it the following way you could install a directory as a
> >> bundle if the framework is equinox.
> >>
> >> What do you think?
> >> Alasdair
> >>
> >> 2009/11/30  <aw...@apache.org>:
> >>  > Author: awojtuniak
> >> > Date: Mon Nov 30 22:41:08 2009
> >> > New Revision: 885603
> >> >
> >> > URL: http://svn.apache.org/viewvc?rev=885603&view=rev
> >> > Log:
> >> > ARIES-30 Framework MBean test case
> >> >
> >> > Added:
> >> >
> >>
>  incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >>   (with props)
> >> > Modified:
> >> >
> >>
>  incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >> >
> >> > Modified:
> >>
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >> > URL:
> >>
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
> >> >
> >>
> ==============================================================================
> >> > ---
> >>
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >> (original)
> >> > +++
> >>
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >> Mon Nov 30 22:41:08 2009
> >> > @@ -88,16 +88,27 @@
> >> >      * @see
> >> org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String,
> >> java.lang.String)
> >> >      */
> >> >     public long installBundle(String location, String url) throws
> >> IOException {
> >> > -        InputStream inputStream;
> >> > +        InputStream inputStream = null;
> >> >         try {
> >> > -            inputStream = new URL(url).openStream();
> >> > +            inputStream = createStream(url);
> >> >             Bundle bundle = context.installBundle(location,
> inputStream);
> >> >             return bundle.getBundleId();
> >> >         } catch (BundleException e) {
> >> > +            if (inputStream != null) {
> >> > +                try {
> >> > +                    inputStream.close();
> >> > +                } catch (IOException ioe) {
> >> > +
> >> > +                }
> >> > +            }
> >> >             throw new IOException("Can't install bundle with location"
> +
> >> location);
> >> >         }
> >> >     }
> >> >
> >> > +    public InputStream createStream(String url) throws IOException {
> >> > +        return new URL(url).openStream();
> >> > +    }
> >> > +
> >> >     /**
> >> >      * @see
> >> org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
> >> >      */
> >> > @@ -170,12 +181,7 @@
> >> >             try {
> >> >                 refreshPackages(bundleIdentifiers[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -241,12 +247,7 @@
> >> >             try {
> >> >                 setBundleStartLevel(bundleIdentifiers[i],
> newlevels[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -304,12 +305,7 @@
> >> >             try {
> >> >                 startBundle(bundleIdentifiers[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -340,12 +336,7 @@
> >> >             try {
> >> >                 stopBundle(bundleIdentifiers[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -355,7 +346,7 @@
> >> >      * @see
> org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
> >> >      */
> >> >     public void uninstallBundle(long bundleIdentifier) throws
> IOException
> >> {
> >> > -        Bundle bundle = context.getBundle(bundleIdentifier);
> >> > +        Bundle bundle = getBundle(bundleIdentifier);
> >> >         if (bundle != null) {
> >> >             try {
> >> >                 bundle.uninstall();
> >> > @@ -364,8 +355,6 @@
> >> >             }
> >> >         }
> >> >
> >> > -        throw new IllegalArgumentException("Can't find bundle with id
> "
> >> + bundleIdentifier);
> >> > -
> >> >     }
> >> >
> >> >     /**
> >> > @@ -379,12 +368,7 @@
> >> >             try {
> >> >                 uninstallBundle(bundleIdentifiers[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -408,10 +392,10 @@
> >> >      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long,
> >> java.lang.String)
> >> >      */
> >> >     public void updateBundle(long bundleIdentifier, String url) throws
> >> IOException {
> >> > -        Bundle bundle = getBundle(0);
> >> > +        Bundle bundle = getBundle(bundleIdentifier);
> >> >         InputStream inputStream = null;
> >> >         try {
> >> > -            inputStream = new URL(url).openStream();
> >> > +            inputStream = createStream(url);
> >> >             bundle.update(inputStream);
> >> >         } catch (BundleException be) {
> >> >             if (inputStream != null) {
> >> > @@ -437,12 +421,7 @@
> >> >             try {
> >> >                 updateBundle(bundleIdentifiers[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -463,12 +442,7 @@
> >> >             try {
> >> >                 updateBundle(bundleIdentifiers[i], urls[i]);
> >> >             } catch (Throwable t) {
> >> > -                long[] completed = new long[i];
> >> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0,
> i);
> >> > -                long[] remaining = new long[completed.length - i -
> 1];
> >> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining,
> 0,
> >> remaining.length);
> >> > -                return new BatchActionResult(completed, t.toString(),
> >> remaining, bundleIdentifiers[i])
> >> > -                        .toCompositeData();
> >> > +                return
> createFailedBatchActionResult(bundleIdentifiers,
> >> i, t);
> >> >             }
> >> >         }
> >> >         return new
> >> BatchActionResult(bundleIdentifiers).toCompositeData();
> >> > @@ -488,6 +462,22 @@
> >> >     }
> >> >
> >> >     /**
> >> > +     * Create {@link BatchActionResult}, when the operation fail.
> >> > +     *
> >> > +     * @param bundleIdentifiers bundle ids for operation.
> >> > +     * @param i index of loop pointing on which operation fails.
> >> > +     * @param t Throwable thrown by failed operation.
> >> > +     * @return created BatchActionResult instance.
> >> > +     */
> >> > +    private CompositeData createFailedBatchActionResult(long[]
> >> bundleIdentifiers, int i, Throwable t) {
> >> > +        long[] completed = new long[i];
> >> > +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> >> > +        long[] remaining = new long[bundleIdentifiers.length - i -
> 1];
> >> > +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> >> remaining.length);
> >> > +        return new BatchActionResult(completed, t.toString(),
> remaining,
> >> bundleIdentifiers[i]).toCompositeData();
> >> > +    }
> >> > +
> >> > +    /**
> >> >      * Gets bundle with provided bundleId.
> >> >      *
> >> >      * @param bundleIdentifier bundle id.
> >> >
> >> > Added:
> >>
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >> > URL:
> >>
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
> >> >
> >>
> ==============================================================================
> >> > ---
> >>
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >> (added)
> >> > +++
> >>
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >> Mon Nov 30 22:41:08 2009
> >> > @@ -0,0 +1,510 @@
> >> > +/**
> >> > + *  Licensed to the Apache Software Foundation (ASF) under one or
> more
> >> > + *  contributor license agreements.  See the NOTICE file distributed
> >> with
> >> > + *  this work for additional information regarding copyright
> ownership.
> >> > + *  The ASF licenses this file to You under the Apache License,
> Version
> >> 2.0
> >> > + *  (the "License"); you may not use this file except in compliance
> with
> >> > + *  the License.  You may obtain a copy of the License at
> >> > + *
> >> > + *     http://www.apache.org/licenses/LICENSE-2.0
> >> > + *
> >> > + *  Unless required by applicable law or agreed to in writing,
> software
> >> > + *  distributed under the License is distributed on an "AS IS" BASIS,
> >> > + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> >> implied.
> >> > + *  See the License for the specific language governing permissions
> and
> >> > + *  limitations under the License.
> >> > + */
> >> > +package org.apache.aries.jmx.framework;
> >> > +
> >> > +import java.io.IOException;
> >> > +import java.io.InputStream;
> >> > +
> >> > +import javax.management.openmbean.CompositeData;
> >> > +
> >> > +import org.junit.Assert;
> >> > +
> >> > +import org.apache.aries.jmx.codec.BatchActionResult;
> >> > +import org.apache.aries.jmx.codec.BatchInstallResult;
> >> > +import org.junit.Before;
> >> > +import org.junit.Test;
> >> > +import org.mockito.Mock;
> >> > +import org.mockito.Mockito;
> >> > +import org.mockito.MockitoAnnotations;
> >> > +import org.osgi.framework.Bundle;
> >> > +import org.osgi.framework.BundleContext;
> >> > +import org.osgi.framework.BundleException;
> >> > +import org.osgi.jmx.framework.FrameworkMBean;
> >> > +import org.osgi.service.packageadmin.PackageAdmin;
> >> > +import org.osgi.service.startlevel.StartLevel;
> >> > +
> >> > +/**
> >> > + * {@link FrameworkMBean} test case.
> >> > + *
> >> > + *
> >> > + * @version $Rev$ $Date$
> >> > + */
> >> > +public class FrameworkTest {
> >> > +
> >> > +    @Mock
> >> > +    private StartLevel startLevel;
> >> > +    @Mock
> >> > +    private PackageAdmin admin;
> >> > +    @Mock
> >> > +    private BundleContext context;
> >> > +    private Framework mbean;
> >> > +
> >> > +    @Before
> >> > +    public void setUp() throws Exception {
> >> > +        MockitoAnnotations.initMocks(this);
> >> > +        mbean = new Framework(context, startLevel, admin);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testGetFrameworkStartLevel() throws IOException {
> >> > +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
> >> > +        int level = mbean.getFrameworkStartLevel();
> >> > +        Assert.assertEquals(1, level);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testGetInitialBundleStartLevel() throws IOException {
> >> > +
> >>  Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
> >> > +        int level = mbean.getInitialBundleStartLevel();
> >> > +        Mockito.verify(startLevel).getInitialBundleStartLevel();
> >> > +        Assert.assertEquals(2, level);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testInstallBundleString() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +
> >>
>  Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
> >> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >> > +        long bundleId = mbean.installBundle("file:test.jar");
> >> > +        Assert.assertEquals(2, bundleId);
> >> > +        Mockito.reset(context);
> >> > +
> >>  Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new
> >> BundleException("location doesn't exist"));
> >> > +
> >> > +        try {
> >> > +            mbean.installBundle("file:test2.jar");
> >> > +            Assert.fail("Shouldn't go to this stage, location doesn't
> >> exist");
> >> > +        } catch (IOException e) {
> >> > +            // ok
> >> > +        }
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testInstallBundleStringString() throws Exception {
> >> > +        Framework partialMock = new FrameworkPartialMock(context,
> >> startLevel, admin);
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.installBundle(Mockito.anyString(),
> >> Mockito.any(InputStream.class))).thenReturn(bundle);
> >> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >> > +        Framework spiedMBean = Mockito.spy(partialMock);
> >> > +        InputStream stream = Mockito.mock(InputStream.class);
> >> > +
> >>  Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
> >> > +        long bundleId = spiedMBean.installBundle("file:test.jar",
> >> "test.jar");
> >> > +        Assert.assertEquals(2, bundleId);
> >> > +        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"));
> >> > +
> >> > +        try {
> >> > +            spiedMBean.installBundle("file:test2.jar", "test.jar");
> >> > +            Assert.fail("Shouldn't go to this stage, location doesn't
> >> exist");
> >> > +        } catch (IOException e) {
> >> > +            // ok
> >> > +        }
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testInstallBundlesStringArray() 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(new Long(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"));
> >> > +
> >> > +        /*
> >> > +         * this one is not passing bug in api bundleInError should be
> >> String CompositeData data2 =
> >> > +         * mbean.installBundles(locations); BatchInstallResult batch2
> =
> >> BatchInstallResult.from(data2);
> >> > +         * Assert.assertNotNull(batch2);
> >> Assert.assertNull(batch2.getCompleted());
> >> > +         * Assert.assertFalse(batch2.isSuccess());
> >> Assert.assertNotNull(batch2.getError());
> >> > +         * Assert.assertEquals("file:test.jar",
> >> batch2.getBundleInError());
> >> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
> >> > +         */
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testInstallBundlesStringArrayStringArray() throws
> >> Exception {
> >> > +        Framework partialMock = new FrameworkPartialMock(context,
> >> startLevel, admin);
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.installBundle(Mockito.anyString(),
> >> Mockito.any(InputStream.class))).thenReturn(bundle);
> >> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> >> > +        Framework spiedMBean = Mockito.spy(partialMock);
> >> > +        InputStream stream = Mockito.mock(InputStream.class);
> >> > +
> >>
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >> > +        CompositeData data = spiedMBean.installBundles(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"));
> >> > +
> >> > +        /*
> >> > +         * this one is not passing bug in api bundleInError should be
> >> String CompositeData data2 =
> >> > +         * spiedMBean.installBundles(new String[]{"file:test.jar"},
> new
> >> String[]{"test.jar"}); BatchInstallResult batch2
> >> > +         * = BatchInstallResult.from(data2);
> >> Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
> >> > +         * Assert.assertFalse(batch2.isSuccess());
> >> Assert.assertNotNull(batch2.getError());
> >> > +         * Assert.assertEquals("file:test.jar",
> >> batch2.getBundleInError());
> >> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
> >> > +         */
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testRefreshPackagesLong() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >> > +
> >> > +        mbean.refreshPackages(1);
> >> > +        Mockito.verify(admin).refreshPackages((Bundle[])
> Mockito.any());
> >> > +
> >> > +        try {
> >> > +            mbean.refreshPackages(2);
> >> > +            Assert.fail("Shouldn't happen illegal argument");
> >> > +        } catch (IllegalArgumentException iae) {
> >> > +            // expected
> >> > +        }
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testRefreshPackagesLongArray() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >> > +
> >> > +        mbean.refreshPackages(new long[] { 1 });
> >> > +        Mockito.verify(admin).refreshPackages((Bundle[])
> Mockito.any());
> >> > +
> >> > +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
> >> > +        // shouldn't found bundle whith id 2
> >> > +        Assert.assertNotNull(data);
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(0, batch.getCompleted().length);
> >> > +        Assert.assertFalse(batch.isSuccess());
> >> > +        Assert.assertNotNull(batch.getError());
> >> > +        Assert.assertNotNull(batch.getRemainingItems());
> >> > +        Assert.assertEquals(2, batch.getBundleInError());
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testResolveBundle() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >> > +
> >> > +        mbean.resolveBundle(1);
> >> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle
> });
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testResolveBundles() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> >> > +
> >> > +        mbean.resolveBundles(new long[] { 1 });
> >> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle
> });
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testRestartFramework() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >> > +        mbean.restartFramework();
> >> > +        Mockito.verify(bundle).update();
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testSetBundleStartLevel() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> >> > +        mbean.setBundleStartLevel(2, 1);
> >> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testSetBundleStartLevels() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> >> > +        CompositeData data = mbean.setBundleStartLevels(new long[] {
> 2
> >> }, new int[] { 2 });
> >> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(2, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = mbean.setBundleStartLevels(new long[] {
> 2
> >> }, new int[] { 2, 4 });
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertNull(batch2.getCompleted());
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testSetFrameworkStartLevel() throws IOException {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >> > +        mbean.setFrameworkStartLevel(1);
> >> > +        Mockito.verify(startLevel).setStartLevel(1);
> >> > +
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testSetInitialBundleStartLevel() throws IOException {
> >> > +        mbean.setInitialBundleStartLevel(5);
> >> > +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testShutdownFramework() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >> > +        mbean.shutdownFramework();
> >> > +        Mockito.verify(bundle).stop();
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testStartBundle() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        mbean.startBundle(5);
> >> > +        Mockito.verify(bundle).start();
> >> > +
> >> > +        Mockito.reset(context);
> >> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> >> > +        Mockito.doThrow(new
> BundleException("")).when(bundle).start();
> >> > +
> >> > +        try {
> >> > +            mbean.startBundle(6);
> >> > +            Assert.fail("Shouldn't go to this stage, BundleException
> was
> >> thrown");
> >> > +        } catch (IOException ioe) {
> >> > +            //expected
> >> > +        }
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testStartBundles() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        CompositeData data = mbean.startBundles(new long[] { 5 });
> >> > +        Mockito.verify(bundle).start();
> >> > +
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = mbean.startBundles(null);
> >> > +
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertNull(batch2.getCompleted());
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testStopBundle() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        mbean.stopBundle(5);
> >> > +        Mockito.verify(bundle).stop();
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testStopBundles() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        CompositeData data = mbean.stopBundles(new long[] { 5 });
> >> > +        Mockito.verify(bundle).stop();
> >> > +
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = mbean.stopBundles(null);
> >> > +
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertNull(batch2.getCompleted());
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUninstallBundle() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        mbean.uninstallBundle(5);
> >> > +        Mockito.verify(bundle).uninstall();
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUninstallBundles() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        CompositeData data = mbean.uninstallBundles(new long[] { 5
> });
> >> > +        Mockito.verify(bundle).uninstall();
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = mbean.uninstallBundles(null);
> >> > +
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertNull(batch2.getCompleted());
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUpdateBundleLong() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        mbean.updateBundle(5);
> >> > +        Mockito.verify(bundle).update();
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUpdateBundleLongString() throws Exception {
> >> > +        Framework partialMock = new FrameworkPartialMock(context,
> >> startLevel, admin);
> >> > +        Framework spiedMBean = Mockito.spy(partialMock);
> >> > +        InputStream stream = Mockito.mock(InputStream.class);
> >> > +
> >>
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        spiedMBean.updateBundle(5, "file:test.jar");
> >> > +        Mockito.verify(bundle).update(stream);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUpdateBundlesLongArray() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        CompositeData data = mbean.updateBundles(new long[] { 5 });
> >> > +        Mockito.verify(bundle).update();
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = mbean.updateBundles(null);
> >> > +
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertNull(batch2.getCompleted());
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +
> >> > +        Mockito.reset(bundle);
> >> > +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
> >> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> >> > +        Mockito.doThrow(new
> BundleException("")).when(bundle).update();
> >> > +        BatchActionResult batch3 = BatchActionResult.from(data3);
> >> > +        Assert.assertEquals(0, batch3.getCompleted().length);
> >> > +        Assert.assertFalse(batch3.isSuccess());
> >> > +        Assert.assertNotNull(batch3.getError());
> >> > +        Assert.assertEquals(6, batch3.getBundleInError());
> >> > +
> >> > +        Bundle bundle6 = Mockito.mock(Bundle.class);
> >> > +        Bundle bundle8 = Mockito.mock(Bundle.class);
> >> > +        Bundle bundle7 = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
> >> > +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
> >> > +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
> >> > +        Mockito.doThrow(new
> BundleException("")).when(bundle8).update();
> >> > +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8,
> 7
> >> });
> >> > +        BatchActionResult batch4 = BatchActionResult.from(data4);
> >> > +        Mockito.verify(bundle6).update();
> >> > +        Assert.assertEquals(1, batch4.getCompleted().length);
> >> > +        // should contain only bundleid 6
> >> > +        Assert.assertEquals(6, batch4.getCompleted()[0]);
> >> > +        Assert.assertFalse(batch4.isSuccess());
> >> > +        Assert.assertNotNull(batch4.getError());
> >> > +        Assert.assertEquals(8, batch4.getBundleInError());
> >> > +        Assert.assertEquals(1, batch4.getRemainingItems().length);
> >> > +        // should contain only bundleid 7
> >> > +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUpdateBundlesLongArrayStringArray() throws
> Exception
> >> {
> >> > +        Framework partialMock = new FrameworkPartialMock(context,
> >> startLevel, admin);
> >> > +        Framework spiedMBean = Mockito.spy(partialMock);
> >> > +        InputStream stream = Mockito.mock(InputStream.class);
> >> > +
> >>
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> >> > +        CompositeData data = spiedMBean.updateBundles(new long[] { 5
> },
> >> new String[] { "file:test.jar" });
> >> > +        Mockito.verify(bundle).update(stream);
> >> > +        BatchActionResult batch = BatchActionResult.from(data);
> >> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> >> > +        Assert.assertTrue(batch.isSuccess());
> >> > +        Assert.assertNull(batch.getError());
> >> > +        Assert.assertNull(batch.getRemainingItems());
> >> > +
> >> > +        CompositeData data2 = spiedMBean.updateBundles(new long[] {
> 2, 4
> >> }, new String[] { "file:test.jar" });
> >> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> >> > +        Assert.assertFalse(batch2.isSuccess());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNotNull(batch2.getError());
> >> > +        Assert.assertNull(batch2.getRemainingItems());
> >> > +    }
> >> > +
> >> > +    @Test
> >> > +    public void testUpdateFramework() throws Exception {
> >> > +        Bundle bundle = Mockito.mock(Bundle.class);
> >> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> >> > +        mbean.restartFramework();
> >> > +        Mockito.verify(bundle).update();
> >> > +    }
> >> > +
> >> > +    /**
> >> > +     * Mocking {@link Framework} method createStream.
> >> > +     */
> >> > +    private class FrameworkPartialMock extends Framework {
> >> > +
> >> > +        public FrameworkPartialMock(BundleContext context, StartLevel
> >> startLevel, PackageAdmin packageAdmin) {
> >> > +            super(context, startLevel, packageAdmin);
> >> > +        }
> >> > +
> >> > +        public InputStream createStream(String url) throws
> IOException {
> >> > +            return null;
> >> > +        }
> >> > +    }
> >> > +
> >> > +}
> >> >
> >> > Propchange:
> >>
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >> >
> >>
> ------------------------------------------------------------------------------
> >> >    svn:eol-style = native
> >> >
> >> > Propchange:
> >>
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >> >
> >>
> ------------------------------------------------------------------------------
> >> >    svn:keywords = Revision Date
> >> >
> >> >
> >> >
> >>
> >>
> >>
> >> --
> >> Alasdair Nottingham
> >> alasdair.nottingham@gmail.com
> >>
> >
>
>
>
> --
> Alasdair Nottingham
> alasdair.nottingham@gmail.com
>

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by Alasdair Nottingham <al...@gmail.com>.
I'm not sure I follow. What test does this?

Alasdair

2009/12/1 adam wojtuniak <ad...@gmail.com>:
> Yes but test is testing installBundle with location and inputstream.
>
> *FrameworkMBean.*installBundle(String location, String url),
> So I have to test it with inputstream.
> Cheers,
> Adam
> On Tue, Dec 1, 2009 at 4:18 PM, Alasdair Nottingham <
> alasdair.nottingham@gmail.com> wrote:
>
>> Hi,
>>
>> Looking at this change the installBundle method does this:
>>
>>            inputStream = createStream(url);
>>            Bundle bundle = context.installBundle(location, inputStream);
>>
>> when I think it could just do this:
>>
>>            Bundle bundle = context.installBundle(location);
>>
>> By doing it the following way you could install a directory as a
>> bundle if the framework is equinox.
>>
>> What do you think?
>> Alasdair
>>
>> 2009/11/30  <aw...@apache.org>:
>>  > Author: awojtuniak
>> > Date: Mon Nov 30 22:41:08 2009
>> > New Revision: 885603
>> >
>> > URL: http://svn.apache.org/viewvc?rev=885603&view=rev
>> > Log:
>> > ARIES-30 Framework MBean test case
>> >
>> > Added:
>> >
>>  incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>>   (with props)
>> > Modified:
>> >
>>  incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> >
>> > Modified:
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> > URL:
>> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
>> >
>> ==============================================================================
>> > ---
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> (original)
>> > +++
>> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>> Mon Nov 30 22:41:08 2009
>> > @@ -88,16 +88,27 @@
>> >      * @see
>> org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String,
>> java.lang.String)
>> >      */
>> >     public long installBundle(String location, String url) throws
>> IOException {
>> > -        InputStream inputStream;
>> > +        InputStream inputStream = null;
>> >         try {
>> > -            inputStream = new URL(url).openStream();
>> > +            inputStream = createStream(url);
>> >             Bundle bundle = context.installBundle(location, inputStream);
>> >             return bundle.getBundleId();
>> >         } catch (BundleException e) {
>> > +            if (inputStream != null) {
>> > +                try {
>> > +                    inputStream.close();
>> > +                } catch (IOException ioe) {
>> > +
>> > +                }
>> > +            }
>> >             throw new IOException("Can't install bundle with location" +
>> location);
>> >         }
>> >     }
>> >
>> > +    public InputStream createStream(String url) throws IOException {
>> > +        return new URL(url).openStream();
>> > +    }
>> > +
>> >     /**
>> >      * @see
>> org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
>> >      */
>> > @@ -170,12 +181,7 @@
>> >             try {
>> >                 refreshPackages(bundleIdentifiers[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -241,12 +247,7 @@
>> >             try {
>> >                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -304,12 +305,7 @@
>> >             try {
>> >                 startBundle(bundleIdentifiers[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -340,12 +336,7 @@
>> >             try {
>> >                 stopBundle(bundleIdentifiers[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -355,7 +346,7 @@
>> >      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
>> >      */
>> >     public void uninstallBundle(long bundleIdentifier) throws IOException
>> {
>> > -        Bundle bundle = context.getBundle(bundleIdentifier);
>> > +        Bundle bundle = getBundle(bundleIdentifier);
>> >         if (bundle != null) {
>> >             try {
>> >                 bundle.uninstall();
>> > @@ -364,8 +355,6 @@
>> >             }
>> >         }
>> >
>> > -        throw new IllegalArgumentException("Can't find bundle with id "
>> + bundleIdentifier);
>> > -
>> >     }
>> >
>> >     /**
>> > @@ -379,12 +368,7 @@
>> >             try {
>> >                 uninstallBundle(bundleIdentifiers[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -408,10 +392,10 @@
>> >      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long,
>> java.lang.String)
>> >      */
>> >     public void updateBundle(long bundleIdentifier, String url) throws
>> IOException {
>> > -        Bundle bundle = getBundle(0);
>> > +        Bundle bundle = getBundle(bundleIdentifier);
>> >         InputStream inputStream = null;
>> >         try {
>> > -            inputStream = new URL(url).openStream();
>> > +            inputStream = createStream(url);
>> >             bundle.update(inputStream);
>> >         } catch (BundleException be) {
>> >             if (inputStream != null) {
>> > @@ -437,12 +421,7 @@
>> >             try {
>> >                 updateBundle(bundleIdentifiers[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -463,12 +442,7 @@
>> >             try {
>> >                 updateBundle(bundleIdentifiers[i], urls[i]);
>> >             } catch (Throwable t) {
>> > -                long[] completed = new long[i];
>> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > -                long[] remaining = new long[completed.length - i - 1];
>> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > -                return new BatchActionResult(completed, t.toString(),
>> remaining, bundleIdentifiers[i])
>> > -                        .toCompositeData();
>> > +                return createFailedBatchActionResult(bundleIdentifiers,
>> i, t);
>> >             }
>> >         }
>> >         return new
>> BatchActionResult(bundleIdentifiers).toCompositeData();
>> > @@ -488,6 +462,22 @@
>> >     }
>> >
>> >     /**
>> > +     * Create {@link BatchActionResult}, when the operation fail.
>> > +     *
>> > +     * @param bundleIdentifiers bundle ids for operation.
>> > +     * @param i index of loop pointing on which operation fails.
>> > +     * @param t Throwable thrown by failed operation.
>> > +     * @return created BatchActionResult instance.
>> > +     */
>> > +    private CompositeData createFailedBatchActionResult(long[]
>> bundleIdentifiers, int i, Throwable t) {
>> > +        long[] completed = new long[i];
>> > +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
>> > +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
>> > +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
>> remaining.length);
>> > +        return new BatchActionResult(completed, t.toString(), remaining,
>> bundleIdentifiers[i]).toCompositeData();
>> > +    }
>> > +
>> > +    /**
>> >      * Gets bundle with provided bundleId.
>> >      *
>> >      * @param bundleIdentifier bundle id.
>> >
>> > Added:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> > URL:
>> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
>> >
>> ==============================================================================
>> > ---
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> (added)
>> > +++
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> Mon Nov 30 22:41:08 2009
>> > @@ -0,0 +1,510 @@
>> > +/**
>> > + *  Licensed to the Apache Software Foundation (ASF) under one or more
>> > + *  contributor license agreements.  See the NOTICE file distributed
>> with
>> > + *  this work for additional information regarding copyright ownership.
>> > + *  The ASF licenses this file to You under the Apache License, Version
>> 2.0
>> > + *  (the "License"); you may not use this file except in compliance with
>> > + *  the License.  You may obtain a copy of the License at
>> > + *
>> > + *     http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + *  Unless required by applicable law or agreed to in writing, software
>> > + *  distributed under the License is distributed on an "AS IS" BASIS,
>> > + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + *  See the License for the specific language governing permissions and
>> > + *  limitations under the License.
>> > + */
>> > +package org.apache.aries.jmx.framework;
>> > +
>> > +import java.io.IOException;
>> > +import java.io.InputStream;
>> > +
>> > +import javax.management.openmbean.CompositeData;
>> > +
>> > +import org.junit.Assert;
>> > +
>> > +import org.apache.aries.jmx.codec.BatchActionResult;
>> > +import org.apache.aries.jmx.codec.BatchInstallResult;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +import org.mockito.Mock;
>> > +import org.mockito.Mockito;
>> > +import org.mockito.MockitoAnnotations;
>> > +import org.osgi.framework.Bundle;
>> > +import org.osgi.framework.BundleContext;
>> > +import org.osgi.framework.BundleException;
>> > +import org.osgi.jmx.framework.FrameworkMBean;
>> > +import org.osgi.service.packageadmin.PackageAdmin;
>> > +import org.osgi.service.startlevel.StartLevel;
>> > +
>> > +/**
>> > + * {@link FrameworkMBean} test case.
>> > + *
>> > + *
>> > + * @version $Rev$ $Date$
>> > + */
>> > +public class FrameworkTest {
>> > +
>> > +    @Mock
>> > +    private StartLevel startLevel;
>> > +    @Mock
>> > +    private PackageAdmin admin;
>> > +    @Mock
>> > +    private BundleContext context;
>> > +    private Framework mbean;
>> > +
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        MockitoAnnotations.initMocks(this);
>> > +        mbean = new Framework(context, startLevel, admin);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testGetFrameworkStartLevel() throws IOException {
>> > +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
>> > +        int level = mbean.getFrameworkStartLevel();
>> > +        Assert.assertEquals(1, level);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testGetInitialBundleStartLevel() throws IOException {
>> > +
>>  Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
>> > +        int level = mbean.getInitialBundleStartLevel();
>> > +        Mockito.verify(startLevel).getInitialBundleStartLevel();
>> > +        Assert.assertEquals(2, level);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInstallBundleString() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +
>>  Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
>> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> > +        long bundleId = mbean.installBundle("file:test.jar");
>> > +        Assert.assertEquals(2, bundleId);
>> > +        Mockito.reset(context);
>> > +
>>  Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new
>> BundleException("location doesn't exist"));
>> > +
>> > +        try {
>> > +            mbean.installBundle("file:test2.jar");
>> > +            Assert.fail("Shouldn't go to this stage, location doesn't
>> exist");
>> > +        } catch (IOException e) {
>> > +            // ok
>> > +        }
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInstallBundleStringString() throws Exception {
>> > +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.installBundle(Mockito.anyString(),
>> Mockito.any(InputStream.class))).thenReturn(bundle);
>> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> > +        Framework spiedMBean = Mockito.spy(partialMock);
>> > +        InputStream stream = Mockito.mock(InputStream.class);
>> > +
>>  Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
>> > +        long bundleId = spiedMBean.installBundle("file:test.jar",
>> "test.jar");
>> > +        Assert.assertEquals(2, bundleId);
>> > +        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"));
>> > +
>> > +        try {
>> > +            spiedMBean.installBundle("file:test2.jar", "test.jar");
>> > +            Assert.fail("Shouldn't go to this stage, location doesn't
>> exist");
>> > +        } catch (IOException e) {
>> > +            // ok
>> > +        }
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInstallBundlesStringArray() 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(new Long(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"));
>> > +
>> > +        /*
>> > +         * this one is not passing bug in api bundleInError should be
>> String CompositeData data2 =
>> > +         * mbean.installBundles(locations); BatchInstallResult batch2 =
>> BatchInstallResult.from(data2);
>> > +         * Assert.assertNotNull(batch2);
>> Assert.assertNull(batch2.getCompleted());
>> > +         * Assert.assertFalse(batch2.isSuccess());
>> Assert.assertNotNull(batch2.getError());
>> > +         * Assert.assertEquals("file:test.jar",
>> batch2.getBundleInError());
>> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> > +         */
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInstallBundlesStringArrayStringArray() throws
>> Exception {
>> > +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.installBundle(Mockito.anyString(),
>> Mockito.any(InputStream.class))).thenReturn(bundle);
>> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
>> > +        Framework spiedMBean = Mockito.spy(partialMock);
>> > +        InputStream stream = Mockito.mock(InputStream.class);
>> > +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> > +        CompositeData data = spiedMBean.installBundles(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"));
>> > +
>> > +        /*
>> > +         * this one is not passing bug in api bundleInError should be
>> String CompositeData data2 =
>> > +         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new
>> String[]{"test.jar"}); BatchInstallResult batch2
>> > +         * = BatchInstallResult.from(data2);
>> Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
>> > +         * Assert.assertFalse(batch2.isSuccess());
>> Assert.assertNotNull(batch2.getError());
>> > +         * Assert.assertEquals("file:test.jar",
>> batch2.getBundleInError());
>> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
>> > +         */
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testRefreshPackagesLong() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> > +
>> > +        mbean.refreshPackages(1);
>> > +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>> > +
>> > +        try {
>> > +            mbean.refreshPackages(2);
>> > +            Assert.fail("Shouldn't happen illegal argument");
>> > +        } catch (IllegalArgumentException iae) {
>> > +            // expected
>> > +        }
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testRefreshPackagesLongArray() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> > +
>> > +        mbean.refreshPackages(new long[] { 1 });
>> > +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
>> > +
>> > +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
>> > +        // shouldn't found bundle whith id 2
>> > +        Assert.assertNotNull(data);
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(0, batch.getCompleted().length);
>> > +        Assert.assertFalse(batch.isSuccess());
>> > +        Assert.assertNotNull(batch.getError());
>> > +        Assert.assertNotNull(batch.getRemainingItems());
>> > +        Assert.assertEquals(2, batch.getBundleInError());
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testResolveBundle() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> > +
>> > +        mbean.resolveBundle(1);
>> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testResolveBundles() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
>> > +
>> > +        mbean.resolveBundles(new long[] { 1 });
>> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testRestartFramework() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> > +        mbean.restartFramework();
>> > +        Mockito.verify(bundle).update();
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testSetBundleStartLevel() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> > +        mbean.setBundleStartLevel(2, 1);
>> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testSetBundleStartLevels() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
>> > +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2
>> }, new int[] { 2 });
>> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(2, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2
>> }, new int[] { 2, 4 });
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertNull(batch2.getCompleted());
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testSetFrameworkStartLevel() throws IOException {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> > +        mbean.setFrameworkStartLevel(1);
>> > +        Mockito.verify(startLevel).setStartLevel(1);
>> > +
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testSetInitialBundleStartLevel() throws IOException {
>> > +        mbean.setInitialBundleStartLevel(5);
>> > +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testShutdownFramework() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> > +        mbean.shutdownFramework();
>> > +        Mockito.verify(bundle).stop();
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testStartBundle() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        mbean.startBundle(5);
>> > +        Mockito.verify(bundle).start();
>> > +
>> > +        Mockito.reset(context);
>> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> > +        Mockito.doThrow(new BundleException("")).when(bundle).start();
>> > +
>> > +        try {
>> > +            mbean.startBundle(6);
>> > +            Assert.fail("Shouldn't go to this stage, BundleException was
>> thrown");
>> > +        } catch (IOException ioe) {
>> > +            //expected
>> > +        }
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testStartBundles() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        CompositeData data = mbean.startBundles(new long[] { 5 });
>> > +        Mockito.verify(bundle).start();
>> > +
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = mbean.startBundles(null);
>> > +
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertNull(batch2.getCompleted());
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testStopBundle() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        mbean.stopBundle(5);
>> > +        Mockito.verify(bundle).stop();
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testStopBundles() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        CompositeData data = mbean.stopBundles(new long[] { 5 });
>> > +        Mockito.verify(bundle).stop();
>> > +
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = mbean.stopBundles(null);
>> > +
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertNull(batch2.getCompleted());
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUninstallBundle() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        mbean.uninstallBundle(5);
>> > +        Mockito.verify(bundle).uninstall();
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUninstallBundles() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
>> > +        Mockito.verify(bundle).uninstall();
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = mbean.uninstallBundles(null);
>> > +
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertNull(batch2.getCompleted());
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUpdateBundleLong() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        mbean.updateBundle(5);
>> > +        Mockito.verify(bundle).update();
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUpdateBundleLongString() throws Exception {
>> > +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> > +        Framework spiedMBean = Mockito.spy(partialMock);
>> > +        InputStream stream = Mockito.mock(InputStream.class);
>> > +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        spiedMBean.updateBundle(5, "file:test.jar");
>> > +        Mockito.verify(bundle).update(stream);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUpdateBundlesLongArray() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        CompositeData data = mbean.updateBundles(new long[] { 5 });
>> > +        Mockito.verify(bundle).update();
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = mbean.updateBundles(null);
>> > +
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertNull(batch2.getCompleted());
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +
>> > +        Mockito.reset(bundle);
>> > +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
>> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
>> > +        Mockito.doThrow(new BundleException("")).when(bundle).update();
>> > +        BatchActionResult batch3 = BatchActionResult.from(data3);
>> > +        Assert.assertEquals(0, batch3.getCompleted().length);
>> > +        Assert.assertFalse(batch3.isSuccess());
>> > +        Assert.assertNotNull(batch3.getError());
>> > +        Assert.assertEquals(6, batch3.getBundleInError());
>> > +
>> > +        Bundle bundle6 = Mockito.mock(Bundle.class);
>> > +        Bundle bundle8 = Mockito.mock(Bundle.class);
>> > +        Bundle bundle7 = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
>> > +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
>> > +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
>> > +        Mockito.doThrow(new BundleException("")).when(bundle8).update();
>> > +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7
>> });
>> > +        BatchActionResult batch4 = BatchActionResult.from(data4);
>> > +        Mockito.verify(bundle6).update();
>> > +        Assert.assertEquals(1, batch4.getCompleted().length);
>> > +        // should contain only bundleid 6
>> > +        Assert.assertEquals(6, batch4.getCompleted()[0]);
>> > +        Assert.assertFalse(batch4.isSuccess());
>> > +        Assert.assertNotNull(batch4.getError());
>> > +        Assert.assertEquals(8, batch4.getBundleInError());
>> > +        Assert.assertEquals(1, batch4.getRemainingItems().length);
>> > +        // should contain only bundleid 7
>> > +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUpdateBundlesLongArrayStringArray() throws Exception
>> {
>> > +        Framework partialMock = new FrameworkPartialMock(context,
>> startLevel, admin);
>> > +        Framework spiedMBean = Mockito.spy(partialMock);
>> > +        InputStream stream = Mockito.mock(InputStream.class);
>> > +
>>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
>> > +        CompositeData data = spiedMBean.updateBundles(new long[] { 5 },
>> new String[] { "file:test.jar" });
>> > +        Mockito.verify(bundle).update(stream);
>> > +        BatchActionResult batch = BatchActionResult.from(data);
>> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
>> > +        Assert.assertTrue(batch.isSuccess());
>> > +        Assert.assertNull(batch.getError());
>> > +        Assert.assertNull(batch.getRemainingItems());
>> > +
>> > +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4
>> }, new String[] { "file:test.jar" });
>> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
>> > +        Assert.assertFalse(batch2.isSuccess());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNotNull(batch2.getError());
>> > +        Assert.assertNull(batch2.getRemainingItems());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testUpdateFramework() throws Exception {
>> > +        Bundle bundle = Mockito.mock(Bundle.class);
>> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
>> > +        mbean.restartFramework();
>> > +        Mockito.verify(bundle).update();
>> > +    }
>> > +
>> > +    /**
>> > +     * Mocking {@link Framework} method createStream.
>> > +     */
>> > +    private class FrameworkPartialMock extends Framework {
>> > +
>> > +        public FrameworkPartialMock(BundleContext context, StartLevel
>> startLevel, PackageAdmin packageAdmin) {
>> > +            super(context, startLevel, packageAdmin);
>> > +        }
>> > +
>> > +        public InputStream createStream(String url) throws IOException {
>> > +            return null;
>> > +        }
>> > +    }
>> > +
>> > +}
>> >
>> > Propchange:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> >
>> ------------------------------------------------------------------------------
>> >    svn:eol-style = native
>> >
>> > Propchange:
>> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>> >
>> ------------------------------------------------------------------------------
>> >    svn:keywords = Revision Date
>> >
>> >
>> >
>>
>>
>>
>> --
>> Alasdair Nottingham
>> alasdair.nottingham@gmail.com
>>
>



-- 
Alasdair Nottingham
alasdair.nottingham@gmail.com

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by adam wojtuniak <ad...@gmail.com>.
Yes but test is testing installBundle with location and inputstream.

*FrameworkMBean.*installBundle(String location, String url),
So I have to test it with inputstream.
Cheers,
Adam
On Tue, Dec 1, 2009 at 4:18 PM, Alasdair Nottingham <
alasdair.nottingham@gmail.com> wrote:

> Hi,
>
> Looking at this change the installBundle method does this:
>
>            inputStream = createStream(url);
>            Bundle bundle = context.installBundle(location, inputStream);
>
> when I think it could just do this:
>
>            Bundle bundle = context.installBundle(location);
>
> By doing it the following way you could install a directory as a
> bundle if the framework is equinox.
>
> What do you think?
> Alasdair
>
> 2009/11/30  <aw...@apache.org>:
>  > Author: awojtuniak
> > Date: Mon Nov 30 22:41:08 2009
> > New Revision: 885603
> >
> > URL: http://svn.apache.org/viewvc?rev=885603&view=rev
> > Log:
> > ARIES-30 Framework MBean test case
> >
> > Added:
> >
>  incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
>   (with props)
> > Modified:
> >
>  incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> >
> > Modified:
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> > URL:
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
> >
> ==============================================================================
> > ---
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> (original)
> > +++
> incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> Mon Nov 30 22:41:08 2009
> > @@ -88,16 +88,27 @@
> >      * @see
> org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String,
> java.lang.String)
> >      */
> >     public long installBundle(String location, String url) throws
> IOException {
> > -        InputStream inputStream;
> > +        InputStream inputStream = null;
> >         try {
> > -            inputStream = new URL(url).openStream();
> > +            inputStream = createStream(url);
> >             Bundle bundle = context.installBundle(location, inputStream);
> >             return bundle.getBundleId();
> >         } catch (BundleException e) {
> > +            if (inputStream != null) {
> > +                try {
> > +                    inputStream.close();
> > +                } catch (IOException ioe) {
> > +
> > +                }
> > +            }
> >             throw new IOException("Can't install bundle with location" +
> location);
> >         }
> >     }
> >
> > +    public InputStream createStream(String url) throws IOException {
> > +        return new URL(url).openStream();
> > +    }
> > +
> >     /**
> >      * @see
> org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
> >      */
> > @@ -170,12 +181,7 @@
> >             try {
> >                 refreshPackages(bundleIdentifiers[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -241,12 +247,7 @@
> >             try {
> >                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -304,12 +305,7 @@
> >             try {
> >                 startBundle(bundleIdentifiers[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -340,12 +336,7 @@
> >             try {
> >                 stopBundle(bundleIdentifiers[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -355,7 +346,7 @@
> >      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
> >      */
> >     public void uninstallBundle(long bundleIdentifier) throws IOException
> {
> > -        Bundle bundle = context.getBundle(bundleIdentifier);
> > +        Bundle bundle = getBundle(bundleIdentifier);
> >         if (bundle != null) {
> >             try {
> >                 bundle.uninstall();
> > @@ -364,8 +355,6 @@
> >             }
> >         }
> >
> > -        throw new IllegalArgumentException("Can't find bundle with id "
> + bundleIdentifier);
> > -
> >     }
> >
> >     /**
> > @@ -379,12 +368,7 @@
> >             try {
> >                 uninstallBundle(bundleIdentifiers[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -408,10 +392,10 @@
> >      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long,
> java.lang.String)
> >      */
> >     public void updateBundle(long bundleIdentifier, String url) throws
> IOException {
> > -        Bundle bundle = getBundle(0);
> > +        Bundle bundle = getBundle(bundleIdentifier);
> >         InputStream inputStream = null;
> >         try {
> > -            inputStream = new URL(url).openStream();
> > +            inputStream = createStream(url);
> >             bundle.update(inputStream);
> >         } catch (BundleException be) {
> >             if (inputStream != null) {
> > @@ -437,12 +421,7 @@
> >             try {
> >                 updateBundle(bundleIdentifiers[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -463,12 +442,7 @@
> >             try {
> >                 updateBundle(bundleIdentifiers[i], urls[i]);
> >             } catch (Throwable t) {
> > -                long[] completed = new long[i];
> > -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > -                long[] remaining = new long[completed.length - i - 1];
> > -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > -                return new BatchActionResult(completed, t.toString(),
> remaining, bundleIdentifiers[i])
> > -                        .toCompositeData();
> > +                return createFailedBatchActionResult(bundleIdentifiers,
> i, t);
> >             }
> >         }
> >         return new
> BatchActionResult(bundleIdentifiers).toCompositeData();
> > @@ -488,6 +462,22 @@
> >     }
> >
> >     /**
> > +     * Create {@link BatchActionResult}, when the operation fail.
> > +     *
> > +     * @param bundleIdentifiers bundle ids for operation.
> > +     * @param i index of loop pointing on which operation fails.
> > +     * @param t Throwable thrown by failed operation.
> > +     * @return created BatchActionResult instance.
> > +     */
> > +    private CompositeData createFailedBatchActionResult(long[]
> bundleIdentifiers, int i, Throwable t) {
> > +        long[] completed = new long[i];
> > +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> > +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
> > +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0,
> remaining.length);
> > +        return new BatchActionResult(completed, t.toString(), remaining,
> bundleIdentifiers[i]).toCompositeData();
> > +    }
> > +
> > +    /**
> >      * Gets bundle with provided bundleId.
> >      *
> >      * @param bundleIdentifier bundle id.
> >
> > Added:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> > URL:
> http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
> >
> ==============================================================================
> > ---
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> (added)
> > +++
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> Mon Nov 30 22:41:08 2009
> > @@ -0,0 +1,510 @@
> > +/**
> > + *  Licensed to the Apache Software Foundation (ASF) under one or more
> > + *  contributor license agreements.  See the NOTICE file distributed
> with
> > + *  this work for additional information regarding copyright ownership.
> > + *  The ASF licenses this file to You under the Apache License, Version
> 2.0
> > + *  (the "License"); you may not use this file except in compliance with
> > + *  the License.  You may obtain a copy of the License at
> > + *
> > + *     http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + *  Unless required by applicable law or agreed to in writing, software
> > + *  distributed under the License is distributed on an "AS IS" BASIS,
> > + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + *  See the License for the specific language governing permissions and
> > + *  limitations under the License.
> > + */
> > +package org.apache.aries.jmx.framework;
> > +
> > +import java.io.IOException;
> > +import java.io.InputStream;
> > +
> > +import javax.management.openmbean.CompositeData;
> > +
> > +import org.junit.Assert;
> > +
> > +import org.apache.aries.jmx.codec.BatchActionResult;
> > +import org.apache.aries.jmx.codec.BatchInstallResult;
> > +import org.junit.Before;
> > +import org.junit.Test;
> > +import org.mockito.Mock;
> > +import org.mockito.Mockito;
> > +import org.mockito.MockitoAnnotations;
> > +import org.osgi.framework.Bundle;
> > +import org.osgi.framework.BundleContext;
> > +import org.osgi.framework.BundleException;
> > +import org.osgi.jmx.framework.FrameworkMBean;
> > +import org.osgi.service.packageadmin.PackageAdmin;
> > +import org.osgi.service.startlevel.StartLevel;
> > +
> > +/**
> > + * {@link FrameworkMBean} test case.
> > + *
> > + *
> > + * @version $Rev$ $Date$
> > + */
> > +public class FrameworkTest {
> > +
> > +    @Mock
> > +    private StartLevel startLevel;
> > +    @Mock
> > +    private PackageAdmin admin;
> > +    @Mock
> > +    private BundleContext context;
> > +    private Framework mbean;
> > +
> > +    @Before
> > +    public void setUp() throws Exception {
> > +        MockitoAnnotations.initMocks(this);
> > +        mbean = new Framework(context, startLevel, admin);
> > +    }
> > +
> > +    @Test
> > +    public void testGetFrameworkStartLevel() throws IOException {
> > +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
> > +        int level = mbean.getFrameworkStartLevel();
> > +        Assert.assertEquals(1, level);
> > +    }
> > +
> > +    @Test
> > +    public void testGetInitialBundleStartLevel() throws IOException {
> > +
>  Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
> > +        int level = mbean.getInitialBundleStartLevel();
> > +        Mockito.verify(startLevel).getInitialBundleStartLevel();
> > +        Assert.assertEquals(2, level);
> > +    }
> > +
> > +    @Test
> > +    public void testInstallBundleString() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +
>  Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> > +        long bundleId = mbean.installBundle("file:test.jar");
> > +        Assert.assertEquals(2, bundleId);
> > +        Mockito.reset(context);
> > +
>  Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new
> BundleException("location doesn't exist"));
> > +
> > +        try {
> > +            mbean.installBundle("file:test2.jar");
> > +            Assert.fail("Shouldn't go to this stage, location doesn't
> exist");
> > +        } catch (IOException e) {
> > +            // ok
> > +        }
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testInstallBundleStringString() throws Exception {
> > +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.installBundle(Mockito.anyString(),
> Mockito.any(InputStream.class))).thenReturn(bundle);
> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> > +        Framework spiedMBean = Mockito.spy(partialMock);
> > +        InputStream stream = Mockito.mock(InputStream.class);
> > +
>  Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
> > +        long bundleId = spiedMBean.installBundle("file:test.jar",
> "test.jar");
> > +        Assert.assertEquals(2, bundleId);
> > +        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"));
> > +
> > +        try {
> > +            spiedMBean.installBundle("file:test2.jar", "test.jar");
> > +            Assert.fail("Shouldn't go to this stage, location doesn't
> exist");
> > +        } catch (IOException e) {
> > +            // ok
> > +        }
> > +    }
> > +
> > +    @Test
> > +    public void testInstallBundlesStringArray() 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(new Long(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"));
> > +
> > +        /*
> > +         * this one is not passing bug in api bundleInError should be
> String CompositeData data2 =
> > +         * mbean.installBundles(locations); BatchInstallResult batch2 =
> BatchInstallResult.from(data2);
> > +         * Assert.assertNotNull(batch2);
> Assert.assertNull(batch2.getCompleted());
> > +         * Assert.assertFalse(batch2.isSuccess());
> Assert.assertNotNull(batch2.getError());
> > +         * Assert.assertEquals("file:test.jar",
> batch2.getBundleInError());
> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
> > +         */
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testInstallBundlesStringArrayStringArray() throws
> Exception {
> > +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.installBundle(Mockito.anyString(),
> Mockito.any(InputStream.class))).thenReturn(bundle);
> > +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> > +        Framework spiedMBean = Mockito.spy(partialMock);
> > +        InputStream stream = Mockito.mock(InputStream.class);
> > +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> > +        CompositeData data = spiedMBean.installBundles(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"));
> > +
> > +        /*
> > +         * this one is not passing bug in api bundleInError should be
> String CompositeData data2 =
> > +         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new
> String[]{"test.jar"}); BatchInstallResult batch2
> > +         * = BatchInstallResult.from(data2);
> Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
> > +         * Assert.assertFalse(batch2.isSuccess());
> Assert.assertNotNull(batch2.getError());
> > +         * Assert.assertEquals("file:test.jar",
> batch2.getBundleInError());
> > +         * Assert.assertNull(batch2.getRemainingLocationItems());
> > +         */
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testRefreshPackagesLong() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> > +
> > +        mbean.refreshPackages(1);
> > +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
> > +
> > +        try {
> > +            mbean.refreshPackages(2);
> > +            Assert.fail("Shouldn't happen illegal argument");
> > +        } catch (IllegalArgumentException iae) {
> > +            // expected
> > +        }
> > +    }
> > +
> > +    @Test
> > +    public void testRefreshPackagesLongArray() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> > +
> > +        mbean.refreshPackages(new long[] { 1 });
> > +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
> > +
> > +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
> > +        // shouldn't found bundle whith id 2
> > +        Assert.assertNotNull(data);
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(0, batch.getCompleted().length);
> > +        Assert.assertFalse(batch.isSuccess());
> > +        Assert.assertNotNull(batch.getError());
> > +        Assert.assertNotNull(batch.getRemainingItems());
> > +        Assert.assertEquals(2, batch.getBundleInError());
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testResolveBundle() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> > +
> > +        mbean.resolveBundle(1);
> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> > +    }
> > +
> > +    @Test
> > +    public void testResolveBundles() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> > +
> > +        mbean.resolveBundles(new long[] { 1 });
> > +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> > +    }
> > +
> > +    @Test
> > +    public void testRestartFramework() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> > +        mbean.restartFramework();
> > +        Mockito.verify(bundle).update();
> > +    }
> > +
> > +    @Test
> > +    public void testSetBundleStartLevel() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> > +        mbean.setBundleStartLevel(2, 1);
> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
> > +    }
> > +
> > +    @Test
> > +    public void testSetBundleStartLevels() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> > +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2
> }, new int[] { 2 });
> > +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(2, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2
> }, new int[] { 2, 4 });
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertNull(batch2.getCompleted());
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testSetFrameworkStartLevel() throws IOException {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> > +        mbean.setFrameworkStartLevel(1);
> > +        Mockito.verify(startLevel).setStartLevel(1);
> > +
> > +    }
> > +
> > +    @Test
> > +    public void testSetInitialBundleStartLevel() throws IOException {
> > +        mbean.setInitialBundleStartLevel(5);
> > +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
> > +    }
> > +
> > +    @Test
> > +    public void testShutdownFramework() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> > +        mbean.shutdownFramework();
> > +        Mockito.verify(bundle).stop();
> > +    }
> > +
> > +    @Test
> > +    public void testStartBundle() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        mbean.startBundle(5);
> > +        Mockito.verify(bundle).start();
> > +
> > +        Mockito.reset(context);
> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> > +        Mockito.doThrow(new BundleException("")).when(bundle).start();
> > +
> > +        try {
> > +            mbean.startBundle(6);
> > +            Assert.fail("Shouldn't go to this stage, BundleException was
> thrown");
> > +        } catch (IOException ioe) {
> > +            //expected
> > +        }
> > +    }
> > +
> > +    @Test
> > +    public void testStartBundles() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        CompositeData data = mbean.startBundles(new long[] { 5 });
> > +        Mockito.verify(bundle).start();
> > +
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = mbean.startBundles(null);
> > +
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertNull(batch2.getCompleted());
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +    }
> > +
> > +    @Test
> > +    public void testStopBundle() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        mbean.stopBundle(5);
> > +        Mockito.verify(bundle).stop();
> > +    }
> > +
> > +    @Test
> > +    public void testStopBundles() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        CompositeData data = mbean.stopBundles(new long[] { 5 });
> > +        Mockito.verify(bundle).stop();
> > +
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = mbean.stopBundles(null);
> > +
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertNull(batch2.getCompleted());
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +    }
> > +
> > +    @Test
> > +    public void testUninstallBundle() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        mbean.uninstallBundle(5);
> > +        Mockito.verify(bundle).uninstall();
> > +    }
> > +
> > +    @Test
> > +    public void testUninstallBundles() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
> > +        Mockito.verify(bundle).uninstall();
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = mbean.uninstallBundles(null);
> > +
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertNull(batch2.getCompleted());
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +    }
> > +
> > +    @Test
> > +    public void testUpdateBundleLong() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        mbean.updateBundle(5);
> > +        Mockito.verify(bundle).update();
> > +    }
> > +
> > +    @Test
> > +    public void testUpdateBundleLongString() throws Exception {
> > +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> > +        Framework spiedMBean = Mockito.spy(partialMock);
> > +        InputStream stream = Mockito.mock(InputStream.class);
> > +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        spiedMBean.updateBundle(5, "file:test.jar");
> > +        Mockito.verify(bundle).update(stream);
> > +    }
> > +
> > +    @Test
> > +    public void testUpdateBundlesLongArray() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        CompositeData data = mbean.updateBundles(new long[] { 5 });
> > +        Mockito.verify(bundle).update();
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = mbean.updateBundles(null);
> > +
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertNull(batch2.getCompleted());
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +
> > +        Mockito.reset(bundle);
> > +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> > +        Mockito.doThrow(new BundleException("")).when(bundle).update();
> > +        BatchActionResult batch3 = BatchActionResult.from(data3);
> > +        Assert.assertEquals(0, batch3.getCompleted().length);
> > +        Assert.assertFalse(batch3.isSuccess());
> > +        Assert.assertNotNull(batch3.getError());
> > +        Assert.assertEquals(6, batch3.getBundleInError());
> > +
> > +        Bundle bundle6 = Mockito.mock(Bundle.class);
> > +        Bundle bundle8 = Mockito.mock(Bundle.class);
> > +        Bundle bundle7 = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
> > +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
> > +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
> > +        Mockito.doThrow(new BundleException("")).when(bundle8).update();
> > +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7
> });
> > +        BatchActionResult batch4 = BatchActionResult.from(data4);
> > +        Mockito.verify(bundle6).update();
> > +        Assert.assertEquals(1, batch4.getCompleted().length);
> > +        // should contain only bundleid 6
> > +        Assert.assertEquals(6, batch4.getCompleted()[0]);
> > +        Assert.assertFalse(batch4.isSuccess());
> > +        Assert.assertNotNull(batch4.getError());
> > +        Assert.assertEquals(8, batch4.getBundleInError());
> > +        Assert.assertEquals(1, batch4.getRemainingItems().length);
> > +        // should contain only bundleid 7
> > +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
> > +    }
> > +
> > +    @Test
> > +    public void testUpdateBundlesLongArrayStringArray() throws Exception
> {
> > +        Framework partialMock = new FrameworkPartialMock(context,
> startLevel, admin);
> > +        Framework spiedMBean = Mockito.spy(partialMock);
> > +        InputStream stream = Mockito.mock(InputStream.class);
> > +
>  Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> > +        CompositeData data = spiedMBean.updateBundles(new long[] { 5 },
> new String[] { "file:test.jar" });
> > +        Mockito.verify(bundle).update(stream);
> > +        BatchActionResult batch = BatchActionResult.from(data);
> > +        Assert.assertEquals(5, batch.getCompleted()[0]);
> > +        Assert.assertTrue(batch.isSuccess());
> > +        Assert.assertNull(batch.getError());
> > +        Assert.assertNull(batch.getRemainingItems());
> > +
> > +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4
> }, new String[] { "file:test.jar" });
> > +        BatchActionResult batch2 = BatchActionResult.from(data2);
> > +        Assert.assertFalse(batch2.isSuccess());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNotNull(batch2.getError());
> > +        Assert.assertNull(batch2.getRemainingItems());
> > +    }
> > +
> > +    @Test
> > +    public void testUpdateFramework() throws Exception {
> > +        Bundle bundle = Mockito.mock(Bundle.class);
> > +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> > +        mbean.restartFramework();
> > +        Mockito.verify(bundle).update();
> > +    }
> > +
> > +    /**
> > +     * Mocking {@link Framework} method createStream.
> > +     */
> > +    private class FrameworkPartialMock extends Framework {
> > +
> > +        public FrameworkPartialMock(BundleContext context, StartLevel
> startLevel, PackageAdmin packageAdmin) {
> > +            super(context, startLevel, packageAdmin);
> > +        }
> > +
> > +        public InputStream createStream(String url) throws IOException {
> > +            return null;
> > +        }
> > +    }
> > +
> > +}
> >
> > Propchange:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >
> ------------------------------------------------------------------------------
> >    svn:eol-style = native
> >
> > Propchange:
> incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> >
> ------------------------------------------------------------------------------
> >    svn:keywords = Revision Date
> >
> >
> >
>
>
>
> --
> Alasdair Nottingham
> alasdair.nottingham@gmail.com
>

Re: svn commit: r885603 - in /incubator/aries/trunk/jmx/jmx-core/src: main/java/org/apache/aries/jmx/framework/Framework.java test/java/org/apache/aries/jmx/framework/FrameworkTest.java

Posted by Alasdair Nottingham <al...@gmail.com>.
Hi,

Looking at this change the installBundle method does this:

            inputStream = createStream(url);
            Bundle bundle = context.installBundle(location, inputStream);

when I think it could just do this:

            Bundle bundle = context.installBundle(location);

By doing it the following way you could install a directory as a
bundle if the framework is equinox.

What do you think?
Alasdair

2009/11/30  <aw...@apache.org>:
> Author: awojtuniak
> Date: Mon Nov 30 22:41:08 2009
> New Revision: 885603
>
> URL: http://svn.apache.org/viewvc?rev=885603&view=rev
> Log:
> ARIES-30 Framework MBean test case
>
> Added:
>    incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java   (with props)
> Modified:
>    incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
>
> Modified: incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=885603&r1=885602&r2=885603&view=diff
> ==============================================================================
> --- incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java (original)
> +++ incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java Mon Nov 30 22:41:08 2009
> @@ -88,16 +88,27 @@
>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundle(java.lang.String, java.lang.String)
>      */
>     public long installBundle(String location, String url) throws IOException {
> -        InputStream inputStream;
> +        InputStream inputStream = null;
>         try {
> -            inputStream = new URL(url).openStream();
> +            inputStream = createStream(url);
>             Bundle bundle = context.installBundle(location, inputStream);
>             return bundle.getBundleId();
>         } catch (BundleException e) {
> +            if (inputStream != null) {
> +                try {
> +                    inputStream.close();
> +                } catch (IOException ioe) {
> +
> +                }
> +            }
>             throw new IOException("Can't install bundle with location" + location);
>         }
>     }
>
> +    public InputStream createStream(String url) throws IOException {
> +        return new URL(url).openStream();
> +    }
> +
>     /**
>      * @see org.osgi.jmx.framework.FrameworkMBean#installBundles(java.lang.String[])
>      */
> @@ -170,12 +181,7 @@
>             try {
>                 refreshPackages(bundleIdentifiers[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -241,12 +247,7 @@
>             try {
>                 setBundleStartLevel(bundleIdentifiers[i], newlevels[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -304,12 +305,7 @@
>             try {
>                 startBundle(bundleIdentifiers[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -340,12 +336,7 @@
>             try {
>                 stopBundle(bundleIdentifiers[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -355,7 +346,7 @@
>      * @see org.osgi.jmx.framework.FrameworkMBean#uninstallBundle(long)
>      */
>     public void uninstallBundle(long bundleIdentifier) throws IOException {
> -        Bundle bundle = context.getBundle(bundleIdentifier);
> +        Bundle bundle = getBundle(bundleIdentifier);
>         if (bundle != null) {
>             try {
>                 bundle.uninstall();
> @@ -364,8 +355,6 @@
>             }
>         }
>
> -        throw new IllegalArgumentException("Can't find bundle with id " + bundleIdentifier);
> -
>     }
>
>     /**
> @@ -379,12 +368,7 @@
>             try {
>                 uninstallBundle(bundleIdentifiers[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -408,10 +392,10 @@
>      * @see org.osgi.jmx.framework.FrameworkMBean#updateBundle(long, java.lang.String)
>      */
>     public void updateBundle(long bundleIdentifier, String url) throws IOException {
> -        Bundle bundle = getBundle(0);
> +        Bundle bundle = getBundle(bundleIdentifier);
>         InputStream inputStream = null;
>         try {
> -            inputStream = new URL(url).openStream();
> +            inputStream = createStream(url);
>             bundle.update(inputStream);
>         } catch (BundleException be) {
>             if (inputStream != null) {
> @@ -437,12 +421,7 @@
>             try {
>                 updateBundle(bundleIdentifiers[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -463,12 +442,7 @@
>             try {
>                 updateBundle(bundleIdentifiers[i], urls[i]);
>             } catch (Throwable t) {
> -                long[] completed = new long[i];
> -                System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> -                long[] remaining = new long[completed.length - i - 1];
> -                System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> -                return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i])
> -                        .toCompositeData();
> +                return createFailedBatchActionResult(bundleIdentifiers, i, t);
>             }
>         }
>         return new BatchActionResult(bundleIdentifiers).toCompositeData();
> @@ -488,6 +462,22 @@
>     }
>
>     /**
> +     * Create {@link BatchActionResult}, when the operation fail.
> +     *
> +     * @param bundleIdentifiers bundle ids for operation.
> +     * @param i index of loop pointing on which operation fails.
> +     * @param t Throwable thrown by failed operation.
> +     * @return created BatchActionResult instance.
> +     */
> +    private CompositeData createFailedBatchActionResult(long[] bundleIdentifiers, int i, Throwable t) {
> +        long[] completed = new long[i];
> +        System.arraycopy(bundleIdentifiers, 0, completed, 0, i);
> +        long[] remaining = new long[bundleIdentifiers.length - i - 1];
> +        System.arraycopy(bundleIdentifiers, i + 1, remaining, 0, remaining.length);
> +        return new BatchActionResult(completed, t.toString(), remaining, bundleIdentifiers[i]).toCompositeData();
> +    }
> +
> +    /**
>      * Gets bundle with provided bundleId.
>      *
>      * @param bundleIdentifier bundle id.
>
> Added: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=885603&view=auto
> ==============================================================================
> --- incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java (added)
> +++ incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java Mon Nov 30 22:41:08 2009
> @@ -0,0 +1,510 @@
> +/**
> + *  Licensed to the Apache Software Foundation (ASF) under one or more
> + *  contributor license agreements.  See the NOTICE file distributed with
> + *  this work for additional information regarding copyright ownership.
> + *  The ASF licenses this file to You under the Apache License, Version 2.0
> + *  (the "License"); you may not use this file except in compliance with
> + *  the License.  You may obtain a copy of the License at
> + *
> + *     http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing, software
> + *  distributed under the License is distributed on an "AS IS" BASIS,
> + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + *  See the License for the specific language governing permissions and
> + *  limitations under the License.
> + */
> +package org.apache.aries.jmx.framework;
> +
> +import java.io.IOException;
> +import java.io.InputStream;
> +
> +import javax.management.openmbean.CompositeData;
> +
> +import org.junit.Assert;
> +
> +import org.apache.aries.jmx.codec.BatchActionResult;
> +import org.apache.aries.jmx.codec.BatchInstallResult;
> +import org.junit.Before;
> +import org.junit.Test;
> +import org.mockito.Mock;
> +import org.mockito.Mockito;
> +import org.mockito.MockitoAnnotations;
> +import org.osgi.framework.Bundle;
> +import org.osgi.framework.BundleContext;
> +import org.osgi.framework.BundleException;
> +import org.osgi.jmx.framework.FrameworkMBean;
> +import org.osgi.service.packageadmin.PackageAdmin;
> +import org.osgi.service.startlevel.StartLevel;
> +
> +/**
> + * {@link FrameworkMBean} test case.
> + *
> + *
> + * @version $Rev$ $Date$
> + */
> +public class FrameworkTest {
> +
> +    @Mock
> +    private StartLevel startLevel;
> +    @Mock
> +    private PackageAdmin admin;
> +    @Mock
> +    private BundleContext context;
> +    private Framework mbean;
> +
> +    @Before
> +    public void setUp() throws Exception {
> +        MockitoAnnotations.initMocks(this);
> +        mbean = new Framework(context, startLevel, admin);
> +    }
> +
> +    @Test
> +    public void testGetFrameworkStartLevel() throws IOException {
> +        Mockito.when(startLevel.getStartLevel()).thenReturn(1);
> +        int level = mbean.getFrameworkStartLevel();
> +        Assert.assertEquals(1, level);
> +    }
> +
> +    @Test
> +    public void testGetInitialBundleStartLevel() throws IOException {
> +        Mockito.when(startLevel.getInitialBundleStartLevel()).thenReturn(2);
> +        int level = mbean.getInitialBundleStartLevel();
> +        Mockito.verify(startLevel).getInitialBundleStartLevel();
> +        Assert.assertEquals(2, level);
> +    }
> +
> +    @Test
> +    public void testInstallBundleString() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> +        long bundleId = mbean.installBundle("file:test.jar");
> +        Assert.assertEquals(2, bundleId);
> +        Mockito.reset(context);
> +        Mockito.when(context.installBundle("file:test2.jar")).thenThrow(new BundleException("location doesn't exist"));
> +
> +        try {
> +            mbean.installBundle("file:test2.jar");
> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
> +        } catch (IOException e) {
> +            // ok
> +        }
> +
> +    }
> +
> +    @Test
> +    public void testInstallBundleStringString() throws Exception {
> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> +        Framework spiedMBean = Mockito.spy(partialMock);
> +        InputStream stream = Mockito.mock(InputStream.class);
> +        Mockito.when(spiedMBean.createStream("test.jar")).thenReturn(stream);
> +        long bundleId = spiedMBean.installBundle("file:test.jar", "test.jar");
> +        Assert.assertEquals(2, bundleId);
> +        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"));
> +
> +        try {
> +            spiedMBean.installBundle("file:test2.jar", "test.jar");
> +            Assert.fail("Shouldn't go to this stage, location doesn't exist");
> +        } catch (IOException e) {
> +            // ok
> +        }
> +    }
> +
> +    @Test
> +    public void testInstallBundlesStringArray() 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(new Long(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"));
> +
> +        /*
> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
> +         * mbean.installBundles(locations); BatchInstallResult batch2 = BatchInstallResult.from(data2);
> +         * Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
> +         * Assert.assertNull(batch2.getRemainingLocationItems());
> +         */
> +
> +    }
> +
> +    @Test
> +    public void testInstallBundlesStringArrayStringArray() throws Exception {
> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
> +        Mockito.when(bundle.getBundleId()).thenReturn(new Long(2));
> +        Framework spiedMBean = Mockito.spy(partialMock);
> +        InputStream stream = Mockito.mock(InputStream.class);
> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> +        CompositeData data = spiedMBean.installBundles(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"));
> +
> +        /*
> +         * this one is not passing bug in api bundleInError should be String CompositeData data2 =
> +         * spiedMBean.installBundles(new String[]{"file:test.jar"}, new String[]{"test.jar"}); BatchInstallResult batch2
> +         * = BatchInstallResult.from(data2); Assert.assertNotNull(batch2); Assert.assertNull(batch2.getCompleted());
> +         * Assert.assertFalse(batch2.isSuccess()); Assert.assertNotNull(batch2.getError());
> +         * Assert.assertEquals("file:test.jar", batch2.getBundleInError());
> +         * Assert.assertNull(batch2.getRemainingLocationItems());
> +         */
> +
> +    }
> +
> +    @Test
> +    public void testRefreshPackagesLong() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> +
> +        mbean.refreshPackages(1);
> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
> +
> +        try {
> +            mbean.refreshPackages(2);
> +            Assert.fail("Shouldn't happen illegal argument");
> +        } catch (IllegalArgumentException iae) {
> +            // expected
> +        }
> +    }
> +
> +    @Test
> +    public void testRefreshPackagesLongArray() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> +
> +        mbean.refreshPackages(new long[] { 1 });
> +        Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
> +
> +        CompositeData data = mbean.refreshPackages(new long[] { 2 });
> +        // shouldn't found bundle whith id 2
> +        Assert.assertNotNull(data);
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(0, batch.getCompleted().length);
> +        Assert.assertFalse(batch.isSuccess());
> +        Assert.assertNotNull(batch.getError());
> +        Assert.assertNotNull(batch.getRemainingItems());
> +        Assert.assertEquals(2, batch.getBundleInError());
> +
> +    }
> +
> +    @Test
> +    public void testResolveBundle() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> +
> +        mbean.resolveBundle(1);
> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> +    }
> +
> +    @Test
> +    public void testResolveBundles() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(1)).thenReturn(bundle);
> +
> +        mbean.resolveBundles(new long[] { 1 });
> +        Mockito.verify(admin).resolveBundles(new Bundle[] { bundle });
> +    }
> +
> +    @Test
> +    public void testRestartFramework() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> +        mbean.restartFramework();
> +        Mockito.verify(bundle).update();
> +    }
> +
> +    @Test
> +    public void testSetBundleStartLevel() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> +        mbean.setBundleStartLevel(2, 1);
> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 1);
> +    }
> +
> +    @Test
> +    public void testSetBundleStartLevels() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(2)).thenReturn(bundle);
> +        CompositeData data = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2 });
> +        Mockito.verify(startLevel).setBundleStartLevel(bundle, 2);
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(2, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = mbean.setBundleStartLevels(new long[] { 2 }, new int[] { 2, 4 });
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertNull(batch2.getCompleted());
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +
> +    }
> +
> +    @Test
> +    public void testSetFrameworkStartLevel() throws IOException {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> +        mbean.setFrameworkStartLevel(1);
> +        Mockito.verify(startLevel).setStartLevel(1);
> +
> +    }
> +
> +    @Test
> +    public void testSetInitialBundleStartLevel() throws IOException {
> +        mbean.setInitialBundleStartLevel(5);
> +        Mockito.verify(startLevel).setInitialBundleStartLevel(5);
> +    }
> +
> +    @Test
> +    public void testShutdownFramework() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> +        mbean.shutdownFramework();
> +        Mockito.verify(bundle).stop();
> +    }
> +
> +    @Test
> +    public void testStartBundle() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        mbean.startBundle(5);
> +        Mockito.verify(bundle).start();
> +
> +        Mockito.reset(context);
> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> +        Mockito.doThrow(new BundleException("")).when(bundle).start();
> +
> +        try {
> +            mbean.startBundle(6);
> +            Assert.fail("Shouldn't go to this stage, BundleException was thrown");
> +        } catch (IOException ioe) {
> +            //expected
> +        }
> +    }
> +
> +    @Test
> +    public void testStartBundles() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        CompositeData data = mbean.startBundles(new long[] { 5 });
> +        Mockito.verify(bundle).start();
> +
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = mbean.startBundles(null);
> +
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertNull(batch2.getCompleted());
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +    }
> +
> +    @Test
> +    public void testStopBundle() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        mbean.stopBundle(5);
> +        Mockito.verify(bundle).stop();
> +    }
> +
> +    @Test
> +    public void testStopBundles() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        CompositeData data = mbean.stopBundles(new long[] { 5 });
> +        Mockito.verify(bundle).stop();
> +
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = mbean.stopBundles(null);
> +
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertNull(batch2.getCompleted());
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +    }
> +
> +    @Test
> +    public void testUninstallBundle() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        mbean.uninstallBundle(5);
> +        Mockito.verify(bundle).uninstall();
> +    }
> +
> +    @Test
> +    public void testUninstallBundles() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        CompositeData data = mbean.uninstallBundles(new long[] { 5 });
> +        Mockito.verify(bundle).uninstall();
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = mbean.uninstallBundles(null);
> +
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertNull(batch2.getCompleted());
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +    }
> +
> +    @Test
> +    public void testUpdateBundleLong() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        mbean.updateBundle(5);
> +        Mockito.verify(bundle).update();
> +    }
> +
> +    @Test
> +    public void testUpdateBundleLongString() throws Exception {
> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
> +        Framework spiedMBean = Mockito.spy(partialMock);
> +        InputStream stream = Mockito.mock(InputStream.class);
> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        spiedMBean.updateBundle(5, "file:test.jar");
> +        Mockito.verify(bundle).update(stream);
> +    }
> +
> +    @Test
> +    public void testUpdateBundlesLongArray() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        CompositeData data = mbean.updateBundles(new long[] { 5 });
> +        Mockito.verify(bundle).update();
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = mbean.updateBundles(null);
> +
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertNull(batch2.getCompleted());
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +
> +        Mockito.reset(bundle);
> +        CompositeData data3 = mbean.updateBundles(new long[] { 6 });
> +        Mockito.when(context.getBundle(6)).thenReturn(bundle);
> +        Mockito.doThrow(new BundleException("")).when(bundle).update();
> +        BatchActionResult batch3 = BatchActionResult.from(data3);
> +        Assert.assertEquals(0, batch3.getCompleted().length);
> +        Assert.assertFalse(batch3.isSuccess());
> +        Assert.assertNotNull(batch3.getError());
> +        Assert.assertEquals(6, batch3.getBundleInError());
> +
> +        Bundle bundle6 = Mockito.mock(Bundle.class);
> +        Bundle bundle8 = Mockito.mock(Bundle.class);
> +        Bundle bundle7 = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(6)).thenReturn(bundle6);
> +        Mockito.when(context.getBundle(8)).thenReturn(bundle8);
> +        Mockito.when(context.getBundle(7)).thenReturn(bundle7);
> +        Mockito.doThrow(new BundleException("")).when(bundle8).update();
> +        CompositeData data4 = mbean.updateBundles(new long[] { 6, 8, 7 });
> +        BatchActionResult batch4 = BatchActionResult.from(data4);
> +        Mockito.verify(bundle6).update();
> +        Assert.assertEquals(1, batch4.getCompleted().length);
> +        // should contain only bundleid 6
> +        Assert.assertEquals(6, batch4.getCompleted()[0]);
> +        Assert.assertFalse(batch4.isSuccess());
> +        Assert.assertNotNull(batch4.getError());
> +        Assert.assertEquals(8, batch4.getBundleInError());
> +        Assert.assertEquals(1, batch4.getRemainingItems().length);
> +        // should contain only bundleid 7
> +        Assert.assertEquals(7, batch4.getRemainingItems()[0]);
> +    }
> +
> +    @Test
> +    public void testUpdateBundlesLongArrayStringArray() throws Exception {
> +        Framework partialMock = new FrameworkPartialMock(context, startLevel, admin);
> +        Framework spiedMBean = Mockito.spy(partialMock);
> +        InputStream stream = Mockito.mock(InputStream.class);
> +        Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(5)).thenReturn(bundle);
> +        CompositeData data = spiedMBean.updateBundles(new long[] { 5 }, new String[] { "file:test.jar" });
> +        Mockito.verify(bundle).update(stream);
> +        BatchActionResult batch = BatchActionResult.from(data);
> +        Assert.assertEquals(5, batch.getCompleted()[0]);
> +        Assert.assertTrue(batch.isSuccess());
> +        Assert.assertNull(batch.getError());
> +        Assert.assertNull(batch.getRemainingItems());
> +
> +        CompositeData data2 = spiedMBean.updateBundles(new long[] { 2, 4 }, new String[] { "file:test.jar" });
> +        BatchActionResult batch2 = BatchActionResult.from(data2);
> +        Assert.assertFalse(batch2.isSuccess());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNotNull(batch2.getError());
> +        Assert.assertNull(batch2.getRemainingItems());
> +    }
> +
> +    @Test
> +    public void testUpdateFramework() throws Exception {
> +        Bundle bundle = Mockito.mock(Bundle.class);
> +        Mockito.when(context.getBundle(0)).thenReturn(bundle);
> +        mbean.restartFramework();
> +        Mockito.verify(bundle).update();
> +    }
> +
> +    /**
> +     * Mocking {@link Framework} method createStream.
> +     */
> +    private class FrameworkPartialMock extends Framework {
> +
> +        public FrameworkPartialMock(BundleContext context, StartLevel startLevel, PackageAdmin packageAdmin) {
> +            super(context, startLevel, packageAdmin);
> +        }
> +
> +        public InputStream createStream(String url) throws IOException {
> +            return null;
> +        }
> +    }
> +
> +}
>
> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
> ------------------------------------------------------------------------------
>    svn:keywords = Revision Date
>
>
>



-- 
Alasdair Nottingham
alasdair.nottingham@gmail.com