@Test public void testResolveProperties() { Properties props = new Properties(); props.put("number", 1); props.put("two", "2"); props.put("greeting", "hello"); props.put("simple", "simple: ${greeting}"); props.put("nested", "nested: ${simple}"); props.put("mixed", "mixed: ${nested} and ${simple}"); props.put("unexisting", "${foobar} substitution"); props.put("recursive", "variable5 ${recursive} recursive ${unexisting}"); props.put("characters{{$$", "characters"); props.put("ugly", "substitute: ${characters{{$$}"); Properties resolved = new ExpandProperties().apply(props); assertEquals(resolved.size(), props.size()); assertEquals(resolved.get("number"), 1); assertEquals(resolved.get("two"), "2"); assertEquals(resolved.get("greeting"), "hello"); assertEquals(resolved.get("simple"), "simple: hello"); assertEquals(resolved.get("nested"), "nested: simple: hello"); assertEquals(resolved.get("mixed"), "mixed: nested: simple: hello and simple: hello"); assertEquals(resolved.get("unexisting"), "${foobar} substitution"); assertEquals(resolved.get("recursive"), "variable5 ${recursive} recursive ${unexisting}"); assertEquals(resolved.get("ugly"), "substitute: characters"); }
@SuppressModernizer private boolean resolveProperties(Map<String, String> properties, Map<String, String> variables) { boolean anyReplacementDone = false; for (Map.Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); StringBuffer sb = new StringBuffer(); Matcher m = VAR.matcher(entry.getValue()); while (m.find()) { String match = m.group(); String var = match.substring(2, match.length() - 1); Optional<String> value = var.equals(key) ? Optional.<String> absent() : Optional.fromNullable(variables .get(var)); m.appendReplacement(sb, value.or("\\" + match)); if (value.isPresent()) { anyReplacementDone = true; } } m.appendTail(sb); properties.put(key, sb.toString()); } return anyReplacementDone; }
ExpandProperties implements Function<Properties, Properties> { @SuppressModernizer private boolean resolveProperties(Map<String, String> properties, Map<String, String> variables) { boolean anyReplacementDone = false; for (Map.Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); StringBuffer sb = new StringBuffer(); Matcher m = VAR.matcher(entry.getValue()); while (m.find()) { String match = m.group(); String var = match.substring(2, match.length() - 1); Optional<String> value = var.equals(key) ? Optional.<String> absent() : Optional.fromNullable(variables .get(var)); m.appendReplacement(sb, value.or("\\" + match)); if (value.isPresent()) { anyReplacementDone = true; } } m.appendTail(sb); properties.put(key, sb.toString()); } return anyReplacementDone; } }
ExpandProperties implements Function<Properties, Properties> { @SuppressModernizer private boolean resolveProperties(Map<String, String> properties, Map<String, String> variables) { boolean anyReplacementDone = false; for (Map.Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); StringBuffer sb = new StringBuffer(); Matcher m = VAR.matcher(entry.getValue()); while (m.find()) { String match = m.group(); String var = match.substring(2, match.length() - 1); Optional<String> value = var.equals(key) ? Optional.<String> absent() : Optional.fromNullable(variables .get(var)); m.appendReplacement(sb, value.or("\\" + match)); if (value.isPresent()) { anyReplacementDone = true; } } m.appendTail(sb); properties.put(key, sb.toString()); } return anyReplacementDone; }  }
ExpandProperties implements Function<Properties, Properties> { @SuppressModernizer private boolean resolveProperties(Map<String, String> properties, Map<String, String> variables) { boolean anyReplacementDone = false; for (Map.Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); StringBuffer sb = new StringBuffer(); Matcher m = VAR.matcher(entry.getValue()); while (m.find()) { String match = m.group(); String var = match.substring(2, match.length() - 1); Optional<String> value = var.equals(key) ? Optional.<String> absent() : Optional.fromNullable(variables .get(var)); m.appendReplacement(sb, value.or("\\" + match)); if (value.isPresent()) { anyReplacementDone = true; } } m.appendTail(sb); properties.put(key, sb.toString()); } return anyReplacementDone; }  @Override Properties apply(final Properties properties); }
ExpandProperties implements Function<Properties, Properties> { @SuppressModernizer private boolean resolveProperties(Map<String, String> properties, Map<String, String> variables) { boolean anyReplacementDone = false; for (Map.Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); StringBuffer sb = new StringBuffer(); Matcher m = VAR.matcher(entry.getValue()); while (m.find()) { String match = m.group(); String var = match.substring(2, match.length() - 1); Optional<String> value = var.equals(key) ? Optional.<String> absent() : Optional.fromNullable(variables .get(var)); m.appendReplacement(sb, value.or("\\" + match)); if (value.isPresent()) { anyReplacementDone = true; } } m.appendTail(sb); properties.put(key, sb.toString()); } return anyReplacementDone; }  @Override Properties apply(final Properties properties);  }