### Question:
VariablesMerger { String applyEnvVars(String text, VariablesSource env, String path) { Matcher matcher = EnvVarDeclaration.VAR_PATTERN.matcher(text); StringBuffer result = new StringBuffer(); while (matcher.find()) { EnvVarDeclaration envVar = new EnvVarDeclaration(matcher.group(1)); String valueToBeUsed; String action; NamedValue entry = env.get(envVar.name); if (entry != null) { valueToBeUsed = String.valueOf(entry.getValue()); incrementCount(entry.getSourceName()); action = "replaced from " + entry.getSourceName(); } else { String effectiveDefaultVal; if (envVar.defaultVal != null) { effectiveDefaultVal = envVar.defaultVal; incrementCount(DEFAULT_KEY); action = "default in package"; } else { effectiveDefaultVal = matcher.group(0); incrementCount(NOTFOUND_KEY); action = "var not found (no default provided)"; } valueToBeUsed = effectiveDefaultVal; } logger.log(path.replace(ApplySystemEnvInstallHook.TEMPLATE_SUFFIX, "") + ": " + envVar.name + "=\"" + valueToBeUsed + "\" (" + action + ")"); if (path.endsWith(EXTENSION_CONFIG)) { valueToBeUsed = valueToBeUsed.replaceAll("([\\\\=\\s\"])", "\\\\$1"); } matcher.appendReplacement(result, Matcher.quoteReplacement(valueToBeUsed)); } matcher.appendTail(result); return result.toString(); } VariablesMerger(InstallHookLogger logger); String getReplacementSummary();  }### Answer:
@Test public void testApplyEnvVars() { VariablesMerger variablesMerger = Mockito.spy(new VariablesMerger(logger)); VariablesSource varSource = new VariablesSource("testsource", new HashMap<String, String>() { { put("var1", "val1"); put("prefix.var2", "val2"); put("urlConfig", "http: put("var.special.chars", "val=3 with\"\\"); } }) { }; assertEquals("no variables", variablesMerger.applyEnvVars("no variables", varSource, "/test")); assertEquals("val1", variablesMerger.applyEnvVars("${var1}", varSource, "/test")); assertEquals("val\\=3\\ with\\\"\\\\", variablesMerger.applyEnvVars("${var.special.chars}", varSource, "/test.config")); assertEquals("x-val\\=3\\ with\\\"\\\\-x", variablesMerger.applyEnvVars("x-${var.special.chars}-x", varSource, "/test.config")); assertEquals("val=3 with\"\\", variablesMerger.applyEnvVars("${var.special.chars}", varSource, "/test.xml")); assertEquals("x-val=3 with\"\\-x", variablesMerger.applyEnvVars("x-${var.special.chars}-x", varSource, "/test.xml")); assertEquals("x-val1-x", variablesMerger.applyEnvVars("x-${var1}-x", varSource, "/test")); assertEquals("x-val2-x", variablesMerger.applyEnvVars("x-${prefix.var2}-x", varSource, "/test")); assertEquals("x-val2-x-val1", variablesMerger.applyEnvVars("x-${prefix.var2}-x-${var1}", varSource, "/test")); assertEquals("x-val1-x", variablesMerger.applyEnvVars("x-${var1:defaultVal}-x", varSource, "/test")); assertEquals("x-defaultVal-x", variablesMerger.applyEnvVars("x-${varNotInEnv:defaultVal}-x", varSource, "/test")); assertEquals("x-${varNotInEnv}-x", variablesMerger.applyEnvVars("x-${varNotInEnv}-x", varSource, "/test")); assertEquals("x--x", variablesMerger.applyEnvVars("x-${varNotInEnv:}-x", varSource, "/test")); assertEquals("x-http: assertEquals("x-http: variablesMerger.applyEnvVars("x-${urlConfig:http: assertEquals("x-http: variablesMerger.applyEnvVars("x-${urlConfigVarNotSet:http: }