[FELIX-2571] Have fileinstall listen for configuration changes and write them back to the config files
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1027254 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java b/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java
new file mode 100644
index 0000000..5e4dcc4
--- /dev/null
+++ b/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.utils.properties;
+
+import junit.framework.TestCase;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+public class InterpolationHelperTest extends TestCase {
+
+ public void testBasicSubstitution()
+ {
+ System.setProperty("value1", "sub_value1");
+ Hashtable props = new Hashtable();
+ props.put("key0", "value0");
+ props.put("key1", "${value1}");
+ props.put("key2", "${value2}");
+
+ for (Enumeration e = props.keys(); e.hasMoreElements();)
+ {
+ String name = (String) e.nextElement();
+ props.put(name, InterpolationHelper.substVars((String) props.get(name), name, null, props));
+ }
+
+ assertEquals("value0", props.get("key0"));
+ assertEquals("sub_value1", props.get("key1"));
+ assertEquals("", props.get("key2"));
+
+ }
+
+ public void testSubstitutionFailures()
+ {
+ assertEquals("a}", InterpolationHelper.substVars("a}", "b", null, new Hashtable()));
+ assertEquals("${a", InterpolationHelper.substVars("${a", "b", null, new Hashtable()));
+ }
+
+ public void testEmptyVariable() {
+ assertEquals("", InterpolationHelper.substVars("${}", "b", null, new Hashtable()));
+ }
+
+ public void testInnerSubst() {
+ Hashtable props = new Hashtable();
+ props.put("a", "b");
+ props.put("b", "c");
+ assertEquals("c", InterpolationHelper.substVars("${${a}}", "z", null, props));
+ }
+
+ public void testSubstLoop() {
+ try {
+ InterpolationHelper.substVars("${a}", "a", null, new Hashtable());
+ fail("Should have thrown an exception");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ public void testSubstitutionEscape()
+ {
+ assertEquals("${a}", InterpolationHelper.substVars("$\\{a${#}\\}", "b", null, new Hashtable()));
+ assertEquals("${a}", InterpolationHelper.substVars("$\\{a\\}${#}", "b", null, new Hashtable()));
+ assertEquals("${a}", InterpolationHelper.substVars("$\\{a\\}", "b", null, new Hashtable()));
+ }
+
+}
diff --git a/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java b/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
new file mode 100644
index 0000000..7809943
--- /dev/null
+++ b/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.utils.properties;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+/**
+ * <p>
+ * Unit tests on <code>Properties</code>.
+ * </p>
+ *
+ * @author jbonofre
+ */
+public class PropertiesTest extends TestCase {
+
+ private final static String TEST_PROPERTIES_FILE = "test.properties";
+
+ private Properties properties;
+
+ /*
+ * (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ properties = new Properties();
+ properties.load(this.getClass().getClassLoader().getResourceAsStream(TEST_PROPERTIES_FILE));
+ }
+
+ /**
+ * <p>
+ * Test getting property.
+ * </p>
+ *
+ * @throws Exception
+ */
+ public void testGettingProperty() throws Exception {
+ assertEquals("test", properties.get("test"));
+ }
+
+ public void testLoadSave() throws IOException {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ pw.println("# ");
+ pw.println("# The Main ");
+ pw.println("# ");
+ pw.println("# Comment ");
+ pw.println("# ");
+ pw.println("");
+ pw.println("# Another comment");
+ pw.println("");
+ pw.println("# A value comment");
+ pw.println("key1 = val1");
+ pw.println("");
+ pw.println("# Another value comment");
+ pw.println("key2 = ${key1}/foo");
+ pw.println("");
+ pw.println("# A third comment");
+ pw.println("key3 = val3");
+ pw.println("");
+
+
+ Properties props = new Properties();
+ props.load(new StringReader(sw.toString()));
+ props.save(System.err);
+ System.err.println("=====");
+
+ props.put("key2", props.get("key2"));
+ props.put("key3", "foo");
+ props.save(System.err);
+ System.err.println("=====");
+ }
+}
diff --git a/utils/src/test/resources/test.properties b/utils/src/test/resources/test.properties
new file mode 100644
index 0000000..2856e72
--- /dev/null
+++ b/utils/src/test/resources/test.properties
@@ -0,0 +1,21 @@
+##---------------------------------------------------------------------------
+## 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.
+##---------------------------------------------------------------------------
+#
+# test.properties
+# Used in the PropertiesTest
+#
+test=test
\ No newline at end of file