Skip to content

Commit 935b94f

Browse files
Merge pull request #39 from mccabd/feature/issue37-Refactor
Refactor with Unit Tests and increase coverage
2 parents 886ade4 + 9e2d9bb commit 935b94f

18 files changed

+1034
-425
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
## Release in-progress
44

5+
## 1.0.7
6+
7+
### API Changes
8+
* Introduction of profile property `bordertech.config.profile` to replace environment property.
9+
10+
### Enhancements
11+
* Refactor of substitution code to use apache commons text
12+
* includes can define substitution variables e.g. `include=${previously.defined.property.key}/special-file.properties`
13+
* Increase in unit tests and code coverage.
14+
515
## 1.0.6
616

717
### API Changes

README.md

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
[![Maven Central](https://img.shields.io/maven-central/v/com.github.bordertech.config/config.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.bordertech.config%22%20AND%20a:%22config%22)
1111

1212
## Content
13-
1413
- [What is Config](#what-is-config)
1514
- [Why use Config](#why-use-config)
1615
- [Getting started](#getting-started)
@@ -29,7 +28,6 @@ The [features](#features) of the [Default Configuration](https://github.com/Bord
2928
Projects can easily override this default implementation via the [configuration](#configuration) settings.
3029

3130
## Getting started
32-
3331
Add dependency:
3432

3533
``` xml
@@ -62,8 +60,7 @@ my.example=a-override-value
6260
## Features
6361

6462
### Predefined property resources
65-
66-
The default implementation looks for the following resources:
63+
The default implementation looks for the following resources either as a classpath resource or a URL:
6764

6865
- `bordertech-defaults.properties` - framework defaults
6966
- `bordertech-app.properties` - application properties
@@ -76,29 +73,61 @@ The priority of the properties is in reverse order to the list of resources (i.e
7673
The resources loaded into the Configuration can be overridden via [configuration](#configuration) settings.
7774

7875
### Include resources
79-
80-
Other property files can be included from the predefined property files:
76+
Other property files can be included from other predefined property files.
77+
If the "include" property is defined, it is treated as a (comma-separated) list of additional resources (classpath resource or a URL)
78+
to load that are processed immediately within the current resource being loaded
8179

8280
``` java properties
83-
include=another.properties
81+
include=include_resource_1.properties[,include_resource_2.properties]
8482
```
8583

86-
### Environment Suffix
84+
### IncludeAfter resources
85+
Other property files can be included from the predefined property file after the current set has loaded.
86+
If this property is defined, it is treated as a (comma-separated) list of additional resources (classpath resource or a URL)
87+
to load that are processed after the current (set of) resources have loaded.
88+
89+
``` java properties
90+
includeAfter=include_resource_1.properties[,include_resource_2.properties]
91+
```
92+
93+
### += Append values to predefined properties
94+
Config also allows for the ability to append values to properties already defined.
95+
This is done using '+=' instead of '=' on a key-value pair. Suggested use case is you have a global default set and then
96+
you want to append application specific values to the default values for access within an application.
97+
98+
``` java properties
99+
# Defined in default.properties
100+
already.defined.key+=value1
101+
102+
#Defined in app.properties
103+
already.defined.key+=value2,value3
104+
```
105+
106+
`Config.getInstance().get("already.defined.key")` returns `value1,value2,value3`
87107

88-
It is possible to define properties to only take effect in a certain environment.
108+
### Profiles
109+
Profiles allow you to map properties to different profiles - for example, dev, test, prod or mock.
110+
We can activate these profiles in different environments to set(override) the properties we need.
111+
The profile property is generally to be defined as either an OS environment variable or a JVM system property.
112+
However, it can be set in a properties file which is useful in unit testing or testing on a local environment.
89113

90-
When the runtime property `bordertech.config.environment` is set, it is used as the suffix for each property lookup:
114+
When a property with the key `bordertech.config.profile` is set, it is used as the suffix for each property lookup:
91115

92116
``` java properties
93-
## MOCK Environment
94-
bordertech.config.environment=MOCK
117+
## MOCK Environment set as an Environment or JVM System property only
118+
bordertech.config.profile=MOCK
95119

96120
my.example.property.MOCK=mocking
97121
my.example.property.PROD=proding
98122
my.example.property=defaulting
99123
```
100124

101-
If no property exists with the current environment suffix then the default property (ie no suffix) value is used.
125+
The Environment Suffix `bordertech.config.environment` feature has been deprecated and will be removed in the next
126+
major release but is still honoured within the profile feature.
127+
128+
The order of precedence:
129+
- `bordertech.config.profile` defined as a property anywhere
130+
- `bordertech.config.environment` defined as a property anywhere(Deprecated - to be removed next major release)
102131

103132
### Touchfile
104133

@@ -174,7 +203,8 @@ The following methods in the `Config` class are useful for unit testing:
174203

175204
## Configuration
176205

177-
The initial configuration of `Config` can be overridden by setting properties in a file `bordertech-config.properties`. The file name can be overriden via a System or Environment property `BT_CONFIG_FILE`.
206+
The initial configuration of `Config` can be overridden by setting properties in a file `bordertech-config.properties`.
207+
The default `bordertech-config.properties` file name can also be overriden via a System or Environment property `BT_CONFIG_FILE`.
178208

179209
The following options can be set:-
180210

@@ -206,7 +236,14 @@ bordertech.config.resource.append=my-project.properties
206236

207237
[ConfigurationLoader](https://github.com/BorderTech/java-config/blob/master/src/main/java/com/github/bordertech/config/ConfigurationLoader.java) is the [SPI](https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html) interface for classes that can load a custom configuration.
208238

209-
By default, the SPI lookup is enabled and if found, will be appended to the default implementation.
239+
By default, the SPI lookup is enabled and if found, it will create the custom configuration
240+
241+
If the `bordertech.config.spi.append.default` is true the Default Configuration will also be appended to the configuration.
242+
243+
### Best Practice
244+
245+
When using java-config in a container and setting specific properties for that container instance,
246+
this can be achieved by property file(s) placed in the container that can be referred to and included within the application at runtime.
210247

211248
## Contributing
212249

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,31 @@
5555
<artifactId>commons-lang3</artifactId>
5656
<version>3.9</version>
5757
</dependency>
58+
<dependency>
59+
<groupId>org.apache.commons</groupId>
60+
<artifactId>commons-text</artifactId>
61+
<version>1.8</version>
62+
</dependency>
5863
<!-- Junit -->
5964
<dependency>
6065
<groupId>org.junit.vintage</groupId>
6166
<artifactId>junit-vintage-engine</artifactId>
6267
</dependency>
68+
69+
<dependency>
70+
<groupId>org.awaitility</groupId>
71+
<artifactId>awaitility</artifactId>
72+
<version>4.0.3</version>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>commons-io</groupId>
78+
<artifactId>commons-io</artifactId>
79+
<version>2.6</version>
80+
<scope>test</scope>
81+
</dependency>
82+
6383
</dependencies>
6484

6585
</project>

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.github.bordertech.config;
22

3+
import org.apache.commons.configuration.CompositeConfiguration;
4+
import org.apache.commons.configuration.Configuration;
5+
import org.apache.commons.configuration.MapConfiguration;
6+
import org.apache.commons.lang.StringUtils;
7+
38
import java.beans.PropertyChangeListener;
49
import java.util.ArrayList;
510
import java.util.HashMap;
@@ -8,10 +13,6 @@
813
import java.util.List;
914
import java.util.ServiceLoader;
1015
import java.util.Set;
11-
import org.apache.commons.configuration.CompositeConfiguration;
12-
import org.apache.commons.configuration.Configuration;
13-
import org.apache.commons.configuration.MapConfiguration;
14-
import org.apache.commons.lang.StringUtils;
1516

1617
/**
1718
* The Config class is the central access point to the configuration mechanism, and is used to read or modify the
@@ -185,7 +186,7 @@ public static void addPropertyChangeListener(final PropertyChangeListener listen
185186
* Load the configuration.
186187
*/
187188
private static void loadConfiguration() {
188-
Configuration config = checkSLIConfiguration();
189+
Configuration config = checkSPIConfiguration();
189190
if (config == null) {
190191
config = getDefaultConfiguration();
191192
}
@@ -221,10 +222,9 @@ private static long getTouchFileInterval() {
221222
}
222223

223224
/**
224-
*
225225
* @return a SLI Configuration or null if none available
226226
*/
227-
private static Configuration checkSLIConfiguration() {
227+
private static Configuration checkSPIConfiguration() {
228228

229229
if (!InitHelper.SPI_ENABLED) {
230230
return null;

0 commit comments

Comments
 (0)