aries-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alasdair Nottingham <alasdair.notting...@gmail.com>
Subject 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
Date Tue, 01 Dec 2009 16:18:40 GMT
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  <awojtuniak@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

Mime
View raw message