You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by zo...@apache.org on 2011/02/27 18:58:23 UTC
svn commit: r1075096 [10/13] - in /aries/tags/jmx-0.1-incubating: ./
jmx-api/ jmx-api/src/ jmx-api/src/main/ jmx-api/src/main/appended-resources/
jmx-api/src/main/appended-resources/META-INF/ jmx-api/src/main/java/
jmx-api/src/main/java/org/ jmx-api/sr...
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/FrameworkTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,504 @@
+/**
+ * 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: 929337 $ $Date: 2010-03-31 00:10:23 +0100 (Wed, 31 Mar 2010) $
+ */
+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 testInstallBundle() throws Exception {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(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 testInstallBundleFromURL() throws Exception {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
+ Framework spiedMBean = Mockito.spy(mbean);
+ InputStream stream = Mockito.mock(InputStream.class);
+ Mockito.doReturn(stream).when(spiedMBean).createStream("test.jar");
+ long bundleId = spiedMBean.installBundleFromURL("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.installBundleFromURL("file:test2.jar", "test.jar");
+ Assert.fail("Shouldn't go to this stage, location doesn't exist");
+ } catch (IOException e) {
+ // ok
+ }
+ }
+
+ @Test
+ public void testInstallBundles() throws Exception {
+ String[] locations = new String[] { "file:test.jar" };
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.installBundle("file:test.jar")).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
+ CompositeData data = mbean.installBundles(locations);
+ BatchInstallResult batch = BatchInstallResult.from(data);
+ Assert.assertNotNull(batch);
+ Assert.assertEquals(2, batch.getCompleted()[0]);
+ Assert.assertTrue(batch.isSuccess());
+ Assert.assertNull(batch.getError());
+ Assert.assertNull(batch.getRemainingLocationItems());
+ Mockito.reset(context);
+ Mockito.when(context.installBundle("file:test.jar")).thenThrow(new BundleException("location doesn't exist"));
+ CompositeData data2 = mbean.installBundles(locations);
+ BatchInstallResult batch2 = BatchInstallResult.from(data2);
+ Assert.assertNotNull(batch2);
+ Assert.assertNotNull(batch2.getCompleted());
+ Assert.assertEquals(0, batch2.getCompleted().length);
+ Assert.assertFalse(batch2.isSuccess());
+ Assert.assertNotNull(batch2.getError());
+ Assert.assertEquals("file:test.jar", batch2.getBundleInError());
+ Assert.assertNotNull(batch2.getRemainingLocationItems());
+ Assert.assertEquals(0, batch2.getRemainingLocationItems().length);
+ }
+
+ @Test
+ public void testInstallBundlesFromURL() throws Exception {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
+ Framework spiedMBean = Mockito.spy(mbean);
+ InputStream stream = Mockito.mock(InputStream.class);
+ Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
+ CompositeData data = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
+ Assert.assertNotNull(data);
+ BatchInstallResult batch = BatchInstallResult.from(data);
+ Assert.assertEquals(2, batch.getCompleted()[0]);
+ Assert.assertTrue(batch.isSuccess());
+ Assert.assertNull(batch.getError());
+ Assert.assertNull(batch.getRemainingLocationItems());
+ Mockito.reset(context);
+ Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
+ Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenThrow(
+ new BundleException("location doesn't exist"));
+ CompositeData data2 = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
+ BatchInstallResult batch2 = BatchInstallResult.from(data2);
+ Assert.assertNotNull(batch2);
+ Assert.assertNotNull(batch2.getCompleted());
+ Assert.assertEquals(0, batch2.getCompleted().length);
+ Assert.assertFalse(batch2.isSuccess());
+ Assert.assertNotNull(batch2.getError());
+ Assert.assertEquals("file:test.jar", batch2.getBundleInError());
+ Assert.assertNotNull(batch2.getRemainingLocationItems());
+ Assert.assertEquals(0, batch2.getRemainingLocationItems().length);
+ }
+
+ @Test
+ public void testRefreshBundle() throws Exception {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+ mbean.refreshBundle(1);
+ Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
+
+ try {
+ mbean.refreshBundle(2);
+ Assert.fail("Shouldn't happen illegal argument");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testRefreshBundles() throws IOException {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.getBundle(1)).thenReturn(bundle);
+
+ mbean.refreshBundles(new long[] { 1 });
+ Mockito.verify(admin).refreshPackages((Bundle[]) Mockito.any());
+
+ mbean.refreshBundles(null);
+ Mockito.verify(admin).refreshPackages(null);
+ }
+
+ @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 });
+
+ mbean.resolveBundles(null);
+ Mockito.verify(admin).resolveBundles(null);
+ }
+
+ @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
+ }
+
+ Mockito.when(context.getBundle(6)).thenReturn(null);
+ try {
+ mbean.startBundle(6);
+ Assert.fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException iae) {
+ //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();
+
+ Mockito.when(context.getBundle(5)).thenReturn(null);
+ try {
+ mbean.stopBundle(5);
+ Assert.fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException iae) {
+ //expected
+ }
+
+ }
+
+ @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 testUpdateBundle() 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 testUpdateBundleFromUrl() throws Exception {
+ Framework spiedMBean = Mockito.spy(mbean);
+ InputStream stream = Mockito.mock(InputStream.class);
+ Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.getBundle(5)).thenReturn(bundle);
+ spiedMBean.updateBundleFromURL(5, "file:test.jar");
+ Mockito.verify(bundle).update(stream);
+ }
+
+ @Test
+ public void testUpdateBundles() 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 testUpdateBundlesFromURL() throws Exception {
+ Framework spiedMBean = Mockito.spy(mbean);
+ InputStream stream = Mockito.mock(InputStream.class);
+ Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.getBundle(5)).thenReturn(bundle);
+ CompositeData data = spiedMBean.updateBundlesFromURL(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.updateBundlesFromURL(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();
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/PackageStateTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/PackageStateTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/PackageStateTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/PackageStateTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,131 @@
+/**
+ * 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.util.Collection;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.TabularData;
+
+import org.junit.Assert;
+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.Version;
+import org.osgi.jmx.framework.PackageStateMBean;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/**
+ * {@link PackageStateMBean} test case.
+ *
+ *
+ * @version $Rev: 919624 $ $Date: 2010-03-05 21:22:07 +0000 (Fri, 05 Mar 2010) $
+ */
+public class PackageStateTest {
+
+ @Mock
+ private BundleContext context;
+ @Mock
+ private PackageAdmin admin;
+ private PackageState mbean;
+
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ mbean = new PackageState(context, admin);
+ }
+
+ @Test
+ public void testGetExportingBundles() throws IOException {
+ ExportedPackage exported = Mockito.mock(ExportedPackage.class);
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
+ Mockito.when(exported.getExportingBundle()).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(5));
+ ExportedPackage exported2 = Mockito.mock(ExportedPackage.class);
+ Bundle bundle2 = Mockito.mock(Bundle.class);
+ Mockito.when(exported2.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
+ Mockito.when(exported2.getExportingBundle()).thenReturn(bundle2);
+ Mockito.when(bundle2.getBundleId()).thenReturn(Long.valueOf(6));
+ Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[]{exported, exported2});
+ long[] ids = mbean.getExportingBundles("test", "1.0.0");
+ Assert.assertNotNull(ids);
+ Assert.assertArrayEquals(new long[]{5,6}, ids);
+ }
+
+ @Test
+ public void testGetImportingBundles() throws IOException {
+ ExportedPackage exported = Mockito.mock(ExportedPackage.class);
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Bundle exportingBundle = Mockito.mock(Bundle.class);
+ Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
+ Mockito.when(exported.getExportingBundle()).thenReturn(exportingBundle);
+ Mockito.when(exportingBundle.getBundleId()).thenReturn(Long.valueOf(2));
+ Mockito.when(exported.getImportingBundles()).thenReturn(new Bundle[]{bundle});
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(4));
+ Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[]{exported});
+ long[] ids = mbean.getImportingBundles("test", "1.0.0", 2);
+ Assert.assertArrayEquals(new long[]{4}, ids);
+ }
+
+ @Test
+ public void testIsRemovalPending() throws IOException {
+ ExportedPackage exported = Mockito.mock(ExportedPackage.class);
+ Bundle expBundle = Mockito.mock(Bundle.class);
+ Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
+ Mockito.when(exported.isRemovalPending()).thenReturn(true);
+ Mockito.when(exported.getExportingBundle()).thenReturn(expBundle);
+ Mockito.when(expBundle.getBundleId()).thenReturn(Long.valueOf(2));
+ Mockito.when(admin.getExportedPackages(Mockito.anyString())).thenReturn(new ExportedPackage[]{exported});
+ boolean isRemoval = mbean.isRemovalPending("test", "1.0.0", Long.valueOf(2));
+ Assert.assertTrue(isRemoval);
+ }
+
+ @Test
+ public void testListPackages() throws IOException {
+ Bundle bundle = Mockito.mock(Bundle.class);
+ Bundle impBundle = Mockito.mock(Bundle.class);
+ Mockito.when(context.getBundles()).thenReturn(new Bundle[]{bundle});
+ ExportedPackage exported = Mockito.mock(ExportedPackage.class);
+ Mockito.when(exported.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
+ Mockito.when(exported.getImportingBundles()).thenReturn(new Bundle[]{impBundle});
+ Mockito.when(exported.getName()).thenReturn("test");
+ Mockito.when(exported.getExportingBundle()).thenReturn(bundle);
+ Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(4));
+ Mockito.when(impBundle.getBundleId()).thenReturn(Long.valueOf(5));
+ Mockito.when(admin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[]{exported});
+ TabularData table = mbean.listPackages();
+ Assert.assertEquals(PackageStateMBean.PACKAGES_TYPE,table.getTabularType());
+ Collection values = table.values();
+ Assert.assertEquals(1, values.size());
+ CompositeData data = (CompositeData) values.iterator().next();
+ Long[] exportingBundles = (Long[])data.get(PackageStateMBean.EXPORTING_BUNDLES);
+ Assert.assertArrayEquals(new Long[]{Long.valueOf(4)}, exportingBundles);
+ String name = (String) data.get(PackageStateMBean.NAME);
+ Assert.assertEquals("test", name);
+ String version = (String) data.get(PackageStateMBean.VERSION);
+ Assert.assertEquals("1.0.0", version);
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,47 @@
+/**
+ * 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 static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.apache.aries.jmx.Logger;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+/**
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class ServiceStateMBeanHandlerTest {
+
+
+ @Test
+ public void testOpen() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ Logger logger = mock(Logger.class);
+
+ ServiceStateMBeanHandler handler = new ServiceStateMBeanHandler(context, logger);
+ handler.open();
+
+ assertNotNull(handler.getMbean());
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,202 @@
+/**
+ * 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 static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.atMost;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_IDENTIFIER;
+import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_LOCATION;
+import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_SYMBOLIC_NAME;
+import static org.osgi.jmx.framework.ServiceStateMBean.EVENT;
+import static org.osgi.jmx.framework.ServiceStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.ServiceStateMBean.OBJECTNAME;
+import static org.osgi.jmx.framework.ServiceStateMBean.OBJECT_CLASS;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import javax.management.MBeanServer;
+import javax.management.Notification;
+import javax.management.NotificationListener;
+import javax.management.ObjectName;
+import javax.management.openmbean.CompositeData;
+
+import org.apache.aries.jmx.Logger;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.osgi.framework.AllServiceListener;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class ServiceStateTest {
+
+
+ @Test
+ public void testNotificationsForServiceEvents() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ Logger logger = mock(Logger.class);
+
+ ServiceState serviceState = new ServiceState(context, logger);
+
+ ServiceReference reference = mock(ServiceReference.class);
+ Bundle b1 = mock(Bundle.class);
+
+ when(b1.getBundleId()).thenReturn(new Long(9));
+ when(b1.getSymbolicName()).thenReturn("bundle");
+ when(b1.getLocation()).thenReturn("file:/location");
+ when(reference.getBundle()).thenReturn(b1);
+ when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
+ when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] {"org.apache.aries.jmx.Mock"});
+
+ ServiceEvent registeredEvent = mock(ServiceEvent.class);
+ when(registeredEvent.getServiceReference()).thenReturn(reference);
+ when(registeredEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
+
+ ServiceEvent modifiedEvent = mock(ServiceEvent.class);
+ when(modifiedEvent.getServiceReference()).thenReturn(reference);
+ when(modifiedEvent.getType()).thenReturn(ServiceEvent.MODIFIED);
+
+ MBeanServer server = mock(MBeanServer.class);
+
+ //setup for notification
+ ObjectName objectName = new ObjectName(OBJECTNAME);
+ serviceState.preRegister(server, objectName);
+ serviceState.postRegister(true);
+
+ //holder for Notifications captured
+ final List<Notification> received = new LinkedList<Notification>();
+
+ //add NotificationListener to receive the events
+ serviceState.addNotificationListener(new NotificationListener() {
+ public void handleNotification(Notification notification, Object handback) {
+ received.add(notification);
+ }
+ }, null, null);
+
+ // capture the ServiceListener registered with BundleContext to issue ServiceEvents
+ ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
+ verify(context).addServiceListener(argument.capture());
+
+ //send events
+ AllServiceListener serviceListener = argument.getValue();
+ serviceListener.serviceChanged(registeredEvent);
+ serviceListener.serviceChanged(modifiedEvent);
+
+ //shutdown dispatcher via unregister callback
+ serviceState.postDeregister();
+ //check the ServiceListener is cleaned up
+ verify(context).removeServiceListener(serviceListener);
+
+ ExecutorService dispatcher = serviceState.getEventDispatcher();
+ assertTrue(dispatcher.isShutdown());
+ dispatcher.awaitTermination(2, TimeUnit.SECONDS);
+ assertTrue(dispatcher.isTerminated());
+
+ assertEquals(2, received.size());
+ Notification registered = received.get(0);
+ assertEquals(1, registered.getSequenceNumber());
+ CompositeData data = (CompositeData) registered.getUserData();
+ assertEquals(new Long(44), data.get(IDENTIFIER));
+ assertEquals(new Long(9), data.get(BUNDLE_IDENTIFIER));
+ assertEquals("file:/location", data.get(BUNDLE_LOCATION));
+ assertEquals("bundle", data.get(BUNDLE_SYMBOLIC_NAME));
+ assertArrayEquals(new String[] {"org.apache.aries.jmx.Mock" }, (String[]) data.get(OBJECT_CLASS));
+ assertEquals(ServiceEvent.REGISTERED, data.get(EVENT));
+
+ Notification modified = received.get(1);
+ assertEquals(2, modified.getSequenceNumber());
+ data = (CompositeData) modified.getUserData();
+ assertEquals(new Long(44), data.get(IDENTIFIER));
+ assertEquals(new Long(9), data.get(BUNDLE_IDENTIFIER));
+ assertEquals("file:/location", data.get(BUNDLE_LOCATION));
+ assertEquals("bundle", data.get(BUNDLE_SYMBOLIC_NAME));
+ assertArrayEquals(new String[] {"org.apache.aries.jmx.Mock" }, (String[]) data.get(OBJECT_CLASS));
+ assertEquals(ServiceEvent.MODIFIED, data.get(EVENT));
+
+ }
+
+ @Test
+ public void testLifeCycleOfNotificationSupport() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ Logger logger = mock(Logger.class);
+
+ ServiceState serviceState = new ServiceState(context, logger);
+
+ MBeanServer server1 = mock(MBeanServer.class);
+ MBeanServer server2 = mock(MBeanServer.class);
+
+ ObjectName objectName = new ObjectName(OBJECTNAME);
+ serviceState.preRegister(server1, objectName);
+ serviceState.postRegister(true);
+
+ // capture the ServiceListener registered with BundleContext to issue ServiceEvents
+ ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
+ verify(context).addServiceListener(argument.capture());
+
+ AllServiceListener serviceListener = argument.getValue();
+ assertNotNull(serviceListener);
+
+ ExecutorService dispatcher = serviceState.getEventDispatcher();
+
+ //do registration with another server
+ serviceState.preRegister(server2, objectName);
+ serviceState.postRegister(true);
+
+ // check no more actions on BundleContext
+ argument = ArgumentCaptor.forClass(AllServiceListener.class);
+ verify(context, atMost(1)).addServiceListener(argument.capture());
+ assertEquals(1, argument.getAllValues().size());
+
+ //do one unregister
+ serviceState.postDeregister();
+
+ //verify bundleListener not invoked
+ verify(context, never()).removeServiceListener(serviceListener);
+ assertFalse(dispatcher.isShutdown());
+
+ //do second unregister and check cleanup
+ serviceState.postDeregister();
+ verify(context).removeServiceListener(serviceListener);
+ assertTrue(dispatcher.isShutdown());
+ dispatcher.awaitTermination(2, TimeUnit.SECONDS);
+ assertTrue(dispatcher.isTerminated());
+
+
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/permissionadmin/PermissionAdminTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/permissionadmin/PermissionAdminTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/permissionadmin/PermissionAdminTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/permissionadmin/PermissionAdminTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,126 @@
+/**
+ * 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.permissionadmin;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.osgi.jmx.service.permissionadmin.PermissionAdminMBean;
+import org.osgi.service.permissionadmin.PermissionInfo;
+
+/**
+ * {@link PermissionAdminMBean} test case.
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class PermissionAdminTest {
+
+ @Mock
+ private org.osgi.service.permissionadmin.PermissionAdmin permAdmin;
+ private PermissionAdminMBean mbean;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mbean = new PermissionAdmin(permAdmin);
+ }
+
+ @Test
+ public void testGetPermissions() throws IOException {
+ PermissionInfo info = new PermissionInfo("Admin", "test", "get");
+ PermissionInfo[] permInfos = new PermissionInfo[] { info, info };
+
+ Mockito.when(permAdmin.getPermissions(Mockito.anyString())).thenReturn(permInfos);
+ String[] permissions = mbean.getPermissions("test");
+
+ Assert.assertNotNull(permissions);
+ Assert.assertEquals(2, permissions.length);
+ Assert.assertArrayEquals("Checks encoded permissions", new String[] { info.getEncoded(), info.getEncoded() },
+ permissions);
+
+ Mockito.reset(permAdmin);
+ Mockito.when(permAdmin.getPermissions(Mockito.anyString())).thenReturn(null);
+ String[] permissions2 = mbean.getPermissions("test");
+
+ Assert.assertNull(permissions2);
+ }
+
+ @Test
+ public void testListDefaultPermissions() throws IOException {
+ PermissionInfo info = new PermissionInfo("Admin", "test", "get");
+ PermissionInfo[] permInfos = new PermissionInfo[] { info, info };
+
+ Mockito.when(permAdmin.getDefaultPermissions()).thenReturn(permInfos);
+ String[] permissions = mbean.listDefaultPermissions();
+
+ Assert.assertNotNull(permissions);
+ Assert.assertEquals(2, permissions.length);
+ Assert.assertArrayEquals("Checks encoded default permissions", new String[] { info.getEncoded(), info.getEncoded() },
+ permissions);
+
+ Mockito.reset(permAdmin);
+ Mockito.when(permAdmin.getDefaultPermissions()).thenReturn(null);
+ String[] permissions2 = mbean.listDefaultPermissions();
+
+ Assert.assertNull(permissions2);
+ }
+
+ @Test
+ public void testListLocations() throws IOException {
+ String[] locations1 = new String[] { "test1", "test2" };
+ Mockito.when(permAdmin.getLocations()).thenReturn(locations1);
+ String[] locations2 = mbean.listLocations();
+ Assert.assertNotNull(locations2);
+ Assert.assertEquals(2, locations2.length);
+ Assert.assertSame(locations1, locations2);
+ }
+
+ @Test
+ public void testSetDefaultPermissions() throws IOException {
+ PermissionInfo info1 = new PermissionInfo("Admin", "test", "get");
+ PermissionInfo info2 = new PermissionInfo("Admin", "test2", "get");
+ PermissionInfo[] permInfos = new PermissionInfo[] { info1, info2 };
+ String[] encodedPermissions = new String[2];
+ int i = 0;
+ for (PermissionInfo info : permInfos) {
+ encodedPermissions[i++] = info.getEncoded();
+ }
+ mbean.setDefaultPermissions(encodedPermissions);
+ Mockito.verify(permAdmin).setDefaultPermissions(permInfos);
+ }
+
+ @Test
+ public void testSetPermissions() throws IOException {
+ PermissionInfo info1 = new PermissionInfo("Admin", "test", "set");
+ PermissionInfo info2 = new PermissionInfo("Admin", "test2", "set");
+ PermissionInfo[] permInfos = new PermissionInfo[] { info1, info2 };
+ String[] encodedPermissions = new String[2];
+ int i = 0;
+ for (PermissionInfo info : permInfos) {
+ encodedPermissions[i++] = info.getEncoded();
+ }
+ mbean.setPermissions("test", encodedPermissions);
+ Mockito.verify(permAdmin).setPermissions("test", permInfos);
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceMBeanHandlerTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceMBeanHandlerTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceMBeanHandlerTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,53 @@
+/**
+ * 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.provisioning;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+
+import javax.management.StandardMBean;
+
+import org.apache.aries.jmx.Logger;
+import org.apache.aries.jmx.agent.JMXAgentContext;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.provisioning.ProvisioningService;
+
+/**
+ *
+ *
+ * @version $Rev: 897315 $ $Date: 2010-01-08 20:13:13 +0000 (Fri, 08 Jan 2010) $
+ */
+public class ProvisioningServiceMBeanHandlerTest {
+
+
+ @Test
+ public void testConstructInjectMBean() {
+
+ BundleContext bundleContext = mock(BundleContext.class);
+ Logger agentLogger = mock(Logger.class);
+ JMXAgentContext agentContext = new JMXAgentContext(bundleContext, null, agentLogger);
+ ProvisioningService provService = mock(ProvisioningService.class);
+
+ ProvisioningServiceMBeanHandler handler = new ProvisioningServiceMBeanHandler(agentContext);
+ StandardMBean mbean = handler.constructInjectMBean(provService);
+ assertNotNull(mbean);
+
+ }
+
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/provisioning/ProvisioningServiceTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,155 @@
+/**
+ * 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.provisioning;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.osgi.jmx.JmxConstants.PROPERTIES_TYPE;
+import static org.osgi.service.provisioning.ProvisioningService.PROVISIONING_AGENT_CONFIG;
+import static org.osgi.service.provisioning.ProvisioningService.PROVISIONING_REFERENCE;
+import static org.osgi.service.provisioning.ProvisioningService.PROVISIONING_RSH_SECRET;
+import static org.osgi.service.provisioning.ProvisioningService.PROVISIONING_SPID;
+import static org.osgi.service.provisioning.ProvisioningService.PROVISIONING_UPDATE_COUNT;
+
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.zip.ZipInputStream;
+
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.apache.aries.jmx.codec.PropertyData;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ *
+ *
+ * @version $Rev: 922888 $ $Date: 2010-03-14 16:31:18 +0000 (Sun, 14 Mar 2010) $
+ */
+public class ProvisioningServiceTest {
+
+
+ @Test
+ public void testAddInformationFromZip() throws Exception {
+
+ org.osgi.service.provisioning.ProvisioningService provService = mock(org.osgi.service.provisioning.ProvisioningService.class);
+ ProvisioningService mbean = new ProvisioningService(provService);
+ ProvisioningService spiedMBean = spy(mbean);
+
+ InputStream is = mock(InputStream.class);
+ doReturn(is).when(spiedMBean).createStream("file://prov.zip");
+
+ spiedMBean.addInformationFromZip("file://prov.zip");
+ verify(provService).addInformation(any(ZipInputStream.class));
+ verify(is).close();
+
+ }
+
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testAddInformationWithTabularData() throws Exception {
+
+ org.osgi.service.provisioning.ProvisioningService provService = mock(org.osgi.service.provisioning.ProvisioningService.class);
+ ProvisioningService mbean = new ProvisioningService(provService);
+
+ TabularData data = new TabularDataSupport(PROPERTIES_TYPE);
+ PropertyData<byte[]> p1 = PropertyData.newInstance(PROVISIONING_AGENT_CONFIG, new byte[] { 20, 30, 40 });
+ data.put(p1.toCompositeData());
+ PropertyData<String> p2 = PropertyData.newInstance(PROVISIONING_SPID, "x.test");
+ data.put(p2.toCompositeData());
+
+ mbean.addInformation(data);
+ ArgumentCaptor<Dictionary> dictionaryArgument = ArgumentCaptor.forClass(Dictionary.class);
+ verify(provService).addInformation(dictionaryArgument.capture());
+
+ Dictionary<String, Object> info = dictionaryArgument.getValue();
+ assertEquals(2, info.size() );
+ assertArrayEquals(new byte[] { 20, 30, 40 }, (byte[]) info.get(PROVISIONING_AGENT_CONFIG));
+ assertEquals("x.test", info.get(PROVISIONING_SPID));
+
+ }
+
+
+ @Test
+ public void testListInformation() throws Exception {
+
+ org.osgi.service.provisioning.ProvisioningService provService = mock(org.osgi.service.provisioning.ProvisioningService.class);
+ ProvisioningService mbean = new ProvisioningService(provService);
+
+ Dictionary<String, Object> info = new Hashtable<String, Object>();
+ info.put(PROVISIONING_AGENT_CONFIG, new byte[] { 20, 30, 40 });
+ info.put(PROVISIONING_SPID, "x.test");
+ info.put(PROVISIONING_REFERENCE, "rsh://0.0.0.0/provX");
+ info.put(PROVISIONING_RSH_SECRET, new byte[] { 15, 25, 35 });
+ info.put(PROVISIONING_UPDATE_COUNT, 1);
+
+ when(provService.getInformation()).thenReturn(info);
+
+ TabularData provData = mbean.listInformation();
+ assertNotNull(provData);
+ assertEquals(PROPERTIES_TYPE, provData.getTabularType());
+ assertEquals(5, provData.values().size());
+ PropertyData<byte[]> agentConfig = PropertyData.from(provData.get(new Object[]{ PROVISIONING_AGENT_CONFIG }));
+ assertArrayEquals(new byte[] { 20, 30, 40 }, agentConfig.getValue());
+ PropertyData<String> spid = PropertyData.from(provData.get(new Object[] { PROVISIONING_SPID }));
+ assertEquals("x.test", spid.getValue());
+ PropertyData<String> ref = PropertyData.from(provData.get(new Object[] { PROVISIONING_REFERENCE }));
+ assertEquals("rsh://0.0.0.0/provX", ref.getValue());
+ PropertyData<byte[]> sec = PropertyData.from(provData.get(new Object[] { PROVISIONING_RSH_SECRET }));
+ assertArrayEquals(new byte[] { 15, 25, 35 }, sec.getValue());
+ PropertyData<Integer> count = PropertyData.from(provData.get(new Object[] { PROVISIONING_UPDATE_COUNT }));
+ assertEquals(new Integer(1), count.getValue());
+
+ }
+
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testSetInformation() throws Exception {
+
+ org.osgi.service.provisioning.ProvisioningService provService = mock(org.osgi.service.provisioning.ProvisioningService.class);
+ ProvisioningService mbean = new ProvisioningService(provService);
+
+ TabularData data = new TabularDataSupport(PROPERTIES_TYPE);
+ PropertyData<String> p1 = PropertyData.newInstance(PROVISIONING_REFERENCE, "rsh://0.0.0.0/provX");
+ data.put(p1.toCompositeData());
+ PropertyData<String> p2 = PropertyData.newInstance(PROVISIONING_SPID, "x.test");
+ data.put(p2.toCompositeData());
+
+ mbean.setInformation(data);
+
+ ArgumentCaptor<Dictionary> dictionaryArgument = ArgumentCaptor.forClass(Dictionary.class);
+ verify(provService).setInformation(dictionaryArgument.capture());
+
+ Dictionary<String, Object> info = dictionaryArgument.getValue();
+ assertEquals(2, info.size() );
+ assertEquals("rsh://0.0.0.0/provX", info.get(PROVISIONING_REFERENCE));
+ assertEquals("x.test", info.get(PROVISIONING_SPID));
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/useradmin/UserAdminTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/useradmin/UserAdminTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/useradmin/UserAdminTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/useradmin/UserAdminTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,592 @@
+/**
+ * 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.useradmin;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.TabularData;
+
+import org.apache.aries.jmx.codec.AuthorizationData;
+import org.apache.aries.jmx.codec.GroupData;
+import org.apache.aries.jmx.codec.RoleData;
+import org.apache.aries.jmx.codec.UserData;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.osgi.jmx.JmxConstants;
+import org.osgi.service.useradmin.Authorization;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+
+/**
+ * UserAdminMBean test case.
+ *
+ * @version $Rev: 920467 $ $Date: 2010-03-08 19:22:22 +0000 (Mon, 08 Mar 2010) $
+ */
+public class UserAdminTest {
+
+ @Mock
+ private org.osgi.service.useradmin.UserAdmin userAdmin;
+ private UserAdmin mbean;
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ mbean = new UserAdmin(userAdmin);
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#addCredential(java.lang.String, byte[], java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddCredential() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> credentials = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getCredentials()).thenReturn(credentials);
+ mbean.addCredential("password", new byte[] { 1, 2 }, "user1");
+ Assert.assertArrayEquals(new byte[] { 1, 2 }, (byte[]) credentials.get("password"));
+
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#addCredentialString(String, String, String)}
+ * .
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddCredentialString() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> credentials = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getCredentials()).thenReturn(credentials);
+ mbean.addCredentialString("password", "1234", "user1");
+ Assert.assertEquals("1234", (String) credentials.get("password"));
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#addMember(java.lang.String, java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddMember() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(userAdmin.getRole("group1")).thenReturn(group1);
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.addMember(user1)).thenReturn(true);
+ boolean isAdded = mbean.addMember("group1", "user1");
+ Assert.assertTrue(isAdded);
+ Mockito.verify(group1).addMember(user1);
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#addPropertyString(String, String, String)}
+ * .
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddPropertyString() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getProperties()).thenReturn(props);
+ mbean.addPropertyString("key", "1234", "user1");
+ Assert.assertEquals("1234", (String) props.get("key"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#addProperty(java.lang.String, byte[], java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddProperty() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getProperties()).thenReturn(props);
+ mbean.addProperty("key", new byte[] { 1, 2 }, "user1");
+ Assert.assertArrayEquals(new byte[] { 1, 2 }, (byte[]) props.get("key"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#addRequiredMember(java.lang.String, java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testAddRequiredMember() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(userAdmin.getRole("group1")).thenReturn(group1);
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.addRequiredMember(user1)).thenReturn(true);
+ boolean isAdded = mbean.addRequiredMember("group1", "user1");
+ Assert.assertTrue(isAdded);
+ Mockito.verify(group1).addRequiredMember(user1);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#createGroup(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testCreateGroup() throws IOException {
+ mbean.createGroup("group1");
+ Mockito.verify(userAdmin).createRole("group1", Role.GROUP);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#createRole(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testCreateRole() throws IOException {
+ mbean.createRole("role1");
+ Mockito.verify(userAdmin).createRole("role1", Role.ROLE);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#createUser(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testCreateUser() throws IOException {
+ mbean.createUser("user1");
+ Mockito.verify(userAdmin).createRole("user1", Role.USER);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getAuthorization(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetAuthorization() throws IOException {
+ Authorization auth = Mockito.mock(Authorization.class);
+ User user = Mockito.mock(User.class);
+ Mockito.when(user.getType()).thenReturn(Role.USER);
+ Mockito.when(userAdmin.getAuthorization(user)).thenReturn(auth);
+ Mockito.when(userAdmin.getRole("role1")).thenReturn(user);
+ Mockito.when(auth.getName()).thenReturn("auth1");
+ Mockito.when(auth.getRoles()).thenReturn(new String[]{"role1"});
+ CompositeData data = mbean.getAuthorization("role1");
+ Assert.assertNotNull(data);
+ AuthorizationData authData = AuthorizationData.from(data);
+ Assert.assertNotNull(authData);
+ Assert.assertEquals("auth1", authData.getName());
+ Assert.assertArrayEquals(new String[] { "role1" }, authData.getRoles());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getCredentials(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetCredentials() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> properties = new Hashtable<String, Object>();
+ properties.put("key", "value");
+ Mockito.when(user1.getCredentials()).thenReturn(properties);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(userAdmin.getRole(Mockito.anyString())).thenReturn(user1);
+ TabularData data = mbean.getCredentials("user1");
+ Assert.assertNotNull(data);
+ Assert.assertEquals(JmxConstants.PROPERTIES_TYPE, data.getTabularType());
+ CompositeData composite = data.get(new Object[] { "key" });
+ Assert.assertNotNull(composite);
+ Assert.assertEquals("key", (String) composite.get(JmxConstants.KEY));
+ Assert.assertEquals("value", (String) composite.get(JmxConstants.VALUE));
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getGroup(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetGroup() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.getName()).thenReturn("group1");
+ Role role1 = Mockito.mock(Role.class);
+ Mockito.when(role1.getName()).thenReturn("role1");
+ Role role2 = Mockito.mock(Role.class);
+ Mockito.when(role2.getName()).thenReturn("role2");
+ Mockito.when(group1.getRequiredMembers()).thenReturn(new Role[] { role1 });
+ Mockito.when(group1.getMembers()).thenReturn(new Role[] { role2 });
+ Mockito.when(userAdmin.getRole(Mockito.anyString())).thenReturn(group1);
+ CompositeData data = mbean.getGroup("group1");
+ Assert.assertNotNull(data);
+ GroupData group = GroupData.from(data);
+ Assert.assertNotNull(group);
+ Assert.assertEquals("group1", group.getName());
+ Assert.assertEquals(Role.GROUP, group.getType());
+ Assert.assertArrayEquals(new String[] { "role2" }, group.getMembers());
+ Assert.assertArrayEquals(new String[] { "role1" }, group.getRequiredMembers());
+ Mockito.verify(userAdmin).getRole(Mockito.anyString());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getGroups(java.lang.String)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetGroups() throws Exception {
+ Group group1 = Mockito.mock(Group.class);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.getName()).thenReturn("group1");
+ Mockito.when(userAdmin.getRoles("name=group1")).thenReturn(new Role[] { group1 });
+ String[] groups = mbean.getGroups("name=group1");
+ Assert.assertArrayEquals(new String[] { "group1" }, groups);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getImpliedRoles(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetImpliedRoles() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Authorization auth = Mockito.mock(Authorization.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(auth.getRoles()).thenReturn(new String[] { "role1" });
+ Mockito.when(userAdmin.getRole("role1")).thenReturn(user1);
+ Mockito.when(userAdmin.getAuthorization(user1)).thenReturn(auth);
+ String[] roles = mbean.getImpliedRoles("role1");
+ Assert.assertArrayEquals(new String[] { "role1" }, roles);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getMembers(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetMembers() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.getName()).thenReturn("group1");
+ User user1 = Mockito.mock(Group.class);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(group1.getMembers()).thenReturn(new Role[] { user1 });
+ Mockito.when(userAdmin.getRole("group1")).thenReturn(group1);
+ String[] members = mbean.getMembers("group1");
+ Assert.assertArrayEquals(new String[] { "user1" }, members);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getProperties(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetProperties() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> properties = new Hashtable<String, Object>();
+ properties.put("key", "value");
+ Mockito.when(user1.getProperties()).thenReturn(properties);
+ Mockito.when(userAdmin.getRole(Mockito.anyString())).thenReturn(user1);
+ TabularData data = mbean.getProperties("user1");
+ Assert.assertNotNull(data);
+ Assert.assertEquals(JmxConstants.PROPERTIES_TYPE, data.getTabularType());
+ CompositeData composite = data.get(new Object[] { "key" });
+ Assert.assertNotNull(composite);
+ Assert.assertEquals("key", (String) composite.get(JmxConstants.KEY));
+ Assert.assertEquals("value", (String) composite.get(JmxConstants.VALUE));
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getRequiredMembers(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetRequiredMembers() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.getName()).thenReturn("group1");
+ User user1 = Mockito.mock(Group.class);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(group1.getRequiredMembers()).thenReturn(new Role[] { user1 });
+ Mockito.when(userAdmin.getRole("group1")).thenReturn(group1);
+ String[] members = mbean.getRequiredMembers("group1");
+ Assert.assertArrayEquals(new String[] { "user1" }, members);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getRole(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetRole() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(userAdmin.getRole(Mockito.anyString())).thenReturn(user1);
+ CompositeData data = mbean.getRole("user1");
+ Assert.assertNotNull(data);
+ RoleData role = RoleData.from(data);
+ Assert.assertNotNull(role);
+ Assert.assertEquals("user1", role.getName());
+ Assert.assertEquals(Role.USER, role.getType());
+ Mockito.verify(userAdmin).getRole(Mockito.anyString());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getRoles(java.lang.String)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetRoles() throws Exception {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(userAdmin.getRoles("name=user1")).thenReturn(new Role[] { user1 });
+ String[] roles = mbean.getRoles("name=user1");
+ Assert.assertArrayEquals(new String[] { "user1" }, roles);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getUser(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetUser() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(userAdmin.getRole(Mockito.anyString())).thenReturn(user1);
+ CompositeData data = mbean.getUser("user1");
+ Assert.assertNotNull(data);
+ UserData user = UserData.from(data);
+ Assert.assertNotNull(user);
+ Assert.assertEquals("user1", user.getName());
+ Assert.assertEquals(Role.USER, user.getType());
+ Mockito.verify(userAdmin).getRole(Mockito.anyString());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getUserWithProperty(String, String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testGetUserString() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(userAdmin.getUser("key", "valuetest")).thenReturn(user1);
+ String username = mbean.getUserWithProperty("key", "valuetest");
+ Assert.assertEquals(username, "user1");
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#getUsers(java.lang.String)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetUsers() throws Exception {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ Mockito.when(userAdmin.getRoles("name=user1")).thenReturn(new Role[] { user1 });
+ String[] roles = mbean.getUsers("name=user1");
+ Assert.assertArrayEquals(new String[] { "user1" }, roles);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#listGroups()}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testListGroups() throws Exception {
+ Group group1 = Mockito.mock(Group.class);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.getName()).thenReturn("group1");
+ Group group2 = Mockito.mock(Group.class);
+ Mockito.when(group2.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group2.getName()).thenReturn("group2");
+ Mockito.when(userAdmin.getRoles(null)).thenReturn(new Role[] { group1, group2 });
+ String[] groups = mbean.listGroups();
+ Assert.assertArrayEquals(new String[] { "group1", "group2" }, groups);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#listRoles()}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testListRoles() throws Exception {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ User user2 = Mockito.mock(User.class);
+ Mockito.when(user2.getType()).thenReturn(Role.USER);
+ Mockito.when(user2.getName()).thenReturn("user2");
+ Mockito.when(userAdmin.getRoles(null)).thenReturn(new Role[] { user1, user2 });
+ String[] roles = mbean.listRoles();
+ Assert.assertArrayEquals(new String[] { "user1", "user2" }, roles);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#listUsers()}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testListUsers() throws Exception {
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getName()).thenReturn("user1");
+ User user2 = Mockito.mock(User.class);
+ Mockito.when(user2.getType()).thenReturn(Role.USER);
+ Mockito.when(user2.getName()).thenReturn("user2");
+ Mockito.when(userAdmin.getRoles(null)).thenReturn(new Role[] { user1, user2 });
+ String[] roles = mbean.listUsers();
+ Assert.assertArrayEquals(new String[] { "user1", "user2" }, roles);
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#removeCredential(java.lang.String, java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveCredential() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> cred = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getCredentials()).thenReturn(cred);
+ mbean.removeCredential("key", "user1");
+ Assert.assertEquals(0, cred.size());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#removeGroup(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveGroup() throws IOException {
+ Mockito.when(userAdmin.removeRole("group1")).thenReturn(true);
+ boolean isRemoved = mbean.removeGroup("group1");
+ Assert.assertTrue(isRemoved);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#removeMember(java.lang.String, java.lang.String)}
+ * .
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveMember() throws IOException {
+ Group group1 = Mockito.mock(Group.class);
+ User user1 = Mockito.mock(User.class);
+ Mockito.when(userAdmin.getRole("group1")).thenReturn(group1);
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(group1.getType()).thenReturn(Role.GROUP);
+ Mockito.when(group1.removeMember(user1)).thenReturn(true);
+ boolean isAdded = mbean.removeMember("group1", "user1");
+ Assert.assertTrue(isAdded);
+ Mockito.verify(group1).removeMember(user1);
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.aries.jmx.useradmin.UserAdmin#removeProperty(java.lang.String, java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveProperty() throws IOException {
+ User user1 = Mockito.mock(User.class);
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ Mockito.when(userAdmin.getRole("user1")).thenReturn(user1);
+ Mockito.when(user1.getType()).thenReturn(Role.USER);
+ Mockito.when(user1.getProperties()).thenReturn(props);
+ mbean.removeProperty("key", "user1");
+ Assert.assertEquals(0, props.size());
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#removeRole(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveRole() throws IOException {
+ Mockito.when(userAdmin.removeRole("role1")).thenReturn(true);
+ boolean isRemoved = mbean.removeRole("role1");
+ Assert.assertTrue(isRemoved);
+ }
+
+ /**
+ * Test method for {@link org.apache.aries.jmx.useradmin.UserAdmin#removeUser(java.lang.String)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRemoveUser() throws IOException {
+ Mockito.when(userAdmin.removeRole("user1")).thenReturn(true);
+ boolean isRemoved = mbean.removeUser("user1");
+ Assert.assertTrue(isRemoved);
+ }
+
+}