EnvVarResolverProperties extends Properties { protected static String resolveEnvVars(String input) { Preconditions.checkNotNull(input); Pattern p = Pattern.compile("\\$\\{(\\w+)\\}"); Matcher m = p.matcher(input); StringBuffer sb = new StringBuffer(); while (m.find()) { String envVarName = m.group(1); String envVarValue = System.getenv(envVarName); m.appendReplacement(sb, null == envVarValue ? "" : envVarValue); } m.appendTail(sb); return sb.toString(); }  @Override String getProperty(String key);  }
@Test public void resolveEnvVar() throws Exception { environmentVariables.set("VARNAME", "varvalue"); String resolved = EnvVarResolverProperties.resolveEnvVars("padding ${VARNAME} padding"); Assert.assertEquals("padding varvalue padding", resolved); }
@Test public void resolveEnvVars() throws Exception { environmentVariables.set("VARNAME1", "varvalue1"); environmentVariables.set("VARNAME2", "varvalue2"); String resolved = EnvVarResolverProperties .resolveEnvVars("padding ${VARNAME1} ${VARNAME2} padding"); Assert.assertEquals("padding varvalue1 varvalue2 padding", resolved); }