Skip to content

Commit bacd35c

Browse files
author
mccabd
committed
Refactor profile usage
1 parent 6e144c0 commit bacd35c

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/main/java/com/github/bordertech/config/DefaultConfiguration.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.commons.configuration.ConversionException;
55
import org.apache.commons.configuration.MapConfiguration;
66
import org.apache.commons.lang3.BooleanUtils;
7+
import org.apache.commons.lang3.ObjectUtils;
78
import org.apache.commons.lang3.StringUtils;
89
import org.apache.commons.lang3.tuple.ImmutablePair;
910
import org.apache.commons.lang3.tuple.Pair;
@@ -282,8 +283,6 @@ private void load() {
282283

283284
recordMessage("Working directory is " + workingDir);
284285

285-
setEnvironmentProfile();
286-
287286
for (String resourceName : resourceLoadOrder) {
288287
loadTop(resourceName);
289288
}
@@ -298,6 +297,8 @@ private void load() {
298297
loadEnvironmentProperties();
299298
}
300299

300+
setEnvironmentProfile();
301+
301302
// Check if environment set
302303
checkEnvironmentProperty();
303304

@@ -723,7 +724,8 @@ private void loadEnvironmentProperties() {
723724
* If both are defined, system property overrides and environment property
724725
*/
725726
private void setEnvironmentProfile() {
726-
environmentProfile = System.getProperty(PROFILE_PROPERTY, System.getenv().get(PROFILE_PROPERTY));
727+
728+
environmentProfile = ObjectUtils.firstNonNull(backing.get(PROFILE_PROPERTY), System.getProperty(PROFILE_PROPERTY), System.getenv().get(PROFILE_PROPERTY));
727729

728730
if (StringUtils.isBlank(environmentProfile)) {
729731
recordMessage("Environment Profile Property <" + PROFILE_PROPERTY + "> has not been defined.");
@@ -1320,7 +1322,14 @@ protected String get(final String key, final String defolt) {
13201322
* @return the property value or null
13211323
*/
13221324
protected String get(final String key) {
1323-
return backing.get(getKey(key));
1325+
String result = backing.get(getKey(key));
1326+
1327+
if (StringUtils.isNotBlank(result)) {
1328+
//Final substitution check
1329+
result = StringSubstitutor.replace(result, backing);
1330+
}
1331+
1332+
return result;
13241333
}
13251334

13261335
/**
@@ -1357,6 +1366,7 @@ protected void handlePropertiesChanged() {
13571366
subcontextCache.clear();
13581367
// Check if environment changed
13591368
checkEnvironmentProperty();
1369+
setEnvironmentProfile();
13601370
}
13611371

13621372
/**

src/test/java/com/github/bordertech/config/DefaultConfigurationTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ public void testSubstitution() {
102102
assertPropertyEquals("substitute.part1And2And3Key", "part1Value+part2Value+part3Value");
103103
assertPropertyEquals("substitute.combinedKey", "multiPart1ValuemultiPart2Value");
104104
assertPropertyEquals("substitute.reurse", "${substitute.recurse}");
105-
assertPropertyEquals("substitute.key.not.defined.in.value", "${this.key.is.not.defined}");
105+
assertPropertyEquals("substitute.key.not.defined.in.value", "${this.key.is.not.defined.initially}");
106+
assertPropertyEquals("substitute.key.with.profile.in.value", "prefix_${bordertech.config.profile}_suffix");
107+
108+
config.setProperty("this.key.is.not.defined.initially", "isNowDefined");
109+
110+
assertPropertyEquals("substitute.key.not.defined.in.value", "isNowDefined");
111+
112+
config.setProperty(PROFILE_PROPERTY, "a_profile");
113+
114+
assertPropertyEquals("substitute.key.with.profile.in.value", "prefix_a_profile_suffix");
115+
116+
config.clearProperty(PROFILE_PROPERTY);
106117
}
107118

108119
@Test
@@ -458,7 +469,7 @@ public void testRefresh() {
458469
public void testClear() {
459470
Assert.assertFalse(config.isEmpty());
460471
config.clear();
461-
Assert.assertTrue(config.isEmpty());
472+
Assert.assertTrue("Config should be cleared", config.isEmpty());
462473
}
463474

464475
@Test
@@ -609,7 +620,16 @@ public void testUseEnvironmentProfile() {
609620
//Profile is set and property key with profile is set
610621
Assert.assertEquals(profileValue, config.get(key));
611622

623+
config.addProperty(PROFILE_PROPERTY, env + "second");
624+
625+
Assert.assertEquals(value, config.get(key));
626+
627+
config.addProperty(key + "." + env + "second", profileValue + "Second");
628+
629+
Assert.assertEquals(profileValue + "Second", config.get(key));
630+
612631
System.clearProperty(PROFILE_PROPERTY);
632+
config.refresh();
613633
}
614634

615635
@Test

src/test/resources/com/github/bordertech/config/DefaultConfigurationTest.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ substitute.reurse2=${substitute.recurse3}
4646
substitute.reurse3=${substitute.recurse1}
4747

4848
#Substitution before key is defined
49-
substitute.key.not.defined.in.value=${this.key.is.not.defined}
49+
substitute.key.not.defined.in.value=${this.key.is.not.defined.initially}
50+
51+
substitute.key.with.profile.in.value=prefix_${bordertech.config.profile}_suffix
5052

5153
# -------------------------------------------------------------------------------------------------
5254
# Environment Suffix tests

0 commit comments

Comments
 (0)