aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Girardot2010-08-13 08:45:45 (EDT)
committerSteve Powell2010-08-13 08:56:34 (EDT)
commit6aed63fd535198d320a2be88e63e6388258e5210 (patch)
tree064bad0d4b80c1c75fd1c3369e1dcd6be633db08
parentc3b5384044e320802bdf87cc69ad9d02b209afe0 (diff)
downloadorg.eclipse.virgo.kernel-6aed63fd535198d320a2be88e63e6388258e5210.zip
org.eclipse.virgo.kernel-6aed63fd535198d320a2be88e63e6388258e5210.tar.gz
org.eclipse.virgo.kernel-6aed63fd535198d320a2be88e63e6388258e5210.tar.bz2
Modified ConfigurationAdminConfigurationInfo to cope with null Dictionary;
Added @NonNull to ConfgurationAdmin argument on C…A…C…I… constructor; Modified StubConfiguration to return null initially; Added tests for ConfigurationAdminConfigurationInfo.
-rw-r--r--org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfo.java20
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java3
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfoTests.java84
3 files changed, 98 insertions, 9 deletions
diff --git a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfo.java b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfo.java
index e15805d..c6d51a1 100644
--- a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfo.java
+++ b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfo.java
@@ -17,6 +17,7 @@ import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
+import org.eclipse.virgo.kernel.serviceability.NonNull;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
@@ -31,7 +32,7 @@ public class ConfigurationAdminConfigurationInfo implements ConfigurationInfo {
private final String pid;
- public ConfigurationAdminConfigurationInfo(ConfigurationAdmin configurationAdmin, String pid) {
+ public ConfigurationAdminConfigurationInfo(@NonNull ConfigurationAdmin configurationAdmin, String pid) {
this.configurationAdmin = configurationAdmin;
this.pid = pid;
}
@@ -40,18 +41,21 @@ public class ConfigurationAdminConfigurationInfo implements ConfigurationInfo {
return this.pid;
}
- @SuppressWarnings("unchecked")
public Map<String, String> getProperties() {
try {
Configuration configuration = configurationAdmin.getConfiguration(this.pid, null);
+ Map<String, String> properties = new HashMap<String, String>();
+
+ @SuppressWarnings("unchecked")
Dictionary<String, String> dictionary = configuration.getProperties();
- Map<String, String> properties = new HashMap<String, String>(dictionary.size());
- Enumeration<String> keys = dictionary.keys();
- while (keys.hasMoreElements()) {
- String key = keys.nextElement();
- String value = dictionary.get(key);
- properties.put(key, value);
+ if (dictionary != null) {
+ Enumeration<String> keys = dictionary.keys();
+ while (keys.hasMoreElements()) {
+ String key = keys.nextElement();
+ String value = dictionary.get(key);
+ properties.put(key, value);
+ }
}
return properties;
} catch (IOException e) {
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
index 2d09a67..ad67c5c 100644
--- a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/StubConfiguration.java
@@ -19,7 +19,7 @@ import org.osgi.service.cm.Configuration;
public class StubConfiguration implements Configuration {
- private Hashtable properties = new Hashtable();
+ private Hashtable properties = null;
public void delete() throws IOException {
throw new UnsupportedOperationException();
@@ -39,6 +39,7 @@ public class StubConfiguration implements Configuration {
@SuppressWarnings("unchecked")
public Dictionary getProperties() {
+ if (this.properties == null) return null;
Hashtable propertiesCopy = new Hashtable();
propertiesCopy.putAll(this.properties);
return propertiesCopy;
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfoTests.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfoTests.java
new file mode 100644
index 0000000..02835fe
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminConfigurationInfoTests.java
@@ -0,0 +1,84 @@
+/* Copyright (c) 2010 Olivier Girardot
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Olivier Girardot - initial contribution
+ */
+
+package org.eclipse.virgo.kernel.config.internal;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.eclipse.virgo.kernel.StubConfigurationAdmin;
+import org.eclipse.virgo.kernel.serviceability.Assert.FatalAssertionException;
+import org.junit.Test;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+/**
+ * This class is for testing {@link ConfigurationAdminConfigurationInfo} class,
+ * an implementation of the {@link ConfigurationInfo} interface.
+ */
+public class ConfigurationAdminConfigurationInfoTests {
+ private static final String CONFIG_INFO_TEST_PID = "CUSTOM_PID";
+
+ @Test
+ public void testGetPid() {
+ ConfigurationAdmin stubAdmin = new StubConfigurationAdmin();
+ ConfigurationInfo configurationInfo = new ConfigurationAdminConfigurationInfo(stubAdmin, CONFIG_INFO_TEST_PID);
+ assertEquals(CONFIG_INFO_TEST_PID, configurationInfo.getPid());
+ }
+
+ @Test
+ public void testGetPropertiesFromEmptyConfiguration() {
+ ConfigurationAdmin stubAdmin = new StubConfigurationAdmin();
+ ConfigurationInfo configurationInfo = new ConfigurationAdminConfigurationInfo(stubAdmin, CONFIG_INFO_TEST_PID);
+ assertEquals("Properties map should be empty.", new HashMap<String, String>(), configurationInfo.getProperties());
+ }
+
+ @Test
+ public void testFailingConfigurationAdmin() throws IOException {
+ ConfigurationAdmin mockAdmin = createMock(ConfigurationAdmin.class);
+ expect(mockAdmin.getConfiguration(CONFIG_INFO_TEST_PID, null)).andThrow(new IOException("Error trying to load configuration"));
+ replay(mockAdmin);
+
+ ConfigurationInfo configurationInfo = new ConfigurationAdminConfigurationInfo(mockAdmin, CONFIG_INFO_TEST_PID);
+ assertNull(configurationInfo.getProperties());
+ }
+
+ @Test(expected=FatalAssertionException.class)
+ public void testFailingConstructorWithNullConfigurationAdmin() throws IOException {
+ ConfigurationInfo configurationInfo = new ConfigurationAdminConfigurationInfo(null, CONFIG_INFO_TEST_PID);
+ }
+
+ @Test
+ public void testGetPropertiesWithFilledConfiguration() throws IOException {
+ ConfigurationAdmin stubAdmin = new StubConfigurationAdmin();
+ Configuration config = stubAdmin.getConfiguration(CONFIG_INFO_TEST_PID);
+
+ Dictionary<String, String> dict = new Hashtable<String, String>();
+ dict.put("key", "value");
+
+ config.update(dict);
+
+ ConfigurationInfo configurationInfo = new ConfigurationAdminConfigurationInfo(stubAdmin, CONFIG_INFO_TEST_PID);
+
+ Map<String, String> expectedMap = new HashMap<String, String>();
+ expectedMap.put("key", "value");
+ assertEquals(expectedMap, configurationInfo.getProperties());
+ }
+
+} \ No newline at end of file