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

Add default values for variables in the configuration file #115

Merged
merged 3 commits into from
Aug 4, 2019
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
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