Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions owner/src/main/java/org/aeonbits/owner/StrSubstitutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ String replace(String source) {
Matcher m = PATTERN.matcher(source);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String var = m.group(1);
String value = values.getProperty(var);
String replacement = (value != null) ? replace(value) : "";
String[] var = m.group(1).split(":");
String value = values.getProperty(var[0]);
String replacement = (value != null) ? replace(value) :
var.length > 1 ? var[1] : ""; // Otherwise try to extract a default value
m.appendReplacement(sb, Matcher.quoteReplacement(replacement));
}
m.appendTail(sb);
Expand Down
20 changes: 20 additions & 0 deletions owner/src/test/java/org/aeonbits/owner/StrSubstitutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public void shouldReplaceVariables() {
assertEquals("The quick brown fox jumped over the lazy dog.", resolvedString);
}

@Test
public void shouldApplyDefaultValues() {
Properties values = new Properties();
String templateString = "The ${animal:wolf} jumped over the ${target:sheep}.";
StrSubstitutor sub = new StrSubstitutor(values);
String resolvedString = sub.replace(templateString);
assertEquals("The wolf jumped over the sheep.", resolvedString);
}

@Test
public void shouldOverrideDefaultValues() {
Properties values = new Properties();
values.setProperty("animal", "quick brown fox");
values.setProperty("target", "lazy dog");
String templateString = "The ${animal:wolf} jumped over the ${target:sheep}.";
StrSubstitutor sub = new StrSubstitutor(values);
String resolvedString = sub.replace(templateString);
assertEquals("The quick brown fox jumped over the lazy dog.", resolvedString);
}

@Test
public void shouldReplaceVariablesHavingBackslashes() {
Properties values = new Properties();
Expand Down