Skip to content

Commit 30fd1b4

Browse files
Merge pull request #50 from BorderTech/feature/profilesub
Restore ability for profile suffix key to be substituted by another property
2 parents 7120779 + 17c43cf commit 30fd1b4

23 files changed

+159
-66
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Release in-progress
44
* Switch from travis-ci to GitHub Actions #46
55
* Latest qa-parent and project dependencies
6+
* Restore ability for profile suffix key to refer to other property #49
67

78
## 1.0.7
89

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

+52-45
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package com.github.bordertech.config;
22

3-
import org.apache.commons.configuration.Configuration;
4-
import org.apache.commons.configuration.ConversionException;
5-
import org.apache.commons.configuration.MapConfiguration;
6-
import org.apache.commons.io.FileUtils;
7-
import org.apache.commons.lang3.BooleanUtils;
8-
import org.apache.commons.lang3.ObjectUtils;
9-
import org.apache.commons.lang3.StringUtils;
10-
import org.apache.commons.lang3.SystemUtils;
11-
import org.apache.commons.lang3.tuple.ImmutablePair;
12-
import org.apache.commons.lang3.tuple.Pair;
13-
import org.apache.commons.logging.Log;
14-
import org.apache.commons.logging.impl.SimpleLog;
15-
import org.apache.commons.text.StringSubstitutor;
16-
173
import java.io.BufferedInputStream;
184
import java.io.ByteArrayInputStream;
195
import java.io.ByteArrayOutputStream;
@@ -43,6 +29,19 @@
4329
import java.util.Properties;
4430
import java.util.Set;
4531
import java.util.TreeSet;
32+
import org.apache.commons.configuration.Configuration;
33+
import org.apache.commons.configuration.ConversionException;
34+
import org.apache.commons.configuration.MapConfiguration;
35+
import org.apache.commons.io.FileUtils;
36+
import org.apache.commons.lang3.BooleanUtils;
37+
import org.apache.commons.lang3.ObjectUtils;
38+
import org.apache.commons.lang3.StringUtils;
39+
import org.apache.commons.lang3.SystemUtils;
40+
import org.apache.commons.lang3.tuple.ImmutablePair;
41+
import org.apache.commons.lang3.tuple.Pair;
42+
import org.apache.commons.logging.Log;
43+
import org.apache.commons.logging.impl.SimpleLog;
44+
import org.apache.commons.text.StringSubstitutor;
4645

