Skip to content

Commit 885a669

Browse files
Merge pull request #43 from mccabd/feature/issue34
Issue 34 resource locations
2 parents 935b94f + e544b76 commit 885a669

File tree

6 files changed

+77
-31
lines changed

6 files changed

+77
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Refactor of substitution code to use apache commons text
1212
* includes can define substitution variables e.g. `include=${previously.defined.property.key}/special-file.properties`
1313
* Increase in unit tests and code coverage.
14+
* Issue 34 - Addition of User Home directory as an option to look for URL resources.
1415

1516
## 1.0.6
1617

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ The default implementation looks for the following resources either as a classpa
6565
- `bordertech-defaults.properties` - framework defaults
6666
- `bordertech-app.properties` - application properties
6767
- `bordertech-local.properties` - local developer properties
68+
69+
URL resources can be loaded from the user home directory or the current working directory
6870

6971
Projects will usually use `bordertech-app.properties` resource files.
7072

pom.xml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@
5353
<dependency>
5454
<groupId>org.apache.commons</groupId>
5555
<artifactId>commons-lang3</artifactId>
56-
<version>3.9</version>
56+
<version>3.11</version>
5757
</dependency>
5858
<dependency>
5959
<groupId>org.apache.commons</groupId>
6060
<artifactId>commons-text</artifactId>
61-
<version>1.8</version>
61+
<version>1.9</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>commons-io</groupId>
65+
<artifactId>commons-io</artifactId>
66+
<version>2.8.0</version>
6267
</dependency>
6368
<!-- Junit -->
6469
<dependency>
@@ -73,13 +78,6 @@
7378
<scope>test</scope>
7479
</dependency>
7580

76-
<dependency>
77-
<groupId>commons-io</groupId>
78-
<artifactId>commons-io</artifactId>
79-
<version>2.6</version>
80-
<scope>test</scope>
81-
</dependency>
82-
8381
</dependencies>
8482

8583
</project>

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.apache.commons.configuration.Configuration;
44
import org.apache.commons.configuration.ConversionException;
55
import org.apache.commons.configuration.MapConfiguration;
6+
import org.apache.commons.io.FileUtils;
67
import org.apache.commons.lang3.BooleanUtils;
78
import org.apache.commons.lang3.ObjectUtils;
89
import org.apache.commons.lang3.StringUtils;
10+
import org.apache.commons.lang3.SystemUtils;
911
import org.apache.commons.lang3.tuple.ImmutablePair;
1012
import org.apache.commons.lang3.tuple.Pair;
1113
import org.apache.commons.logging.Log;
@@ -516,23 +518,34 @@ private void load(final String resourceName) {
516518
loadResourceContents(contents);
517519
}
518520

519-
// Load the resource as a FILE (if exists)
520-
File file = new File(resourceName);
521-
if (file.exists()) {
521+
// Load the resource as a FILE from the user home directory (if exists)
522+
if (loadFile(FileUtils.getFile(SystemUtils.getUserHome(), resourceName))) {
523+
found = true;
524+
}
525+
526+
// Load the resource as a FILE from the user directory (if exists)
527+
if (loadFile(FileUtils.getFile(SystemUtils.getUserDir(), resourceName))) {
522528
found = true;
523-
loadFileResource(file);
524529
}
525530

526531
if (!found) {
527532
recordMessage("Did not find resource " + resourceName);
528533
}
529-
} catch (IOException | IllegalArgumentException ex) {
534+
} catch (IOException ex) {
530535
// Most likely a "Malformed uxxxx encoding." error, which is
531536
// usually caused by a developer forgetting to escape backslashes
532537
recordException(ex);
533538
}
534539
}
535540

