FELIX-2366: Avoiding property substitution by escaping does not remove escape character

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@949136 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
index ed39a52..e712a26 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
@@ -133,7 +133,7 @@
         // return the existing value.
         if ((startDelim < 0) || (stopDelim < 0))
         {
-            return val;
+            return unescape(val);
         }
 
         // At this point, we have found a variable placeholder so
@@ -154,7 +154,7 @@
         if (substValue == null)
         {
             // Ignore unknown property values.
-            substValue = System.getProperty(variable, "");
+            substValue = variable.length() > 0 ? System.getProperty(variable, "") : "";
         }
 
         // Remove the found variable from the cycle map, since
@@ -172,6 +172,13 @@
         val = substVars(val, currentKey, cycleMap, configProps);
 
         // Remove escape characters preceding {, } and \
+        val = unescape(val);
+
+        // Return the value.
+        return val;
+    }
+
+    private static String unescape(String val) {
         int escape = val.indexOf(ESCAPE_CHAR);
         while (escape >= 0 && escape < val.length() - 1)
         {
@@ -182,8 +189,6 @@
             }
             escape = val.indexOf(ESCAPE_CHAR, escape + 1);
         }
-
-        // Return the value.
         return val;
     }
 
diff --git a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/UtilTest.java b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/UtilTest.java
index 61fff9c..343380d 100644
--- a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/UtilTest.java
+++ b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/UtilTest.java
@@ -54,4 +54,31 @@
         assertEquals("${a", Util.substVars("${a", "b", null, new Hashtable()));
     }
 
+    public void testEmptyVariable() {
+        assertEquals("", Util.substVars("${}", "b", null, new Hashtable()));
+    }
+
+    public void testInnerSubst() {
+        Dictionary props = new Hashtable();
+        props.put("a", "b");
+        props.put("b", "c");
+        assertEquals("c", Util.substVars("${${a}}", "z", null, props));
+    }
+
+    public void testSubstLoop() {
+        try {
+            Util.substVars("${a}", "a", null, new Hashtable());
+            fail("Should have thrown an exception");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void testSubstitutionEscape()
+    {
+        assertEquals("${a}", Util.substVars("$\\{a${#}\\}", "b", null, new Hashtable()));
+        assertEquals("${a}", Util.substVars("$\\{a\\}${#}", "b", null, new Hashtable()));
+        assertEquals("${a}", Util.substVars("$\\{a\\}", "b", null, new Hashtable()));
+    }
+
 }
\ No newline at end of file