Skip to content

Commit

Permalink
feat: DH-18588: Allow shortened Name.rootFIle config file specifier (#…
Browse files Browse the repository at this point in the history
…6621)

(cherry picked from commit a3b00c2)
  • Loading branch information
abaranec authored Feb 5, 2025
1 parent 315ff3f commit 12d02a0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ private static class Named extends Configuration {

@Override
protected String determinePropertyFile() {
final String propFile = System.getProperty("Configuration." + name + ".rootFile");
if (propFile == null) {
throw new ConfigurationException("No property file defined for named configuration " + name);
String propFile = System.getProperty("Configuration." + name + ".rootFile");
if (propFile != null) {
return propFile;
}

return propFile;
// If Configuration.name.rootFile doesn't exist allow a fallback onto `name.rootFile`
propFile = System.getProperty(name + ".rootFile");
if (propFile != null) {
return propFile;
}

throw new ConfigurationException("No property file defined for named configuration " + name +
": Make sure the property `Configuration." + name + ".rootFile` or `" + name + ".rootFile` is set");
}
}

Expand All @@ -98,6 +105,7 @@ public static Configuration getInstance() {
* @throws ConfigurationException if the named configuration could not be loaded.
*/
public static Configuration getNamed(@NotNull final String name) throws ConfigurationException {
validateConfigName(name);
return NAMED_CONFIGURATIONS.computeIfAbsent(name, (k) -> {
try {
return new Named(name, DefaultConfigurationContext::new);
Expand Down Expand Up @@ -151,6 +159,7 @@ public static Configuration newStandaloneConfiguration(
* @return a new Configuration instance, guaranteed to not be cached.
*/
public static Configuration newStandaloneConfiguration(@NotNull final String name) {
validateConfigName(name);
return newStandaloneConfiguration(name, DefaultConfigurationContext::new);
}

Expand Down Expand Up @@ -433,4 +442,19 @@ private static void writeLine(StringBuilder out, String sKey, String sLeftValue,
static boolean isQuiet() {
return Bootstrap.isQuiet() || System.getProperty(QUIET_PROPERTY) != null;
}

/**
* Check that the passed in config name is not null and is allowed.
*
* @param name the Configuration name.
*/
static void validateConfigName(final String name) {
if (name == null) {
throw new ConfigurationException("Configuration name must not be null");
}

if ("Configuration".equals(name)) {
throw new ConfigurationException("The name `Configuration` may not be used as a Configuration name");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,46 @@ public void testNamedConfiguration() {
assertNull(c2.getStringWithDefault("test2", null));
assertEquals("true", c3.getProperty("test2"));
}

public void testNamedConfigurationSimple() {
System.setProperty(FILENAME_PROPERTY, "resources/lib-tests.prop");
System.clearProperty("Configuration.Test1.rootFile");
System.setProperty("Test1.rootFile", "resources/test1.prop");

Configuration.reset();
Configuration dc = Configuration.getInstance();
Configuration c1 = Configuration.getNamed("Test1");

assertEquals("cache", dc.getProperty("cacheDir"));
assertNull(c1.getStringWithDefault("cacheDir", null));

assertNull(dc.getStringWithDefault("test1", null));
assertEquals("true", c1.getProperty("test1"));

assertNull(dc.getStringWithDefault("test3", null));
// test1 imports test3, so expected.
assertEquals("true", c1.getProperty("test3"));

// Make sure we can't try to load a Configuration with a null name, or `Configuration`
try {
final Configuration c = Configuration.getNamed(null);
fail("Should have rejected a null name");
} catch (ConfigurationException ignored) {

}

try {
final Configuration c = Configuration.getNamed("Configuration");
fail("Should have rejected the name `Configuration`");
} catch (ConfigurationException ignored) {

}

try {
final Configuration c = Configuration.getNamed("NonExistent");
fail("Should have rejected a named config that doesnt exist");
} catch (ConfigurationException ignored) {

}
}
}

0 comments on commit 12d02a0

Please sign in to comment.