public static String replaceTokens(String template, Pattern pattern,
      Map<String, String> replacements) {
    StringBuffer sb = new StringBuffer();
    Matcher matcher = pattern.matcher(template);
    while (matcher.find()) {
      String replacement = replacements.get(matcher.group(1));
      if (replacement == null) {
        replacement = "";
      }
      matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
    }
    matcher.appendTail(sb);
    return sb.toString();
  }
@Test (timeout = 5000)
  public void testReplaceTokensShellEnvVars() {
    Pattern pattern = StringUtils.SHELL_ENV_VAR_PATTERN;
    Map<String, String> replacements = new HashMap<String, String>();
    replacements.put("FOO", "one");
    replacements.put("BAZ", "two");
    replacements.put("NUMBERS123", "one-two-three");
    replacements.put("UNDER_SCORES", "___");

    assertEquals("one", StringUtils.replaceTokens("$FOO", pattern,
      replacements));
    assertEquals("two", StringUtils.replaceTokens("$BAZ", pattern,
      replacements));
    assertEquals("", StringUtils.replaceTokens("$BAR", pattern, replacements));
    assertEquals("", StringUtils.replaceTokens("", pattern, replacements));
    assertEquals("one-two-three", StringUtils.replaceTokens("$NUMBERS123",
      pattern, replacements));
    assertEquals("___", StringUtils.replaceTokens("$UNDER_SCORES", pattern,
      replacements));
  }