Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with string replace methods removing backslashes #51

Merged
merged 1 commit into from
Sep 23, 2013
Merged
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
8 changes: 7 additions & 1 deletion src/main/java/org/aeonbits/owner/StrSubstitutor.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ String replace(String source) {
while (m.find()) {
String var = m.group(1);
String value = values.getProperty(var);
String replacement = (value != null) ? replace(value) : "";
String replacement;
if (value != null) {
String safeValue = value.replace("\\", "\\\\");
replacement = replace(safeValue);
} else {
replacement = "";
}
m.appendReplacement(sb, replacement);
}
m.appendTail(sb);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/aeonbits/owner/Util.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ static <T> T[] reverse(T[] array) {
}

static String expandUserHome(String text) {
if (text.equals("~"))
return System.getProperty("user.home");
if (text.indexOf("~/") == 0 || text.indexOf("file:~/") == 0 || text.indexOf("jar:file:~/") == 0)
return text.replaceFirst("~/", System.getProperty("user.home") + "/");
if (text.equals("~")) {
return System.getProperty("user.home");
} else if (text.indexOf("~/") == 0 || text.indexOf("file:~/") == 0 || text.indexOf("jar:file:~/") == 0) {
String safeHome = System.getProperty("user.home").replace("\\", "\\\\");
return text.replaceFirst("~/", safeHome + "/");
}
return text;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/aeonbits/owner/StrSubstitutorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public void shouldReturnNullWhenNullIsProvided() {
public void shouldReplaceVariables() {
Properties values = new Properties();
values.setProperty("animal", "quick brown fox");
values.setProperty("target", "lazy dog");
values.setProperty("target", "lazy\\slow dog");
String templateString = "The ${animal} jumped over the ${target}.";
StrSubstitutor sub = new StrSubstitutor(values);
String resolvedString = sub.replace(templateString);
assertEquals("The quick brown fox jumped over the lazy dog.", resolvedString);
assertEquals("The quick brown fox jumped over the lazy\\slow dog.", resolvedString);
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/aeonbits/owner/importedprops/WithImportedPropertiesTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public void testMultipleImports() {
Properties propsFromTest = new Properties();
propsFromTest.setProperty("external", "propsFromTest");

String winPath = "C:\\windows\\path";
System.setProperty("value.with.backslash", winPath);
String userHome = System.getProperty("user.home");
String envHome = System.getenv("HOME");
WithImportedProperties conf =
ConfigFactory.create(WithImportedProperties.class,
propsFromTest, System.getProperties(), System.getenv());
assertEquals(userHome, conf.userHome());
assertEquals(envHome, conf.envHome());
assertEquals(winPath, conf.valueWithBackslash());
assertEquals("testing replacement from propsFromTest properties file.", conf.someValue());
}

Expand All @@ -70,5 +73,8 @@ public static interface WithImportedProperties extends Config {

@DefaultValue("${HOME}")
String envHome();

@DefaultValue("${value.with.backslash}")
String valueWithBackslash();
}
}