-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Load System properties from configuration file
- Loading branch information
1 parent
5b3d7d3
commit 066fe68
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/it/mulders/mcs/cli/SystemPropertyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package it.mulders.mcs.cli; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Loads additional system properties from a predefined file on disk. | ||
* <br /> | ||
* Copies those properties over the existing ones @{{@link System#getProperties()}} so the result | ||
* is a drop-in replacement that could be passed for {@link System#setProperties(Properties)}. | ||
* This class does not modify the System properties itself. | ||
*/ | ||
public class SystemPropertyLoader { | ||
private static final Path MCS_PROPERTIES_FILE = Paths.get( | ||
System.getProperty("user.home"), | ||
".mcs", | ||
"mcs.config" | ||
); | ||
|
||
private final Properties properties; | ||
|
||
public SystemPropertyLoader() { | ||
this(MCS_PROPERTIES_FILE); | ||
} | ||
|
||
protected SystemPropertyLoader(final Path source) { | ||
var input = new Properties(); | ||
|
||
if (Files.exists(source) && Files.isRegularFile(source)) { | ||
try (var reader = Files.newBufferedReader(source)) { | ||
input.load(reader); | ||
} catch (IOException ioe) { | ||
System.err.printf("Failed to load %s: %s%n", source, ioe.getLocalizedMessage()); | ||
} | ||
} | ||
|
||
this.properties = new Properties(); | ||
properties.putAll(System.getProperties()); | ||
properties.putAll(input); | ||
} | ||
|
||
public Properties getProperties() { | ||
return properties; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/it/mulders/mcs/cli/SystemPropertyLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package it.mulders.mcs.cli; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.assertj.core.api.WithAssertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class SystemPropertyLoaderTest implements WithAssertions { | ||
private static final Path SAMPLE = Paths.get("src", "test", "resources", "sample-mcs.config"); | ||
@Test | ||
void should_load_if_file_exists() { | ||
var loader = new SystemPropertyLoader(SAMPLE); | ||
|
||
assertThat(loader.getProperties()) | ||
.containsEntry("example.a", "foo"); | ||
} | ||
|
||
@Test | ||
void should_not_fail_if_file_does_not_exist() { | ||
var loader = new SystemPropertyLoader(Paths.get("src", "test", "resources", "non-existing-mcs.config")); | ||
|
||
assertThat(loader.getProperties().isEmpty()).isFalse(); | ||
} | ||
|
||
@Test | ||
void should_delegate_to_System_properties() { | ||
var loader = new SystemPropertyLoader(SAMPLE); | ||
|
||
// user.dir is not overridden in the sample configuration file | ||
assertThat(loader.getProperties()).containsKey("user.dir"); | ||
} | ||
|
||
@Test | ||
void should_override_System_properties() { | ||
var loader = new SystemPropertyLoader(SAMPLE); | ||
|
||
// user.home is recklessly overridden in the sample configuration file | ||
assertThat(loader.getProperties()).containsEntry("user.home", "whatever"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
example.a=foo | ||
example.b=${example.a} bar | ||
example.c=${example.a} ${example.a} baz | ||
user.home=whatever |