Skip to content

Commit

Permalink
Merge pull request #115 from simonsilvalauinger/v2.0
Browse files Browse the repository at this point in the history
Add default values for variables in the configuration file
  • Loading branch information
pmwmedia authored Aug 4, 2019
2 parents 42dcca7 + 40a7309 commit c3596ea
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,24 @@ private static String resolve(final String value, final Resolver resolver) {
return value;
}

String data = resolver.resolve(name);
if (data == null) {
InternalLogger.log(Level.WARN, "'" + name + "' could not be found in " + resolver.getName());
String[] colonSplittedName = name.split(":", -1);
if (colonSplittedName.length > 2) {
InternalLogger.log(Level.WARN, "Multiple default values found: " + value);
return value;
} else {
builder.append(data);
}

String key = colonSplittedName[0];
String defaultValue = colonSplittedName.length == 2 ? colonSplittedName[1] : null;
String data = resolver.resolve(key);
if (data == null) {
if (defaultValue == null) {
InternalLogger.log(Level.WARN, "'" + key + "' could not be found in " + resolver.getName());
return value;
}
data = defaultValue;
}
builder.append(data);

position = end + 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,56 @@ public void nonExistentVariable() throws Exception {
assertThat(systemStream.consumeErrorOutput()).contains("WARN").containsOnlyOnce("my.invalid.variable");
}

/**
* Verifies that environment variables will be resolved and the given default value is ignored.
*
* @throws Exception
* Failed creating temporary file or invoking private method {@link Configuration#load()}
*/
@Test
public void existingVariableWithUnusedDefaultValue() throws Exception {
loadProperties(FileSystem.createTemporaryFile("test = ${PATH:/a/default/path}"));
assertThat(Configuration.get("test")).isEqualTo(System.getenv("PATH"));
}

/**
* Verifies that a given empty string is used as default value, if a variable exists neither as system property nor
* as environment variable..
*
* @throws Exception
* Failed creating temporary file or invoking private method {@link Configuration#load()}
*/
@Test
public void nonExistingVariableWithEmptyDefaultValue() throws Exception {
loadProperties(FileSystem.createTemporaryFile("test = ${my.invalid.variable:}"));
assertThat(Configuration.get("test")).isEqualTo("");
}

/**
* Verifies that a given default value is used, if a variable exists neither as system property nor
* as environment variable..
*
* @throws Exception
* Failed creating temporary file or invoking private method {@link Configuration#load()}
*/
@Test
public void nonExistingVariableWithDefaultValue() throws Exception {
loadProperties(FileSystem.createTemporaryFile("test = ${my.invalid.variable:a_default_value}"));
assertThat(Configuration.get("test")).isEqualTo("a_default_value");
}

/**
* Verifies that an accurate warning message will be output, if multiple default values were given.
*
* @throws Exception
* Failed creating temporary file or invoking private method {@link Configuration#load()}
*/
@Test
public void nonExistingVariableMultipleDefaultValues() throws Exception {
loadProperties(FileSystem.createTemporaryFile("test = ${PATH:a_default_value:an_other_default_value}"));
assertThat(systemStream.consumeErrorOutput()).contains("WARN").containsOnlyOnce("PATH");
}

/**
* Verifies that a new property can be added.
*
Expand Down

0 comments on commit c3596ea

Please sign in to comment.