541+
private boolean loadFile(final File file) throws IOException {
542+
if (file.exists()) {
543+
loadFileResource(file);
544+
return true;
545+
}
546+
return false;
547+
}
548+
536549
/**
537550
* Find the resources from the class loader as there maybe more than one.
538551
*

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.apache.commons.configuration.Configuration;
44
import org.apache.commons.configuration.ConversionException;
5+
import org.apache.commons.io.FileUtils;
6+
import org.apache.commons.lang3.SystemUtils;
57
import org.junit.Assert;
68
import org.junit.Before;
79
import org.junit.Test;
@@ -77,7 +79,7 @@ public class DefaultConfigurationTest {
7779
@Before
7880
public void loadProperties() {
7981
config = new DefaultConfiguration(
80-
"com/github/bordertech/config/DefaultConfigurationTest.properties");
82+
"com/github/bordertech/config/DefaultConfigurationTest.properties");
8183
}
8284

8385
@Test
@@ -136,7 +138,7 @@ public void testGetSubProperties() {
136138
assertPropertyEquals(BOOLEAN_FALSE_PROPERTY_KEY, "false", props);
137139
assertPropertyEquals("simple.listPropertyKey", "item1,item2,item3", props);
138140
assertPropertyEquals("simple.propertiesPropertyKey", "key1=value1,key2=value2,key3=value3",
139-
props);
141+
props);
140142

141143
// Now test with the prefix truncated
142144
props = config.getSubProperties("simple.", true);
@@ -212,7 +214,7 @@ public void testGetLong() {
212214
Assert.assertEquals("Incorrect long value for missing key", 0, config.getLong(MISSING_PROPERTY_KEY));
213215

214216
Assert.assertEquals("Incorrect default long value for missing key", MISSING_PROPERTY_VAL,
215-
config.getLong(MISSING_PROPERTY_KEY, MISSING_PROPERTY_VAL));
217+
config.getLong(MISSING_PROPERTY_KEY, MISSING_PROPERTY_VAL));
216218

217219
Assert.assertEquals("Incorrect default long value for missing key",
218220
Long.valueOf(MISSING_PROPERTY_VAL), config.getLong(MISSING_PROPERTY_KEY, Long.valueOf(MISSING_PROPERTY_VAL)));
@@ -325,7 +327,7 @@ public void testGetBigDecimal() {
325327
BigDecimal.valueOf(0.0), config.getBigDecimal(MISSING_PROPERTY_KEY));
326328

327329
Assert.assertEquals("Incorrect default BigDecimal value for missing key",
328-
BigDecimal.valueOf(MISSING_PROPERTY_VAL), config.getBigDecimal(MISSING_PROPERTY_KEY, BigDecimal.
330+
BigDecimal.valueOf(MISSING_PROPERTY_VAL), config.getBigDecimal(MISSING_PROPERTY_KEY, BigDecimal.
329331
valueOf(MISSING_PROPERTY_VAL)));
330332
}
331333

@@ -337,13 +339,13 @@ public void testGetInvalidBigDecimal() {
337339
@Test
338340
public void testGetBigInteger() {
339341
Assert.assertEquals("Incorrect BigInteger value for " + INT_PROPERTY_KEY,
340-
BigInteger.valueOf(INT_PROPERTY_VAL), config.getBigInteger(INT_PROPERTY_KEY));
342+
BigInteger.valueOf(INT_PROPERTY_VAL), config.getBigInteger(INT_PROPERTY_KEY));
341343

342344
Assert.assertEquals("Incorrect BigInteger value for missing key",
343-
BigInteger.valueOf(0), config.getBigInteger(MISSING_PROPERTY_KEY));
345+
BigInteger.valueOf(0), config.getBigInteger(MISSING_PROPERTY_KEY));
344346

345347
Assert.assertEquals("Incorrect default BigInteger value for missing key",
346-
BigInteger.valueOf(MISSING_PROPERTY_VAL), config.getBigInteger(MISSING_PROPERTY_KEY, BigInteger.
348+
BigInteger.valueOf(MISSING_PROPERTY_VAL), config.getBigInteger(MISSING_PROPERTY_KEY, BigInteger.
347349
valueOf(MISSING_PROPERTY_VAL)));
348350
}
349351

@@ -369,13 +371,13 @@ public void testGetBoolean() {
369371
public void testGetFloat() {
370372
final float expectedVal = 234.0f;
371373
Assert.assertEquals("Incorrect float value for " + INT_PROPERTY_KEY,
372-
Float.parseFloat("123"), config.getFloat(INT_PROPERTY_KEY), 0.0);
374+
Float.parseFloat("123"), config.getFloat(INT_PROPERTY_KEY), 0.0);
373375

374376
Assert.assertEquals("Incorrect float value for missing key",
375-
0.0f, config.getFloat(MISSING_PROPERTY_KEY), 0.0);
377+
0.0f, config.getFloat(MISSING_PROPERTY_KEY), 0.0);
376378

377379
Assert.assertEquals("Incorrect default float value for missing key",
378-
expectedVal, config.getFloat(MISSING_PROPERTY_KEY, expectedVal), 0.0);
380+
expectedVal, config.getFloat(MISSING_PROPERTY_KEY, expectedVal), 0.0);
379381

380382
Assert.assertEquals("Incorrect default float value for missing key",
381383
Float.valueOf(MISSING_PROPERTY_VAL), config.getFloat(MISSING_PROPERTY_KEY, Float.valueOf(MISSING_PROPERTY_VAL)));
@@ -401,7 +403,7 @@ public void testGetDouble() {
401403
0.0, config.getDouble(MISSING_PROPERTY_KEY), 0.0);
402404

403405
Assert.assertEquals("Incorrect default double value for missing key",
404-
expectedVal, config.getDouble(MISSING_PROPERTY_KEY, MISSING_PROPERTY_VAL), 0.0);
406+
expectedVal, config.getDouble(MISSING_PROPERTY_KEY, MISSING_PROPERTY_VAL), 0.0);
405407

406408
Assert.assertEquals("Incorrect default double value for missing key",
407409
Double.valueOf(MISSING_PROPERTY_VAL), config.getDouble(MISSING_PROPERTY_KEY, Double.valueOf(MISSING_PROPERTY_VAL)));
@@ -704,6 +706,37 @@ public void testLoadWithPhysicalFile() throws Exception {
704706
Assert.assertEquals("physicalFileIncludeValue", config.getString("physical.file.include"));
705707
}
706708

709+
@Test
710+
public void testLoadFileFromUserHome() throws Exception {
711+
712+
Properties props = new Properties();
713+
props.setProperty("user.home.file.include", "userHomeIncludeValue");
714+
props.store(new FileWriter(FileUtils.getFile(SystemUtils.getUserHome(), "DefaultConfigTestIncludeUserHome.properties")), null);
715+
716+
DefaultConfiguration config = new DefaultConfiguration(
717+
"com/github/bordertech/config/DefaultConfigurationTest_include.properties");
718+
719+
Assert.assertEquals("userHomeIncludeValue", config.getString("user.home.file.include"));
720+
}
721+
722+
@Test
723+
public void testLoadFileFromMultiplePlaces() throws Exception {
724+
725+
Properties props = new Properties();
726+
props.setProperty("user.home.file.include.multi", "userHomeIncludeValue");
727+
props.store(new FileWriter(FileUtils.getFile(SystemUtils.getUserHome(), "DefaultConfigTestIncludeMulti.properties")), null);
728+
props.clear();
729+
730+
props = new Properties();
731+
props.setProperty("user.home.file.include.multi", "userDirIncludeValue");
732+
props.store(new FileWriter(FileUtils.getFile(SystemUtils.getUserHome(), "DefaultConfigTestIncludeMulti.properties")), null);
733+
734+
DefaultConfiguration config = new DefaultConfiguration(
735+
"com/github/bordertech/config/DefaultConfigurationTest_include.properties");
736+
737+
Assert.assertEquals("userDirIncludeValue", config.getString("user.home.file.include.multi"));
738+
}
739+
707740
/**
708741
* Asserts that the configuration contains the given key/value.
709742
*
@@ -717,12 +750,12 @@ private void assertPropertyEquals(final String key, final Object expected) {
717750
/**
718751
* Asserts that the given properties contains the given key/value.
719752
*
720-
* @param key the property key
753+
* @param key the property key
721754
* @param expected the expected property value.
722-
* @param props the properties to search in.
755+
* @param props the properties to search in.
723756
*/
724757
private void assertPropertyEquals(final String key, final Object expected,
725-
final Properties props) {
758+
final Properties props) {
726759
Assert.assertEquals("Incorrect value for " + key, expected, props.get(key));
727760
}
728761
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
# Single-level substitution from other property files
1212
substitute.multiPart1Key=multiPart1Value
13-
1413
# -------------------------------------------------------------------------------------------------
1514
# Include tests
1615
# -------------------------------------------------------------------------------------------------
17-
1816
test.definedBeforeInclude=includeValue
1917
test.definedAfterInclude=includeValue
20-
2118
include=./target/DefaultConfigTestInclude.properties
19+
include=DefaultConfigTestIncludeUserHome.properties
20+
include=DefaultConfigTestIncludeMulti.properties

0 commit comments

Comments
 (0)