InterpolationUtil { public static String substVars(String value) { int startDelim = value.indexOf(_DELIM_START); int stopDelim = value.indexOf(_DELIM_STOP); while ((startDelim >= 0) && (stopDelim >= 0)) { while ((stopDelim > 0) && (value.charAt(stopDelim - 1) == _ESCAPE_CHAR)) { stopDelim = value.indexOf(_DELIM_STOP, stopDelim + 1); } while (stopDelim >= 0) { int index = value.indexOf( _DELIM_START, startDelim + _DELIM_START.length()); if ((index < 0) || (index > stopDelim)) { break; } else if (index < stopDelim) { startDelim = index; } } if (startDelim < stopDelim) { break; } stopDelim = value.indexOf(_DELIM_STOP, stopDelim + 1); startDelim = value.indexOf(_DELIM_START); } if ((startDelim < 0) || (stopDelim < 0)) { return value; } String variable = value.substring( startDelim + _DELIM_START.length(), stopDelim); String substValue = null; if ((substValue == null) && (variable.length() > 0)) { substValue = System.getProperty(variable); } if (substValue == null) { substValue = ""; } value = value.substring(0, startDelim) + substValue + value.substring(stopDelim + _DELIM_STOP.length()); value = substVars(value); return value; }  static String substVars(String value);  }
@Test public void testBlank() { Assert.assertEquals( StringPool.BLANK, InterpolationUtil.substVars("${testSystemKeyNonexistent}")); }
@Test public void testExtraCloseBrace() { Assert.assertEquals( _SYSTEM_VALUE.concat(StringPool.CLOSE_CURLY_BRACE), InterpolationUtil.substVars( _SUBSTITUTION.concat(StringPool.CLOSE_CURLY_BRACE))); }
@Test public void testInlineSubstitution() { Assert.assertEquals( "test" + _SYSTEM_VALUE + "test", InterpolationUtil.substVars("test" + _SUBSTITUTION + "test")); }
@Test public void testMultipleSubstitution() { Assert.assertEquals( _SYSTEM_VALUE + _SYSTEM_VALUE, InterpolationUtil.substVars(_SUBSTITUTION + _SUBSTITUTION)); }
@Test public void testNestedSub() { String systemKey2 = "testSystemKey2"; String oldSystemValue = System.setProperty(systemKey2, _SYSTEM_KEY); try { Assert.assertEquals( _SYSTEM_VALUE, InterpolationUtil.substVars("${${" + systemKey2 + "}}")); } finally { if (oldSystemValue == null) { System.clearProperty(systemKey2); } else { System.setProperty(systemKey2, _oldSystemValue); } } }
@Test public void testSubstitution() { Assert.assertEquals( _SYSTEM_VALUE, InterpolationUtil.substVars(_SUBSTITUTION)); }