4746
/**
4847
* <p>
@@ -103,6 +102,7 @@ public class DefaultConfiguration implements Configuration {
103102
public static final String DUMP_FILE = "bordertech.config.parameters.dump.file";
104103
/**
105104
* If this parameter is set, it will be used as the environment suffix for each property lookup.
105+
*
106106
* @deprecated Use {@link #PROFILE_PROPERTY} to define the profile property
107107
*/
108108
@Deprecated
@@ -215,8 +215,8 @@ public DefaultConfiguration() {
215215
*/
216216
public DefaultConfiguration(final String... resourceLoadOrder) {
217217
if (resourceLoadOrder == null || resourceLoadOrder.length == 0 || Arrays
218-
.stream(resourceLoadOrder)
219-
.anyMatch(StringUtils::isBlank)) {
218+
.stream(resourceLoadOrder)
219+
.anyMatch(StringUtils::isBlank)) {
220220
this.resourceLoadOrder = InitHelper.getDefaultResourceLoadOrder();
221221
} else {
222222
this.resourceLoadOrder = resourceLoadOrder;
@@ -228,7 +228,7 @@ public DefaultConfiguration(final String... resourceLoadOrder) {
228228
/**
229229
* Copies information from the input stream to the output stream using a specified buffer size.
230230
*
231-
* @param in the source stream.
231+
* @param in the source stream.
232232
* @param out the destination stream.
233233
* @throws IOException if there is an error reading or writing to the streams.
234234
*/
@@ -244,7 +244,6 @@ private static void copyStream(final InputStream in, final OutputStream out) thr
244244
}
245245

246246
// -----------------------------------------------------------------------------------------------------------------
247-
248247
/**
249248
* Splits the given comma-delimited string into an an array. Leading/trailing spaces in list items will be trimmed.
250249
*
@@ -303,12 +302,9 @@ private void load() {
303302
loadEnvironmentProperties();
304303
}
305304

306-
checkProfileProperty();
305+
handlePropertySubstitution();
307306

308-
// Now perform variable substitution.
309-
for (String key : backing.keySet()) {
310-
substitute(key);
311-
}
307+
checkProfileProperty();
312308

313309
// Dump Header Info
314310
LOG.info(getDumpHeader());
@@ -715,8 +711,8 @@ private void loadSystemProperties() {
715711
boolean overWriteOnly = getBoolean(USE_SYSTEM_OVERWRITEONLY, false);
716712
List<String> allowedPrefixes = Arrays.asList(getStringArray(USE_SYSTEM_PREFIXES));
717713
System
718-
.getProperties()
719-
.forEach((key, value) -> mergeExternalProperty("System Properties",
714+
.getProperties()
715+
.forEach((key, value) -> mergeExternalProperty("System Properties",
720716
(String) key,
721717
(String) value,
722718
overWriteOnly,
@@ -729,25 +725,25 @@ private void loadSystemProperties() {
729725
private void loadEnvironmentProperties() {
730726
List<String> allowedPrefixes = Arrays.asList(getStringArray(USE_OSENV_PREFIXES));
731727
System
732-
.getenv()
733-
.forEach((key, value) -> mergeExternalProperty("Environment Properties", key, value, false, allowedPrefixes));
728+
.getenv()
729+
.forEach((key, value) -> mergeExternalProperty("Environment Properties", key, value, false, allowedPrefixes));
734730
}
735731

736732
/**
737733
* Merge the external property.
738734
*
739-
* @param location the location of the properties
740-
* @param key the property key
741-
* @param value the property value
742-
* @param overWriteOnly true if only overwrite existing properties
735+
* @param location the location of the properties
736+
* @param key the property key
737+
* @param value the property value
738+
* @param overWriteOnly true if only overwrite existing properties
743739
* @param allowedPrefixes the list of allowed property prefixes
744740
*/
745741
private void mergeExternalProperty(
746-
final String location,
747-
final String key,
748-
final String value,
749-
final boolean overWriteOnly,
750-
final List<String> allowedPrefixes) {
742+
final String location,
743+
final String key,
744+
final String value,
745+
final boolean overWriteOnly,
746+
final List<String> allowedPrefixes) {
751747

752748
// Check for "include" keys (should not come from System or Environment Properties)
753749
if (INCLUDE.equals(key) || INCLUDE_AFTER.equals(key)) {
@@ -772,7 +768,7 @@ private void mergeExternalProperty(
772768
* Check allowed prefixes.
773769
*
774770
* @param allowedPrefixes the list of allowed prefixes
775-
* @param key the key to check
771+
* @param key the key to check
776772
* @return true if the key is an allowed prefix
777773
*/
778774
private boolean isAllowedKeyPrefix(final List<String> allowedPrefixes, final String key) {
@@ -1270,7 +1266,7 @@ public Configuration subset(final String prefix) {
12701266
/**
12711267
* Returns a sub-set of the parameters contained in this configuration.
12721268
*
1273-
* @param prefix the prefix of the parameter keys which should be included.
1269+
* @param prefix the prefix of the parameter keys which should be included.
12741270
* @param truncate if true, the prefix is truncated in the returned properties.
12751271
* @return the properties sub-set, may be empty.
12761272
*/
@@ -1309,7 +1305,7 @@ protected Properties getSubProperties(final String prefix, final boolean truncat
13091305
}
13101306

13111307
/**
1312-
* @param key the property key
1308+
* @param key the property key
13131309
* @param defolt the default value if key not available
13141310
* @return the property value or null
13151311
*/
@@ -1341,7 +1337,7 @@ protected String get(final String key) {
13411337
/**
13421338
* Add or Modify a property at runtime.
13431339
*
1344-
* @param name the property name
1340+
* @param name the property name
13451341
* @param value the property value
13461342
*/
13471343
protected void addOrModifyProperty(final String name, final String value) {
@@ -1367,6 +1363,16 @@ protected void addOrModifyProperty(final String name, final String value) {
13671363
handlePropertiesChanged();
13681364
}
13691365

1366+
/**
1367+
* Handle the substitution of property values.
1368+
*/
1369+
protected void handlePropertySubstitution() {
1370+
// Now perform variable substitution.
1371+
for (String key : backing.keySet()) {
1372+
substitute(key);
1373+
}
1374+
}
1375+
13701376
/**
13711377
* Handle a property change.
13721378
*/
@@ -1378,8 +1384,8 @@ protected void handlePropertiesChanged() {
13781384
}
13791385

13801386
/**
1381-
* Set the current Profile if it has been set as property. An application defined property overrides,
1382-
* a JVM System property which overrides a OS environment variable
1387+
* Set the current Profile if it has been set as property. An application defined property overrides, a JVM System
1388+
* property which overrides a OS environment variable
13831389
*/
13841390
protected void checkProfileProperty() {
13851391

@@ -1452,8 +1458,9 @@ protected String getEnvironmentKey(final String key) {
14521458
/**
14531459
* A helper class for properties which are being loaded into the {@link DefaultConfiguration}.
14541460
*
1455-
* <p>This is used to ensure on the call of put(key, value) is immediately loaded into the
1456-
* {@link DefaultConfiguration} to respect the order hierarchy for the configuration.</p>
1461+
* <p>
1462+
* This is used to ensure on the call of put(key, value) is immediately loaded into the {@link DefaultConfiguration}
1463+
* to respect the order hierarchy for the configuration.</p>
14571464
*/
14581465
private class IncludeProperties extends Properties {
14591466

@@ -1475,7 +1482,7 @@ private class IncludeProperties extends Properties {
14751482
* Adds a value to the properties set. This has been overridden to support the Configuration extensions (e.g.
14761483
* the "include" directive).
14771484
*
1478-
* @param aKey the key to add
1485+
* @param aKey the key to add
14791486
* @param aValue the value to add
14801487
* @return the old value for the key, or null if there was no previously associated value.
14811488
*/

src/test/java/com/github/bordertech/config/DefaultConfigurationEnvSuffixTest.java renamed to src/test/java/com/github/bordertech/config/DefaultConfigurationEnvProfileSuffixTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* DefaultConfiguration_EnvironmentSuffix_Test - JUnit tests for {@link DefaultConfiguration}.
1111
*/
12-
public class DefaultConfigurationEnvSuffixTest {
12+
public class DefaultConfigurationEnvProfileSuffixTest {
1313

1414
private static final String TEST_PROPERTY_KEY = "default.propertyKey";
1515
private static final String DEFAULT_PROPERTY_VALUE = "defaultValue";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.bordertech.config;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Unit tests to check substitution of values for {@link DefaultConfiguration}.
8+
*/
9+
public class DefaultConfigurationSubstitutionBasicTest {
10+
11+
private static final String EXPECTED_VALUEZ = "valueZ";
12+
private static final String EXPECTED_VALUEZ2 = "valueZ2";
13+
14+
@Test
15+
public void testSubstitutePropertyValuesBeforeDefined() {
16+
DefaultConfiguration config = new DefaultConfiguration("com/github/bordertech/config/DefaultConfigurationTestSubstitution.properties");
17+
Assert.assertEquals("Value for X should match Z value", EXPECTED_VALUEZ, config.get("substitute.test.X"));
18+
Assert.assertEquals("Value for Y should match Z value", EXPECTED_VALUEZ, config.get("substitute.test.Y"));
19+
Assert.assertEquals("Value for Z should be valueZ", EXPECTED_VALUEZ, config.get("substitute.test.Z"));
20+
}
21+
22+
@Test
23+
public void testSubstitutePropertyValuesReverseOrder() {
24+
DefaultConfiguration config = new DefaultConfiguration("com/github/bordertech/config/DefaultConfigurationTestSubstitution.properties");
25+
Assert.assertEquals("Value for X2 should match Z2 value", EXPECTED_VALUEZ2, config.get("substitute.test.X2"));
26+
Assert.assertEquals("Value for Y2 should match Z2 value", EXPECTED_VALUEZ2, config.get("substitute.test.Y2"));
27+
Assert.assertEquals("Value for Z2 should be valueZ2", EXPECTED_VALUEZ2, config.get("substitute.test.Z2"));
28+
}
29+
30+
@Test
31+
public void testSubstituteProfileSuffixKey() {
32+
DefaultConfiguration config = new DefaultConfiguration("com/github/bordertech/config/DefaultConfigurationTestSubstitutionProfileSuffix.properties");
33+
Assert.assertEquals("Profile value was not substituted", "TEST", config.get(DefaultConfiguration.PROFILE_PROPERTY));
34+
Assert.assertEquals("Property value with profile suffix not correct", "PROFILE_TEST", config.get("substitute.test.profile"));
35+
}
36+
37+
@Test
38+
public void testSubstituteEnvSuffixKey() {
39+
DefaultConfiguration config = new DefaultConfiguration("com/github/bordertech/config/DefaultConfigurationTestSubstitutionEnvSuffix.properties");
40+
Assert.assertEquals("Environment value was not substituted", "TEST", config.get(DefaultConfiguration.ENVIRONMENT_PROPERTY));
41+
Assert.assertEquals("Property value with environment suffix not correct", "ENV_TEST", config.get("substitute.test.env"));
42+
}
43+
44+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
############################################################################
2-
# This property file is for the DefaultConfigurationEnvSuffix_Test jUnit test
2+
# This property file is for the DefaultConfigurationEnvSuffixTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################
66

7+
## Load the config environment property from the Systems Properties
78
bordertech.config.parameters.useSystemProperties=true
89
bordertech.config.parameters.useSystemPrefixes=bordertech.config.environment
910

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################
@@ -69,7 +69,7 @@ test.definedAfterInclude=mainValue
6969
includeAfter.test.path=com/github/bordertech/config
7070

7171
# -------------------------------------------------------------------------------------------------
72-
# IncludeAfter tests
72+
# IncludeAfter tests
7373
# -------------------------------------------------------------------------------------------------
7474
# This property is redefined in the includeAfter. The includeAfter definition should be effective.
7575
test.definedBeforeIncludeAfter=mainValue
@@ -83,7 +83,7 @@ test.plus.equals+=second
8383

8484
includeAfter=${includeAfter.test.path}/DefaultConfigurationTest_includeAfterWithSubstitution.properties
8585

86-
# Here for the test to prove that the immediately above includeAfter will override this
86+
# Here for the test to prove that the immediately above includeAfter will override this
8787
test.includeAfter.secondaryString=anIncludeAfterWillOvverrideThis
8888

8989
include=./path/to/a/non/existent/file.properties

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationDumpConsoleTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationDumpConsoleTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationDumpFileTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationDumpFileTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationEnvironmentTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationEnvironmentTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationSystemTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationSystemTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################################
2-
# This property file is for the DefaultConfiguration_Test jUnit test
2+
# This property file is for the DefaultConfigurationSystemTest jUnit test
33
# It must not be included or be included by property file that is not
44
# directly related to the test.
55
############################################################################

0 commit comments

Comments
 (0)