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 [9/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/src...
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,194 @@
+/**
+ * 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.util;
+
+import static org.osgi.jmx.JmxConstants.BIGDECIMAL;
+import static org.osgi.jmx.JmxConstants.BIGINTEGER;
+import static org.osgi.jmx.JmxConstants.BOOLEAN;
+import static org.osgi.jmx.JmxConstants.BYTE;
+import static org.osgi.jmx.JmxConstants.CHARACTER;
+import static org.osgi.jmx.JmxConstants.DOUBLE;
+import static org.osgi.jmx.JmxConstants.FLOAT;
+import static org.osgi.jmx.JmxConstants.INTEGER;
+import static org.osgi.jmx.JmxConstants.LONG;
+import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
+import static org.osgi.jmx.JmxConstants.P_BYTE;
+import static org.osgi.jmx.JmxConstants.P_CHAR;
+import static org.osgi.jmx.JmxConstants.P_DOUBLE;
+import static org.osgi.jmx.JmxConstants.P_FLOAT;
+import static org.osgi.jmx.JmxConstants.P_INT;
+import static org.osgi.jmx.JmxConstants.P_LONG;
+import static org.osgi.jmx.JmxConstants.P_SHORT;
+import static org.osgi.jmx.JmxConstants.SHORT;
+import static org.osgi.jmx.JmxConstants.STRING;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class provides common utilities related to type conversions for the MBean implementations
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class TypeUtils {
+
+ private TypeUtils() {
+ super();
+ }
+
+ public static Map<String, Class<? extends Object>> primitiveTypes = new HashMap<String, Class<? extends Object>>();
+ public static Map<String, Class<? extends Object>> wrapperTypes = new HashMap<String, Class<? extends Object>>();
+ public static Map<String, Class<? extends Object>> mathTypes = new HashMap<String, Class<? extends Object>>();
+ public static Map<Class<? extends Object>, Class<? extends Object>> primitiveToWrapper = new HashMap<Class<? extends Object>, Class<? extends Object>>();
+ public static Map<String, Class<? extends Object>> types = new HashMap<String, Class<? extends Object>>();
+
+ static {
+ primitiveTypes.put(P_FLOAT, Float.TYPE);
+ primitiveTypes.put(P_INT, Integer.TYPE);
+ primitiveTypes.put(P_LONG, Long.TYPE);
+ primitiveTypes.put(P_DOUBLE, Double.TYPE);
+ primitiveTypes.put(P_BYTE, Byte.TYPE);
+ primitiveTypes.put(P_SHORT, Short.TYPE);
+ primitiveTypes.put(P_CHAR, Character.TYPE);
+ primitiveTypes.put(P_BOOLEAN, Boolean.TYPE);
+ primitiveToWrapper.put(Float.TYPE, Float.class);
+ primitiveToWrapper.put(Integer.TYPE, Integer.class);
+ primitiveToWrapper.put(Long.TYPE, Long.class);
+ primitiveToWrapper.put(Double.TYPE, Double.class);
+ primitiveToWrapper.put(Byte.TYPE, Byte.class);
+ primitiveToWrapper.put(Short.TYPE, Short.class);
+ primitiveToWrapper.put(Boolean.TYPE, Boolean.class);
+ wrapperTypes.put(INTEGER, Integer.class);
+ wrapperTypes.put(FLOAT, Float.class);
+ wrapperTypes.put(LONG, Long.class);
+ wrapperTypes.put(DOUBLE, Double.class);
+ wrapperTypes.put(BYTE, Byte.class);
+ wrapperTypes.put(SHORT, Short.class);
+ wrapperTypes.put(BOOLEAN, Boolean.class);
+ wrapperTypes.put(CHARACTER, Character.class);
+ mathTypes.put(BIGDECIMAL, BigDecimal.class);
+ mathTypes.put(BIGINTEGER, BigInteger.class);
+ types.put(STRING, String.class);
+ types.putAll(primitiveTypes);
+ types.putAll(wrapperTypes);
+ types.putAll(mathTypes);
+ }
+
+ /**
+ * Converts a <code>Dictionary</code> object to a <code>Map</code>
+ *
+ * @param dictionary
+ * @return
+ */
+ public static Map<String, String> fromDictionary(Dictionary<String, String> dictionary) {
+ Map<String, String> result = new HashMap<String, String>();
+ Enumeration<String> keys = dictionary.keys();
+ while (keys.hasMoreElements()) {
+ String key = keys.nextElement();
+ result.put(key, dictionary.get(key));
+ }
+ return result;
+ }
+
+ /**
+ * Converts primitive long[] array to Long[]
+ *
+ * @param array
+ * @return
+ */
+ public static Long[] toLong(long[] array) {
+ Long[] toArray = (array == null) ? new Long[0] : new Long[array.length];
+ for (int i = 0; i < toArray.length; i++) {
+ toArray[i] = array[i];
+ }
+ return toArray;
+ }
+
+ /**
+ * Converts Long[] array to primitive
+ *
+ * @param array
+ * @return
+ */
+ public static long[] toPrimitive(Long[] array) {
+ long[] toArray = (array == null) ? new long[0] : new long[array.length];
+ for (int i = 0; i < toArray.length; i++) {
+ toArray[i] = array[i];
+ }
+ return toArray;
+ }
+
+ /**
+ * Converts a String value to an Object of the specified type
+ *
+ * @param type
+ * one of types listed in {@link #types}
+ * @param value
+ * @return instance of class <code>type</code>
+ * @throws IllegalArgumentException
+ * if type or value are null or if the Class type does not support a valueOf() or cannot be converted to
+ * a wrapper type
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T fromString(Class<T> type, String value) {
+ if (type == null || !types.containsValue(type)) {
+ throw new IllegalArgumentException("Cannot convert to type argument : " + type);
+ }
+ if (value == null || value.length() < 1) {
+ throw new IllegalArgumentException("Argument value cannot be null or empty");
+ }
+ T result = null;
+ try {
+ if (type.equals(String.class)) {
+ result = (T) value;
+ } else if (type.equals(Character.class) || type.equals(Character.TYPE)) {
+ result = (T) Character.valueOf(value.charAt(0));
+ } else if (wrapperTypes.containsValue(type) || mathTypes.containsValue(type)) {
+ Constructor<? extends Object> constructor = type.getConstructor(String.class);
+ result = (T) constructor.newInstance(value);
+ } else if (primitiveToWrapper.containsKey(type)) { // attempt to promote to wrapper and resolve to the base
+ // type
+ Class<? extends Object> promotedType = primitiveToWrapper.get(type);
+ char[] simpleTypeName = type.getName().toCharArray();
+ simpleTypeName[0] = Character.toUpperCase(simpleTypeName[0]);
+ String parseMethodName = "parse" + new String(simpleTypeName);
+ Method parseMethod = promotedType.getDeclaredMethod(parseMethodName, String.class);
+ result = (T) parseMethod.invoke(null, value);
+ }
+ } catch (SecurityException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]", e);
+ }
+ return result;
+ }
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,137 @@
+/**
+ * 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;
+
+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 java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import javax.management.StandardMBean;
+
+import org.apache.aries.jmx.agent.JMXAgentContext;
+import org.junit.After;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+
+public class CompendiumHandlerTest {
+
+ protected AbstractCompendiumHandler target;
+
+ @After
+ public void tearDown(){
+ target = null;
+ }
+
+
+ @Test
+ public void testAddingServiceWillInitiateMBeanRegistration() throws Exception {
+
+ Object service = new Object();
+
+ ServiceReference reference = mock(ServiceReference.class);
+ BundleContext bundleContext = mock(BundleContext.class);
+ when(bundleContext.getService(reference)).thenReturn(service);
+
+ Logger agentLogger = mock(Logger.class);
+ JMXAgentContext agentContext = mock(JMXAgentContext.class);
+ when(agentContext.getBundleContext()).thenReturn(bundleContext);
+ when(agentContext.getLogger()).thenReturn(agentLogger);
+ ExecutorService executor = Executors.newSingleThreadExecutor();
+ when(agentContext.getRegistrationExecutor()).thenReturn(executor);
+
+ AbstractCompendiumHandler concreteHandler = new CompendiumHandler(agentContext, "org.osgi.service.Xxx");
+ target = spy(concreteHandler);
+
+ target.addingService(reference);
+
+ executor.shutdown();
+ executor.awaitTermination(2, TimeUnit.SECONDS);
+
+ //service only got once
+ verify(bundleContext).getService(reference);
+ //template method is invoked
+ verify(target).constructInjectMBean(service);
+ //registration is invoked on context
+ verify(agentContext).registerMBean(target);
+
+ }
+
+ @Test
+ public void testRemovedServiceWillUnregisterMBean() throws Exception{
+
+ Object service = new Object();
+ ServiceReference reference = mock(ServiceReference.class);
+
+ BundleContext bundleContext = mock(BundleContext.class);
+ Logger agentLogger = mock(Logger.class);
+ JMXAgentContext agentContext = mock(JMXAgentContext.class);
+ when(agentContext.getBundleContext()).thenReturn(bundleContext);
+ when(agentContext.getLogger()).thenReturn(agentLogger);
+ ExecutorService executor = Executors.newSingleThreadExecutor();
+ when(agentContext.getRegistrationExecutor()).thenReturn(executor);
+
+ AbstractCompendiumHandler concreteHandler = new CompendiumHandler(agentContext, "org.osgi.service.Xxx");
+ target = spy(concreteHandler);
+
+ String name = "osgi.compendium:service=xxx,version=1.0";
+ doReturn(name).when(target).getName();
+
+ target.removedService(reference, service);
+
+ executor.shutdown();
+ executor.awaitTermination(2, TimeUnit.SECONDS);
+
+ //service unget
+ verify(bundleContext).ungetService(reference);
+ //unregister is invoked on context
+ verify(agentContext).unregisterMBean(name);
+
+ }
+
+
+
+ /*
+ * Concrete implementation used for test
+ */
+ class CompendiumHandler extends AbstractCompendiumHandler {
+
+ protected CompendiumHandler(JMXAgentContext agentContext, Filter filter) {
+ super(agentContext, filter);
+ }
+
+ protected CompendiumHandler(JMXAgentContext agentContext, String clazz) {
+ super(agentContext, clazz);
+ }
+
+ protected StandardMBean constructInjectMBean(Object targetService) {
+ return null;
+ }
+
+ public String getName() {
+ return null;
+ }
+
+ }
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandlerTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandlerTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandlerTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,52 @@
+/**
+ * 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.cm;
+
+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.cm.ConfigurationAdmin;
+
+/**
+ *
+ *
+ * @version $Rev: 897317 $ $Date: 2010-01-08 20:16:28 +0000 (Fri, 08 Jan 2010) $
+ */
+public class ConfigurationAdminMBeanHandlerTest {
+
+
+ @Test
+ public void testConstructInjectMBean() {
+
+ BundleContext bundleContext = mock(BundleContext.class);
+ Logger agentLogger = mock(Logger.class);
+ JMXAgentContext agentContext = new JMXAgentContext(bundleContext, null, agentLogger);
+ ConfigurationAdmin cAdmin = mock(ConfigurationAdmin.class);
+
+ ConfigurationAdminMBeanHandler handler = new ConfigurationAdminMBeanHandler(agentContext);
+ StandardMBean mbean = handler.constructInjectMBean(cAdmin);
+ assertNotNull(mbean);
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/cm/ConfigurationAdminTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,247 @@
+/**
+ * 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.cm;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.osgi.jmx.JmxConstants.PROPERTIES_TYPE;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+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;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.Configuration;
+
+/**
+ *
+ *
+ * @version $Rev: 922888 $ $Date: 2010-03-14 16:31:18 +0000 (Sun, 14 Mar 2010) $
+ */
+public class ConfigurationAdminTest {
+
+
+ @Test
+ public void testCreateFactoryConfiguration() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String fpid = "org.apache.aries.jmx.mock.factory";
+ Configuration config = mock(Configuration.class);
+
+ when(admin.createFactoryConfiguration(eq(fpid), anyString())).thenReturn(config);
+ when(config.getPid()).thenReturn(fpid + "-1260133982371-0");
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ assertEquals(fpid + "-1260133982371-0", mbean.createFactoryConfiguration(fpid));
+ assertEquals(fpid + "-1260133982371-0", mbean.createFactoryConfigurationForLocation(fpid, "/bundlex"));
+
+ }
+
+
+ @Test
+ public void testDelete() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String pid = "org.apache.aries.jmx.mock";
+ Configuration config = mock(Configuration.class);
+
+ when(admin.getConfiguration(pid, null)).thenReturn(config);
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ mbean.delete(pid);
+ verify(config).delete();
+
+ reset(config);
+
+ when(admin.getConfiguration(pid, "location")).thenReturn(config);
+ mbean.deleteForLocation(pid, "location");
+ verify(config).delete();
+
+ }
+
+
+ @Test
+ public void testDeleteConfigurations() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String filter = "(" + Constants.SERVICE_PID + "=org.apache.aries.jmx.mock)";
+
+ Configuration a = mock(Configuration.class);
+ Configuration b = mock(Configuration.class);
+
+ when(admin.listConfigurations(filter)).thenReturn(new Configuration[] { a, b });
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ mbean.deleteConfigurations(filter);
+
+ verify(a).delete();
+ verify(b).delete();
+
+ }
+
+
+ @Test
+ public void testGetBundleLocation() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String pid = "org.apache.aries.jmx.mock";
+ Configuration config = mock(Configuration.class);
+
+ when(admin.getConfiguration(pid, null)).thenReturn(config);
+ when(config.getBundleLocation()).thenReturn("/location");
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+
+ assertEquals("/location", mbean.getBundleLocation(pid));
+
+ }
+
+
+ @Test
+ public void testGetConfigurations() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String factoryPid = "org.apache.aries.jmx.factory.mock";
+ String filter = "(" + org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID + "=org.apache.aries.jmx.factory.mock)";
+ String location = "../location";
+
+ Configuration a = mock(Configuration.class);
+ when(a.getPid()).thenReturn(factoryPid + "-2160133952674-0");
+ when(a.getBundleLocation()).thenReturn(location);
+ Configuration b = mock(Configuration.class);
+ when(b.getPid()).thenReturn(factoryPid + "-1260133982371-1");
+ when(b.getBundleLocation()).thenReturn(location);
+
+ when(admin.listConfigurations(filter)).thenReturn(new Configuration[] { a, b});
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ String[][] result = mbean.getConfigurations(filter);
+ assertEquals(2, result.length);
+ assertArrayEquals(new String[]{ factoryPid + "-2160133952674-0", location }, result[0] );
+ assertArrayEquals(new String[]{ factoryPid + "-1260133982371-1", location }, result[1] );
+
+ }
+
+
+ @Test
+ public void testGetFactoryPid() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String factoryPid = "org.apache.aries.jmx.factory.mock";
+
+ Configuration config = mock(Configuration.class);
+ when(admin.getConfiguration(eq(factoryPid + "-1260133982371-0"), anyString())).thenReturn(config);
+ when(config.getFactoryPid()).thenReturn(factoryPid);
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ assertEquals(factoryPid, mbean.getFactoryPid(factoryPid + "-1260133982371-0"));
+ assertEquals(factoryPid, mbean.getFactoryPidForLocation(factoryPid + "-1260133982371-0", "location"));
+
+ }
+
+
+ @Test
+ public void testGetProperties() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String pid = "org.apache.aries.jmx.mock";
+ Configuration config = mock(Configuration.class);
+
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put("one", "value");
+ props.put("two", 2);
+ when(admin.getConfiguration(eq(pid), anyString())).thenReturn(config);
+ when(config.getProperties()).thenReturn(props);
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+
+ TabularData properties = mbean.getPropertiesForLocation(pid, null);
+ assertNotNull(properties);
+ assertEquals(PROPERTIES_TYPE, properties.getTabularType());
+ assertEquals(2, properties.values().size());
+ PropertyData<? extends Object> oneData = PropertyData.from(properties.get(new Object[]{ "one"}));
+ assertEquals("value", oneData.getValue());
+ PropertyData<? extends Object> twoData = PropertyData.from(properties.get(new Object[]{ "two"}));
+ assertEquals(2, twoData.getValue());
+ assertEquals("2", twoData.getEncodedValue());
+
+ }
+
+
+
+ @Test
+ public void testSetBundleLocation() throws Exception {
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String pid = "org.apache.aries.jmx.mock";
+
+ Configuration config = mock(Configuration.class);
+ when(admin.getConfiguration(pid, null)).thenReturn(config);
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ mbean.setBundleLocation(pid, "file:/newlocation");
+
+ ArgumentCaptor<String> locationArgument = ArgumentCaptor.forClass(String.class);
+ verify(config).setBundleLocation(locationArgument.capture());
+
+ assertEquals("file:/newlocation", locationArgument.getValue());
+
+ }
+
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testUpdateTabularData() throws Exception {
+
+ TabularData data = new TabularDataSupport(PROPERTIES_TYPE);
+ PropertyData<String> p1 = PropertyData.newInstance("one", "first");
+ data.put(p1.toCompositeData());
+ PropertyData<Integer> p2 = PropertyData.newInstance("two", 3);
+ data.put(p2.toCompositeData());
+
+ org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
+ String pid = "org.apache.aries.jmx.mock";
+
+ Configuration config = mock(Configuration.class);
+ when(admin.getConfiguration(pid, null)).thenReturn(config);
+
+ ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
+ mbean.updateForLocation(pid, null, data);
+
+ ArgumentCaptor<Dictionary> props = ArgumentCaptor.forClass(Dictionary.class);
+ verify(config).update(props.capture());
+
+ Dictionary configProperties = props.getValue();
+ assertEquals("first", configProperties.get("one"));
+ assertEquals(3, configProperties.get("two"));
+
+ }
+
+
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleDataTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleDataTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleDataTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleDataTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,255 @@
+/**
+ * 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.codec;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.osgi.jmx.framework.BundleStateMBean.BUNDLES_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.BUNDLE_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.EXPORTED_PACKAGES;
+import static org.osgi.jmx.framework.BundleStateMBean.FRAGMENT;
+import static org.osgi.jmx.framework.BundleStateMBean.FRAGMENTS;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADERS;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADERS_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADER_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.HOSTS;
+import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.BundleStateMBean.IMPORTED_PACKAGES;
+import static org.osgi.jmx.framework.BundleStateMBean.KEY;
+import static org.osgi.jmx.framework.BundleStateMBean.LAST_MODIFIED;
+import static org.osgi.jmx.framework.BundleStateMBean.LOCATION;
+import static org.osgi.jmx.framework.BundleStateMBean.PERSISTENTLY_STARTED;
+import static org.osgi.jmx.framework.BundleStateMBean.REGISTERED_SERVICES;
+import static org.osgi.jmx.framework.BundleStateMBean.REMOVAL_PENDING;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRED;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRED_BUNDLES;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRING_BUNDLES;
+import static org.osgi.jmx.framework.BundleStateMBean.SERVICES_IN_USE;
+import static org.osgi.jmx.framework.BundleStateMBean.START_LEVEL;
+import static org.osgi.jmx.framework.BundleStateMBean.STATE;
+import static org.osgi.jmx.framework.BundleStateMBean.SYMBOLIC_NAME;
+import static org.osgi.jmx.framework.BundleStateMBean.VALUE;
+import static org.osgi.jmx.framework.BundleStateMBean.VERSION;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.apache.aries.jmx.codec.BundleData.Header;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.packageadmin.RequiredBundle;
+import org.osgi.service.startlevel.StartLevel;
+
+/**
+ *
+ *
+ * @version $Rev: 922888 $ $Date: 2010-03-14 16:31:18 +0000 (Sun, 14 Mar 2010) $
+ */
+public class BundleDataTest {
+
+
+ @Test
+ public void testToCompositeData() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ BundleContext context = mock(BundleContext.class);
+ PackageAdmin packageAdmin = mock(PackageAdmin.class);
+ StartLevel startLevel = mock(StartLevel.class);
+
+ Bundle b1 = mock(Bundle.class);
+ when(b1.getSymbolicName()).thenReturn("b1");
+ when(b1.getBundleId()).thenReturn(new Long(44));
+ Bundle b2 = mock(Bundle.class);
+ when(b2.getSymbolicName()).thenReturn("b2");
+ when(b2.getBundleId()).thenReturn(new Long(55));
+ Bundle b3 = mock(Bundle.class);
+ when(b3.getSymbolicName()).thenReturn("b3");
+ when(b3.getBundleId()).thenReturn(new Long(66));
+ when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2, b3 });
+
+ when(bundle.getSymbolicName()).thenReturn("test");
+ when(bundle.getVersion()).thenReturn(Version.emptyVersion);
+ when(bundle.getBundleId()).thenReturn(new Long(1));
+ when(bundle.getLastModified()).thenReturn(new Long(12345));
+ when(bundle.getLocation()).thenReturn("location");
+
+ //headers
+ Dictionary<String, String> headers = new Hashtable<String, String>();
+ headers.put(Constants.BUNDLE_SYMBOLICNAME, "test");
+ headers.put(Constants.BUNDLE_VERSION, "0.0.0");
+ when(bundle.getHeaders()).thenReturn(headers);
+
+ //exported packages
+ ExportedPackage exported = mock(ExportedPackage.class);
+ when(exported.getName()).thenReturn("org.apache.aries.jmx");
+ when(exported.getVersion()).thenReturn(new Version("1.0.0"));
+ when(packageAdmin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[] { exported });
+
+ //imported packages
+ ExportedPackage ep1 = mock(ExportedPackage.class);
+ when(ep1.getImportingBundles()).thenReturn(new Bundle[] { bundle, b2, b3 });
+ when(ep1.getName()).thenReturn("org.apache.aries.jmx.b1");
+ when(ep1.getVersion()).thenReturn(Version.emptyVersion);
+ ExportedPackage ep2 = mock(ExportedPackage.class);
+ when(ep2.getImportingBundles()).thenReturn(new Bundle[] { bundle, b3 });
+ when(ep2.getName()).thenReturn("org.apache.aries.jmx.b2");
+ when(ep2.getVersion()).thenReturn(Version.parseVersion("2.0.1"));
+ headers.put(Constants.DYNAMICIMPORT_PACKAGE, "*");
+
+ when(packageAdmin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] { ep1 });
+ when(packageAdmin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] { ep2 });
+ when(packageAdmin.getExportedPackages(b3)).thenReturn(null);
+
+ //required bundles
+ RequiredBundle rb1 = mock(RequiredBundle.class);
+ when(rb1.getBundle()).thenReturn(b1);
+ when(rb1.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b2 });
+ RequiredBundle rb2 = mock(RequiredBundle.class);
+ when(rb2.getBundle()).thenReturn(b2);
+ when(rb2.getRequiringBundles()).thenReturn(new Bundle[] { b1 });
+ RequiredBundle rb3 = mock(RequiredBundle.class);
+ when(rb3.getBundle()).thenReturn(b3);
+ when(rb3.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b1, b2 });
+ headers.put(Constants.REQUIRE_BUNDLE, "b1;bundle-version=\"1.0.0\",b3;bundle-version=\"2.0.0\"");
+ when(packageAdmin.getRequiredBundles("b1")).thenReturn(new RequiredBundle[] { rb1 });
+ when(packageAdmin.getRequiredBundles("b2")).thenReturn(new RequiredBundle[] { rb2 });
+ when(packageAdmin.getRequiredBundles("b3")).thenReturn(new RequiredBundle[] { rb3 });
+
+ //services in use
+ ServiceReference s1 = mock(ServiceReference.class);
+ when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(15));
+ ServiceReference s2 = mock(ServiceReference.class);
+ when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(16));
+ ServiceReference s3 = mock(ServiceReference.class);
+ when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(17));
+
+ when(bundle.getServicesInUse()).thenReturn(new ServiceReference[] { s1, s2, s3 });
+
+ BundleData b = new BundleData(context, bundle, packageAdmin, startLevel);
+ CompositeData compositeData = b.toCompositeData();
+
+ assertEquals("test", compositeData.get(SYMBOLIC_NAME));
+ assertEquals("0.0.0", (String) compositeData.get(VERSION));
+ TabularData headerTable = (TabularData) compositeData.get(HEADERS);
+ assertEquals(4, headerTable.values().size());
+ CompositeData header = headerTable.get(new Object[]{Constants.BUNDLE_SYMBOLICNAME});
+ assertNotNull(header);
+ String value = (String) header.get(VALUE);
+ assertEquals("test", value);
+ String key = (String)header.get(KEY);
+ assertEquals(Constants.BUNDLE_SYMBOLICNAME, key);
+
+
+ TabularData bundleTable = new TabularDataSupport(BUNDLES_TYPE);
+ bundleTable.put(b.toCompositeData());
+
+ CompositeData bundleData = bundleTable.get(new Object[]{Long.valueOf(1)});
+ assertNotNull(bundleData);
+ String location = (String) bundleData.get(LOCATION);
+ assertEquals("location", location);
+
+ assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0"} , (String[]) compositeData.get(EXPORTED_PACKAGES));
+ assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0" , "org.apache.aries.jmx.b2;2.0.1"}, (String[]) compositeData.get(IMPORTED_PACKAGES));
+ assertArrayEquals(new Long[] { new Long(44), new Long(66) }, (Long[]) compositeData.get(REQUIRED_BUNDLES));
+ assertArrayEquals(new Long[] { new Long(15), new Long(16), new Long(17) },(Long[]) compositeData.get(SERVICES_IN_USE));
+ assertEquals("UNKNOWN", compositeData.get(STATE)); //default no return stub
+ assertEquals(0,((Long[]) compositeData.get(HOSTS)).length);
+ assertEquals(0, ((Long[]) compositeData.get(FRAGMENTS)).length);
+
+ }
+
+
+ @Test
+ public void testFromCompositeData() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(EXPORTED_PACKAGES, new String[] { "org.apache.aries.jmx;1.0.0"});
+ items.put(FRAGMENT, false);
+ items.put(FRAGMENTS, new Long[0]);
+ items.put(HOSTS, new Long[0]);
+ items.put(IDENTIFIER, new Long(3));
+ items.put(IMPORTED_PACKAGES, new String[] { "org.apache.aries.jmx.b1;0.0.0" , "org.apache.aries.jmx.b2;2.0.1"});
+ items.put(LAST_MODIFIED, new Long(8797));
+ items.put(LOCATION, "");
+ items.put(PERSISTENTLY_STARTED, false);
+ items.put(REGISTERED_SERVICES, new Long[0]);
+ items.put(REMOVAL_PENDING, false);
+ items.put(REQUIRED, true);
+ items.put(REQUIRED_BUNDLES, new Long[] { new Long(44), new Long(66) });
+ items.put(REQUIRING_BUNDLES, new Long[0]);
+ items.put(SERVICES_IN_USE, new Long[] { new Long(15), new Long(16), new Long(17) });
+ items.put(START_LEVEL, 1);
+ items.put(STATE, "ACTIVE");
+ items.put(SYMBOLIC_NAME, "test");
+ items.put(VERSION, "0.0.0");
+ TabularData headerTable = new TabularDataSupport(HEADERS_TYPE);
+ headerTable.put(new Header("a", "a").toCompositeData());
+ headerTable.put(new Header("b", "b").toCompositeData());
+ items.put(HEADERS, headerTable);
+ CompositeData compositeData = new CompositeDataSupport(BUNDLE_TYPE, items);
+
+ BundleData b = BundleData.from(compositeData);
+
+ assertEquals("test", b.getSymbolicName());
+ assertEquals("0.0.0", b.getVersion());
+ assertEquals(2, b.getHeaders().size());
+ assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0"} , b.getExportedPackages());
+ assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0" , "org.apache.aries.jmx.b2;2.0.1"}, b.getImportedPackages());
+ assertArrayEquals(new long[] { 44, 66 }, b.getRequiredBundles());
+ assertArrayEquals(new long[] { 15, 16, 17 }, b.getServicesInUse());
+ assertEquals("ACTIVE", b.getState()); //default no return stub
+ assertEquals(0, b.getHosts().length);
+ assertEquals(0, b.getFragments().length);
+ }
+
+ @Test
+ public void testHeaderToCompositeData() throws Exception{
+
+ Header h1 = new Header("a", "b");
+ CompositeData compositeData = h1.toCompositeData();
+
+ assertEquals("a", compositeData.get(KEY));
+ assertEquals("b", compositeData.get(VALUE));
+
+ }
+
+ @Test
+ public void testHeaderFromCompositeData() throws Exception {
+
+ CompositeData compositeData = new CompositeDataSupport(HEADER_TYPE, new String[] { KEY, VALUE } , new String [] { "c", "d" });
+ Header header = Header.from(compositeData);
+ assertEquals("c", header.getKey());
+ assertEquals("d", header.getValue());
+
+ }
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleEventDataTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleEventDataTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleEventDataTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/BundleEventDataTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,86 @@
+/**
+ * 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.codec;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+import static org.osgi.jmx.framework.BundleStateMBean.BUNDLE_EVENT_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.EVENT;
+import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.BundleStateMBean.LOCATION;
+import static org.osgi.jmx.framework.BundleStateMBean.SYMBOLIC_NAME;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+
+/**
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class BundleEventDataTest {
+
+
+ @Test
+ public void testToCompositeData() throws Exception {
+
+ BundleEvent event = mock(BundleEvent.class);
+ Bundle bundle = mock(Bundle.class);
+ when(event.getBundle()).thenReturn(bundle);
+ when(bundle.getSymbolicName()).thenReturn("test");
+ when(bundle.getBundleId()).thenReturn(new Long(4));
+ when(bundle.getLocation()).thenReturn("location");
+ when(event.getType()).thenReturn(BundleEvent.INSTALLED);
+
+ BundleEventData eventData = new BundleEventData(event);
+ CompositeData eventCompositeData = eventData.toCompositeData();
+
+ assertEquals(new Long(4), (Long) eventCompositeData.get(IDENTIFIER));
+ assertEquals("test", (String) eventCompositeData.get(SYMBOLIC_NAME));
+ assertEquals(new Integer(BundleEvent.INSTALLED), (Integer) eventCompositeData.get(EVENT));
+ assertEquals("location", (String) eventCompositeData.get(LOCATION));
+
+ }
+
+
+ @Test
+ public void testFrom() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(IDENTIFIER, new Long(7));
+ items.put(SYMBOLIC_NAME, "t");
+ items.put(LOCATION, "l");
+ items.put(EVENT, BundleEvent.RESOLVED);
+
+ CompositeData compositeData = new CompositeDataSupport(BUNDLE_EVENT_TYPE, items);
+ BundleEventData event = BundleEventData.from(compositeData);
+
+ assertEquals(7, event.getBundleId());
+ assertEquals("t", event.getBundleSymbolicName());
+ assertEquals("l", event.getLocation());
+ assertEquals(BundleEvent.RESOLVED, event.getEventType());
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/PropertyDataTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/PropertyDataTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/PropertyDataTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/PropertyDataTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,324 @@
+/**
+ * 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.codec;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.osgi.jmx.JmxConstants.BIGINTEGER;
+import static org.osgi.jmx.JmxConstants.BOOLEAN;
+import static org.osgi.jmx.JmxConstants.CHARACTER;
+import static org.osgi.jmx.JmxConstants.DOUBLE;
+import static org.osgi.jmx.JmxConstants.INTEGER;
+import static org.osgi.jmx.JmxConstants.KEY;
+import static org.osgi.jmx.JmxConstants.PROPERTY_TYPE;
+import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
+import static org.osgi.jmx.JmxConstants.P_CHAR;
+import static org.osgi.jmx.JmxConstants.P_DOUBLE;
+import static org.osgi.jmx.JmxConstants.P_INT;
+import static org.osgi.jmx.JmxConstants.STRING;
+import static org.osgi.jmx.JmxConstants.TYPE;
+import static org.osgi.jmx.JmxConstants.VALUE;
+
+import java.math.BigInteger;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+
+import org.junit.Test;
+
+/**
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class PropertyDataTest {
+
+
+ @Test
+ public void testToCompositeDataForPrimitiveTypes() throws Exception {
+
+ PropertyData<Integer> intData = PropertyData.newInstance("test", 1);
+ CompositeData intCData = intData.toCompositeData();
+ assertEquals("test", intCData.get(KEY));
+ assertEquals("1", intCData.get(VALUE));
+ assertEquals(P_INT, intCData.get(TYPE));
+
+ PropertyData<Double> doubleData = PropertyData.newInstance("test", 1.0);
+ CompositeData doubleCData = doubleData.toCompositeData();
+ assertEquals("test", doubleCData.get(KEY));
+ assertEquals("1.0", doubleCData.get(VALUE));
+ assertEquals(P_DOUBLE, doubleCData.get(TYPE));
+
+ PropertyData<Character> charData = PropertyData.newInstance("test", 'c');
+ CompositeData charCData = charData.toCompositeData();
+ assertEquals("test", charCData.get(KEY));
+ assertEquals("c", charCData.get(VALUE));
+ assertEquals(P_CHAR, charCData.get(TYPE));
+
+ PropertyData<Boolean> booleanData = PropertyData.newInstance("test", true);
+ CompositeData booleanCData = booleanData.toCompositeData();
+ assertEquals("test", booleanCData.get(KEY));
+ assertEquals("true", booleanCData.get(VALUE));
+ assertEquals(P_BOOLEAN, booleanCData.get(TYPE));
+ }
+
+ @Test
+ public void testFromCompositeDataForPrimitiveTypes() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(KEY, "key");
+ items.put(VALUE, "1");
+ items.put(TYPE, P_INT);
+ CompositeData compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Integer> intData = PropertyData.from(compositeData);
+ assertEquals("key", intData.getKey());
+ assertEquals(new Integer(1), intData.getValue());
+ assertEquals(P_INT, intData.getEncodedType());
+ assertTrue(intData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "1.0");
+ items.put(TYPE, P_DOUBLE);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Double> doubleData = PropertyData.from(compositeData);
+ assertEquals("key", doubleData.getKey());
+ assertEquals(Double.valueOf(1.0), doubleData.getValue());
+ assertEquals(P_DOUBLE, doubleData.getEncodedType());
+ assertTrue(doubleData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "a");
+ items.put(TYPE, P_CHAR);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Character> charData = PropertyData.from(compositeData);
+ assertEquals("key", charData.getKey());
+ assertEquals(Character.valueOf('a'), charData.getValue());
+ assertEquals(P_CHAR, charData.getEncodedType());
+ assertTrue(charData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "true");
+ items.put(TYPE, P_BOOLEAN);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Boolean> booleanData = PropertyData.from(compositeData);
+ assertEquals("key", booleanData.getKey());
+ assertTrue(booleanData.getValue());
+ assertEquals(P_BOOLEAN, booleanData.getEncodedType());
+ assertTrue(booleanData.isEncodingPrimitive());
+
+ }
+
+ @Test
+ public void testToCompositeDataForWrapperTypes() {
+
+ PropertyData<Integer> intData = PropertyData.newInstance("test", new Integer(1));
+ CompositeData intCData = intData.toCompositeData();
+ assertEquals("test", intCData.get(KEY));
+ assertEquals("1", intCData.get(VALUE));
+ assertEquals(INTEGER, intCData.get(TYPE));
+
+ PropertyData<Double> doubleData = PropertyData.newInstance("test", new Double(1.0));
+ CompositeData doubleCData = doubleData.toCompositeData();
+ assertEquals("test", doubleCData.get(KEY));
+ assertEquals("1.0", doubleCData.get(VALUE));
+ assertEquals(DOUBLE, doubleCData.get(TYPE));
+
+ PropertyData<Character> charData = PropertyData.newInstance("test", Character.valueOf('c'));
+ CompositeData charCData = charData.toCompositeData();
+ assertEquals("test", charCData.get(KEY));
+ assertEquals("c", charCData.get(VALUE));
+ assertEquals(CHARACTER, charCData.get(TYPE));
+
+ PropertyData<Boolean> booleanData = PropertyData.newInstance("test", Boolean.TRUE);
+ CompositeData booleanCData = booleanData.toCompositeData();
+ assertEquals("test", booleanCData.get(KEY));
+ assertEquals("true", booleanCData.get(VALUE));
+ assertEquals(BOOLEAN, booleanCData.get(TYPE));
+
+ }
+
+ @Test
+ public void testFromCompositeDataForWrapperTypes() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(KEY, "key");
+ items.put(VALUE, "1");
+ items.put(TYPE, INTEGER);
+ CompositeData compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Integer> intData = PropertyData.from(compositeData);
+ assertEquals("key", intData.getKey());
+ assertEquals(new Integer(1), intData.getValue());
+ assertEquals(INTEGER, intData.getEncodedType());
+ assertFalse(intData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "1.0");
+ items.put(TYPE, DOUBLE);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Double> doubleData = PropertyData.from(compositeData);
+ assertEquals("key", doubleData.getKey());
+ assertEquals(Double.valueOf(1.0), doubleData.getValue());
+ assertEquals(DOUBLE, doubleData.getEncodedType());
+ assertFalse(doubleData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "a");
+ items.put(TYPE, CHARACTER);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Character> charData = PropertyData.from(compositeData);
+ assertEquals("key", charData.getKey());
+ assertEquals(Character.valueOf('a'), charData.getValue());
+ assertEquals(CHARACTER, charData.getEncodedType());
+ assertFalse(charData.isEncodingPrimitive());
+
+ items.clear();
+ items.put(KEY, "key");
+ items.put(VALUE, "true");
+ items.put(TYPE, BOOLEAN);
+ compositeData = new CompositeDataSupport(PROPERTY_TYPE, items);
+
+ PropertyData<Boolean> booleanData = PropertyData.from(compositeData);
+ assertEquals("key", booleanData.getKey());
+ assertTrue(booleanData.getValue());
+ assertEquals(BOOLEAN, booleanData.getEncodedType());
+ assertFalse(booleanData.isEncodingPrimitive());
+
+ }
+
+ @Test
+ public void testToFromCompositeDataForAdditionalTypes() {
+
+ PropertyData<String> stringData = PropertyData.newInstance("test", "value");
+
+ CompositeData stringCData = stringData.toCompositeData();
+ assertEquals("test", stringCData.get(KEY));
+ assertEquals("value", stringCData.get(VALUE));
+ assertEquals(STRING, stringCData.get(TYPE));
+
+ stringData = PropertyData.from(stringCData);
+ assertEquals("test", stringData.getKey());
+ assertEquals("value", stringData.getValue());
+ assertEquals(STRING, stringData.getEncodedType());
+
+ PropertyData<BigInteger> bigIntData = PropertyData.newInstance("test", new BigInteger("1"));
+
+ CompositeData bigIntCData = bigIntData.toCompositeData();
+ assertEquals("test", bigIntCData.get(KEY));
+ assertEquals("1", bigIntCData.get(VALUE));
+ assertEquals(BIGINTEGER, bigIntCData.get(TYPE));
+
+ bigIntData = PropertyData.from(bigIntCData);
+ assertEquals("test", bigIntData.getKey());
+ assertEquals(new BigInteger("1"), bigIntData.getValue());
+ assertEquals(BIGINTEGER, bigIntData.getEncodedType());
+
+ }
+
+ @Test
+ public void testToFromCompositeDataForArrayTypes() {
+
+ //long[]
+ long[] primitiveLongValues = new long[] { 1, 2, 3 };
+ PropertyData<long[]> primitiveLongArrayData = PropertyData.newInstance("test", primitiveLongValues);
+ CompositeData primitiveLongArrayCData = primitiveLongArrayData.toCompositeData();
+ assertEquals("test", primitiveLongArrayCData.get(KEY));
+ assertEquals("1,2,3", primitiveLongArrayCData.get(VALUE));
+ assertEquals("Array of long", primitiveLongArrayCData.get(TYPE));
+ primitiveLongArrayData = PropertyData.from(primitiveLongArrayCData);
+ assertEquals("test", primitiveLongArrayData.getKey());
+ assertEquals("Array of long", primitiveLongArrayData.getEncodedType());
+ assertArrayEquals(primitiveLongValues, primitiveLongArrayData.getValue());
+
+ //Long[]
+ Long[] longValues = new Long[] { new Long(4), new Long(5), new Long(6) };
+ PropertyData<Long[]> longArrayData = PropertyData.newInstance("test", longValues);
+ CompositeData longArrayCData = longArrayData.toCompositeData();
+ assertEquals("test", longArrayCData.get(KEY));
+ assertEquals("4,5,6", longArrayCData.get(VALUE));
+ assertEquals("Array of Long", longArrayCData.get(TYPE));
+ longArrayData = PropertyData.from(longArrayCData);
+ assertEquals("test", longArrayData.getKey());
+ assertEquals("Array of Long", longArrayData.getEncodedType());
+ assertArrayEquals(longValues, longArrayData.getValue());
+
+ //char[]
+ char[] primitiveCharValues = new char[] { 'a', 'b', 'c' };
+ PropertyData<char[]> primitiveCharArrayData = PropertyData.newInstance("test", primitiveCharValues);
+ CompositeData primitiveCharArrayCData = primitiveCharArrayData.toCompositeData();
+ assertEquals("test", primitiveCharArrayCData.get(KEY));
+ assertEquals("a,b,c", primitiveCharArrayCData.get(VALUE));
+ assertEquals("Array of char", primitiveCharArrayCData.get(TYPE));
+ primitiveCharArrayData = PropertyData.from(primitiveCharArrayCData);
+ assertEquals("test", primitiveCharArrayData.getKey());
+ assertEquals("Array of char", primitiveCharArrayData.getEncodedType());
+ assertArrayEquals(primitiveCharValues, primitiveCharArrayData.getValue());
+
+ //Character[]
+ Character[] charValues = new Character[] { 'a', 'b', 'c' };
+ PropertyData<Character[]> charArrayData = PropertyData.newInstance("test", charValues);
+ CompositeData charArrayCData = charArrayData.toCompositeData();
+ assertEquals("test", charArrayCData.get(KEY));
+ assertEquals("a,b,c", charArrayCData.get(VALUE));
+ assertEquals("Array of Character", charArrayCData.get(TYPE));
+ charArrayData = PropertyData.from(charArrayCData);
+ assertEquals("test", charArrayData.getKey());
+ assertEquals("Array of Character", charArrayData.getEncodedType());
+ assertArrayEquals(charValues, charArrayData.getValue());
+
+ }
+
+ @Test
+ public void testToFromCompositeDataForVector() {
+
+ Vector<Long> vector = new Vector<Long>();
+ vector.add(new Long(40));
+ vector.add(new Long(50));
+ vector.add(new Long(60));
+
+ PropertyData<Vector<Long>> vectorPropertyData = PropertyData.newInstance("test", vector);
+ CompositeData vectorCompositeData = vectorPropertyData.toCompositeData();
+
+ assertEquals("test", vectorCompositeData.get(KEY));
+ assertEquals("40,50,60", vectorCompositeData.get(VALUE));
+ assertEquals("Vector of Long", vectorCompositeData.get(TYPE));
+
+ vectorPropertyData = PropertyData.from(vectorCompositeData);
+ assertEquals("test", vectorPropertyData.getKey());
+ assertEquals("Vector of Long", vectorPropertyData.getEncodedType());
+ assertArrayEquals(vector.toArray(new Long[vector.size()]), vectorPropertyData.getValue().toArray(new Long[vector.size()]));
+
+ }
+
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,151 @@
+/**
+ * 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.codec;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_IDENTIFIER;
+import static org.osgi.jmx.framework.ServiceStateMBean.OBJECT_CLASS;
+import static org.osgi.jmx.framework.ServiceStateMBean.SERVICE_TYPE;
+import static org.osgi.jmx.framework.ServiceStateMBean.USING_BUNDLES;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ *
+ * @version $Rev: 921523 $ $Date: 2010-03-10 19:29:59 +0000 (Wed, 10 Mar 2010) $
+ */
+public class ServiceDataTest {
+
+
+ @Test
+ public void testToCompositeData() throws Exception {
+
+ ServiceReference reference = mock(ServiceReference.class);
+ Bundle bundle = mock(Bundle.class);
+ String[] interfaces = new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" };
+
+ Bundle b1 = mock(Bundle.class);
+ when(b1.getBundleId()).thenReturn(new Long(6));
+ Bundle b2 = mock(Bundle.class);
+ when(b2.getBundleId()).thenReturn(new Long(9));
+
+
+ when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(98));
+ when(reference.getBundle()).thenReturn(bundle);
+ when(bundle.getBundleId()).thenReturn(new Long(34));
+ when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(interfaces);
+ when(reference.getUsingBundles()).thenReturn(new Bundle[] { b1, b2 });
+ when(reference.getPropertyKeys()).thenReturn( new String[] {"x.vendor", "x.domain", "x.index", "x.optimized" } );
+ when(reference.getProperty("x.vendor")).thenReturn("aries");
+ when(reference.getProperty("x.domain")).thenReturn("test");
+ when(reference.getProperty("x.index")).thenReturn(new Long(67));
+ when(reference.getProperty("x.optimized")).thenReturn(true);
+
+
+ ServiceData serviceData = new ServiceData(reference);
+
+ CompositeData compositeData = serviceData.toCompositeData();
+ assertEquals(new Long(98), compositeData.get(IDENTIFIER));
+ assertEquals(new Long(34), compositeData.get(BUNDLE_IDENTIFIER));
+ assertArrayEquals( new Long[] {new Long(6), new Long(9)}, (Long[]) compositeData.get(USING_BUNDLES));
+ assertArrayEquals(interfaces, (String[]) compositeData.get(OBJECT_CLASS));
+ // keep Properties for next version
+ //TabularData propertiesTable = (TabularData) compositeData.get(PROPERTIES);
+ //Collection<CompositeData> propertyData = (Collection<CompositeData>) propertiesTable.values();
+ //assertEquals(4, propertyData.size());
+ //for (CompositeData propertyRow: propertyData) {
+ // String key = (String) propertyRow.get(KEY);
+ // if (key.equals("x.vendor")) {
+ // assertEquals("aries", propertyRow.get(VALUE));
+ // assertEquals(STRING, propertyRow.get(TYPE));
+ // } else if (key.equals("x.domain")) {
+ // assertEquals("test", propertyRow.get(VALUE));
+ // assertEquals(STRING, propertyRow.get(TYPE));
+ // } else if (key.equals("x.index")) {
+ // assertEquals("67", propertyRow.get(VALUE));
+ // assertEquals(LONG, propertyRow.get(TYPE));
+ // } else if (key.equals("x.optimized")) {
+ // assertEquals("true", propertyRow.get(VALUE));
+ // assertEquals(BOOLEAN, propertyRow.get(TYPE));
+ // } else {
+ // fail("unknown key parsed from properties");
+ // }
+ //}
+ }
+
+
+ @Test
+ public void testFromCompositeData() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(IDENTIFIER, new Long(99));
+ items.put(BUNDLE_IDENTIFIER, new Long(5));
+ items.put(USING_BUNDLES, new Long[] { new Long(10), new Long(11) });
+ items.put(OBJECT_CLASS, new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" });
+ //TabularData propertyTable = new TabularDataSupport(PROPERTIES_TYPE);
+ //propertyTable.put(PropertyData.newInstance("a", true).toCompositeData());
+ //propertyTable.put(PropertyData.newInstance("b", "value").toCompositeData());
+ //propertyTable.put(PropertyData.newInstance("c", new int[] {1, 2}).toCompositeData());
+ //propertyTable.put(PropertyData.newInstance("d", new Long[] {new Long(3), new Long(4)}).toCompositeData());
+ //items.put(PROPERTIES, propertyTable);
+ CompositeData compositeData = new CompositeDataSupport(SERVICE_TYPE, items);
+
+ ServiceData data = ServiceData.from(compositeData);
+ assertEquals(99, data.getServiceId());
+ assertEquals(5, data.getBundleId());
+ assertArrayEquals(new long[] {10, 11}, data.getUsingBundles());
+ assertArrayEquals(new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" }, data.getServiceInterfaces());
+
+ //List<PropertyData<? extends Object>> properties = data.getProperties();
+ //assertEquals(4, properties.size());
+
+ //for (PropertyData<? extends Object> property: properties) {
+ // if (property.getKey().equals("a")) {
+ // assertTrue((Boolean) property.getValue());
+ // assertEquals(P_BOOLEAN, property.getEncodedType());
+ // } else if (property.getKey().equals("b")) {
+ // assertEquals("value", property.getValue());
+ // assertEquals(STRING, property.getEncodedType());
+ // } else if (property.getKey().equals("c")) {
+ // assertArrayEquals(new int[] { 1, 2 }, (int[]) property.getValue());
+ // assertEquals("Array of int", property.getEncodedType());
+ // assertEquals("1,2", property.getEncodedValue());
+ // } else if (property.getKey().equals("d")) {
+ // assertArrayEquals(new Long[] {new Long(3), new Long(4) }, (Long[]) property.getValue());
+ // assertEquals("Array of Long", property.getEncodedType());
+ // assertEquals("3,4", property.getEncodedValue());
+ // } else {
+ // fail("unknown key parsed from properties");
+ // }
+ //}
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceEventDataTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceEventDataTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceEventDataTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceEventDataTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,102 @@
+/**
+ * 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.codec;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+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.OBJECT_CLASS;
+import static org.osgi.jmx.framework.ServiceStateMBean.SERVICE_EVENT_TYPE;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+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 ServiceEventDataTest {
+
+
+ @Test
+ public void testToCompositeData() throws Exception {
+
+ ServiceEvent event = mock(ServiceEvent.class);
+ ServiceReference reference = mock(ServiceReference.class);
+ Bundle bundle = mock(Bundle.class);
+
+ when(event.getType()).thenReturn(ServiceEvent.REGISTERED);
+ when(event.getServiceReference()).thenReturn(reference);
+ when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
+ when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] {"org.apache.aries.jmx.Mock"});
+ when(reference.getBundle()).thenReturn(bundle);
+ when(bundle.getBundleId()).thenReturn(new Long(1));
+ when(bundle.getLocation()).thenReturn("string");
+ when(bundle.getSymbolicName()).thenReturn("org.apache.aries.jmx.core");
+
+ ServiceEventData eventData = new ServiceEventData(event);
+ CompositeData data = eventData.toCompositeData();
+
+ assertEquals(new Long(44), data.get(IDENTIFIER));
+ assertEquals(new Long(1), data.get(BUNDLE_IDENTIFIER));
+ assertEquals("string", data.get(BUNDLE_LOCATION));
+ assertEquals("org.apache.aries.jmx.core", data.get(BUNDLE_SYMBOLIC_NAME));
+ assertArrayEquals(new String[] {"org.apache.aries.jmx.Mock" }, (String[]) data.get(OBJECT_CLASS));
+ assertEquals(ServiceEvent.REGISTERED, data.get(EVENT));
+
+ }
+
+
+ @Test
+ public void testFrom() throws Exception {
+
+ Map<String, Object> items = new HashMap<String, Object>();
+ items.put(IDENTIFIER, new Long(7));
+ items.put(BUNDLE_IDENTIFIER, new Long(67));
+ items.put(BUNDLE_LOCATION, "string");
+ items.put(BUNDLE_SYMBOLIC_NAME, "test");
+ items.put(OBJECT_CLASS, new String[] {"org.apache.aries.jmx.Mock" });
+ items.put(EVENT, ServiceEvent.MODIFIED);
+
+ CompositeData compositeData = new CompositeDataSupport(SERVICE_EVENT_TYPE, items);
+ ServiceEventData event = ServiceEventData.from(compositeData);
+
+ assertEquals(7, event.getServiceId());
+ assertEquals(67, event.getBundleId());
+ assertArrayEquals(new String[] {"org.apache.aries.jmx.Mock" }, event.getServiceInterfaces());
+ assertEquals("test", event.getBundleSymbolicName());
+ assertEquals("string", event.getBundleLocation());
+ assertEquals(ServiceEvent.MODIFIED, event.getEventType());
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,64 @@
+/**
+ * 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 org.apache.aries.jmx.Logger;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.startlevel.StartLevel;
+
+import static org.mockito.Mockito.*;
+
+/**
+ *
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class BundleStateMBeanHandlerTest {
+
+
+ @Test
+ public void testOpenAndClose() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ Logger logger = mock(Logger.class);
+
+ ServiceReference packageAdminRef = mock(ServiceReference.class);
+ PackageAdmin packageAdmin = mock(PackageAdmin.class);
+ when(context.getServiceReference(PackageAdmin.class.getName())).thenReturn(packageAdminRef);
+ when(context.getService(packageAdminRef)).thenReturn(packageAdmin);
+ ServiceReference startLevelRef = mock(ServiceReference.class);
+ StartLevel startLevel = mock(StartLevel.class);
+ when(context.getServiceReference(StartLevel.class.getName())).thenReturn(startLevelRef);
+ when(context.getService(startLevelRef)).thenReturn(startLevel);
+
+ BundleStateMBeanHandler handler = new BundleStateMBeanHandler(context, logger);
+ handler.open();
+
+ assertNotNull(handler.getMbean());
+
+ handler.close();
+ verify(context).ungetService(packageAdminRef);
+ verify(context).ungetService(startLevelRef);
+
+ }
+
+}
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateTest.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateTest.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateTest.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateTest.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,188 @@
+/**
+ * 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.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.BundleStateMBean.OBJECTNAME;
+
+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.apache.aries.jmx.codec.BundleEventData;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.startlevel.StartLevel;
+
+
+public class BundleStateTest {
+
+ @Test
+ public void testNotificationsForBundleEvents() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ PackageAdmin admin = mock(PackageAdmin.class);
+ StartLevel startLevel = mock(StartLevel.class);
+ Logger logger = mock(Logger.class);
+
+ //holder for Notifications captured
+ final List<Notification> received = new LinkedList<Notification>();
+
+ BundleState bundleState = new BundleState(context, admin, startLevel, logger);
+
+ Bundle b1 = mock(Bundle.class);
+ when(b1.getBundleId()).thenReturn(new Long(9));
+ when(b1.getSymbolicName()).thenReturn("bundle");
+ when(b1.getLocation()).thenReturn("file:/location");
+
+ BundleEvent installedEvent = mock(BundleEvent.class);
+ when(installedEvent.getBundle()).thenReturn(b1);
+ when(installedEvent.getType()).thenReturn(BundleEvent.INSTALLED);
+
+ BundleEvent resolvedEvent = mock(BundleEvent.class);
+ when(resolvedEvent.getBundle()).thenReturn(b1);
+ when(resolvedEvent.getType()).thenReturn(BundleEvent.RESOLVED);
+
+ MBeanServer server = mock(MBeanServer.class);
+
+ //setup for notification
+ ObjectName objectName = new ObjectName(OBJECTNAME);
+ bundleState.preRegister(server, objectName);
+ bundleState.postRegister(true);
+
+ //add NotificationListener to receive the events
+ bundleState.addNotificationListener(new NotificationListener() {
+ public void handleNotification(Notification notification, Object handback) {
+ received.add(notification);
+ }
+ }, null, null);
+
+ // capture the BundleListener registered with BundleContext to issue BundleEvents
+ ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
+ verify(context).addBundleListener(argument.capture());
+
+ //send events
+ BundleListener listener = argument.getValue();
+ listener.bundleChanged(installedEvent);
+ listener.bundleChanged(resolvedEvent);
+
+ //shutdown dispatcher via unregister callback
+ bundleState.postDeregister();
+ //check the BundleListener is cleaned up
+ verify(context).removeBundleListener(listener);
+
+ ExecutorService dispatcher = bundleState.getEventDispatcher();
+ assertTrue(dispatcher.isShutdown());
+ dispatcher.awaitTermination(2, TimeUnit.SECONDS);
+ assertTrue(dispatcher.isTerminated());
+
+ assertEquals(2, received.size());
+ Notification installed = received.get(0);
+ assertEquals(1, installed.getSequenceNumber());
+ CompositeData installedCompositeData = (CompositeData) installed.getUserData();
+ BundleEventData installedData = BundleEventData.from(installedCompositeData);
+ assertEquals("bundle", installedData.getBundleSymbolicName());
+ assertEquals(9, installedData.getBundleId());
+ assertEquals("file:/location", installedData.getLocation());
+ assertEquals(BundleEvent.INSTALLED, installedData.getEventType());
+
+ Notification resolved = received.get(1);
+ assertEquals(2, resolved.getSequenceNumber());
+ CompositeData resolvedCompositeData = (CompositeData) resolved.getUserData();
+ BundleEventData resolvedData = BundleEventData.from(resolvedCompositeData);
+ assertEquals("bundle", resolvedData.getBundleSymbolicName());
+ assertEquals(9, resolvedData.getBundleId());
+ assertEquals("file:/location", resolvedData.getLocation());
+ assertEquals(BundleEvent.RESOLVED, resolvedData.getEventType());
+
+ }
+
+ @Test
+ public void testLifeCycleOfNotificationSupport() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ PackageAdmin admin = mock(PackageAdmin.class);
+ StartLevel startLevel = mock(StartLevel.class);
+ Logger logger = mock(Logger.class);
+
+ BundleState bundleState = new BundleState(context, admin, startLevel, logger);
+
+ MBeanServer server1 = mock(MBeanServer.class);
+ MBeanServer server2 = mock(MBeanServer.class);
+
+ ObjectName objectName = new ObjectName(OBJECTNAME);
+ bundleState.preRegister(server1, objectName);
+ bundleState.postRegister(true);
+
+ // capture the BundleListener registered with BundleContext
+ ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
+ verify(context).addBundleListener(argument.capture());
+ assertEquals(1, argument.getAllValues().size());
+
+ BundleListener listener = argument.getValue();
+ assertNotNull(listener);
+
+ ExecutorService dispatcher = bundleState.getEventDispatcher();
+
+ //do registration with another server
+ bundleState.preRegister(server2, objectName);
+ bundleState.postRegister(true);
+
+ // check no more actions on BundleContext
+ argument = ArgumentCaptor.forClass(BundleListener.class);
+ verify(context, atMost(1)).addBundleListener(argument.capture());
+ assertEquals(1, argument.getAllValues().size());
+
+ //do one unregister
+ bundleState.postDeregister();
+
+ //verify bundleListener not invoked
+ verify(context, never()).removeBundleListener(listener);
+ assertFalse(dispatcher.isShutdown());
+
+ //do second unregister and check cleanup
+ bundleState.postDeregister();
+ verify(context).removeBundleListener(listener);
+ assertTrue(dispatcher.isShutdown());
+ dispatcher.awaitTermination(2, TimeUnit.SECONDS);
+ assertTrue(dispatcher.isTerminated());
+
+
+
+ }
+}