FELIX-1653 Adding support for system property deletion
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@819790 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/shell/src/main/java/org/apache/felix/shell/impl/SystemPropertiesCommandImpl.java b/shell/src/main/java/org/apache/felix/shell/impl/SystemPropertiesCommandImpl.java
index 7a13736..ca273bc 100644
--- a/shell/src/main/java/org/apache/felix/shell/impl/SystemPropertiesCommandImpl.java
+++ b/shell/src/main/java/org/apache/felix/shell/impl/SystemPropertiesCommandImpl.java
@@ -29,6 +29,7 @@
* Usage:
* sysprop -> displays all the system properties
* sysprop [key] -> displays the [key] property
+ * sysprop -r [key] -> removes the [key] property
* sysprop [key] [value] -> set the property [key] to [value]
*/
public class SystemPropertiesCommandImpl implements Command
@@ -45,17 +46,22 @@
else
{
st.nextToken();
- String propertyKey = st.nextToken();
+ String secondArgument = st.nextToken();
if (tokens == 2)
{
- out.println(propertyKey + "=" + System.getProperty(propertyKey));
+ out.println(secondArgument + "=" + System.getProperty(secondArgument));
}
- else
+ else if (tokens == 3)
{
- String value = st.nextToken();
- System.setProperty(propertyKey, value);
- out.println("Set " + propertyKey + "=" + value);
+ if ("-r".equals(secondArgument))
+ removeProperty(st.nextToken());
+ else
+ {
+ String value = st.nextToken();
+ System.setProperty(secondArgument, value);
+ out.println("Set " + secondArgument + "=" + value);
+ }
}
}
}
@@ -70,6 +76,11 @@
}
}
+ private void removeProperty(String key)
+ {
+ System.getProperties().remove(key);
+ }
+
public String getName()
{
return "sysprop";
@@ -77,11 +88,11 @@
public String getShortDescription()
{
- return "Display, set and modify system properties";
+ return "Display, set, modify and remove system properties";
}
public String getUsage()
{
- return "sysprop [<key>] [<value>]";
+ return "sysprop [-r] [<key>] [<value>]";
}
}
\ No newline at end of file
diff --git a/shell/src/test/java/org/apache/felix/shell/impl/SystemPropertiesTest.java b/shell/src/test/java/org/apache/felix/shell/impl/SystemPropertiesTest.java
new file mode 100644
index 0000000..aea0475
--- /dev/null
+++ b/shell/src/test/java/org/apache/felix/shell/impl/SystemPropertiesTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.felix.shell.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import junit.framework.TestCase;
+
+public class SystemPropertiesTest extends TestCase
+{
+
+ SystemPropertiesCommandImpl sysprop = new SystemPropertiesCommandImpl();
+
+ public void testCanDisplayASingleProperty()
+ {
+ System.setProperty("first", "foo");
+ assertEquals("foo", System.getProperty("first"));
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ PrintStream ps = new PrintStream(bos);
+
+ sysprop.execute("sysprop first", ps, System.err);
+ assertTrue(bos.toString().startsWith("first=foo"));
+ }
+
+ public void testCanCreateProperty()
+ {
+ assertNull(System.getProperty("foo"));
+ sysprop.execute("sysprop foo bar", System.out, System.err);
+ assertEquals("bar", System.getProperty("foo"));
+ }
+
+ public void testCanChangePropertyValue()
+ {
+ System.setProperty("foo", "bar");
+ assertEquals("bar", System.getProperty("foo"));
+
+ sysprop.execute("sysprop foo barbar", System.out, System.err);
+
+ assertEquals("barbar", System.getProperty("foo"));
+ }
+
+ public void testCanRemoveProperty()
+ {
+ System.setProperty("foo", "bar");
+ assertEquals("bar", System.getProperty("foo"));
+ sysprop.execute("sysprop -r foo", System.out, System.err);
+
+ assertNull(System.getProperty("foo"));
+ }
+}