Skip to content

Commit

Permalink
Merge pull request #164 from hsrinara/bug/systemprop
Browse files Browse the repository at this point in the history
[Java] Correctly load system properties in SystemUtil
  • Loading branch information
mjpt777 authored Jan 30, 2019
2 parents 7983876 + 832df4f commit b4951af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion agrona/src/main/java/org/agrona/SystemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public static String threadDump()
*/
public static void loadPropertiesFile(final String filenameOrUrl)
{
final Properties properties = new Properties(System.getProperties());
final Properties properties = new Properties();
System.getProperties().forEach(properties::put);

final URL resource = ClassLoader.getSystemClassLoader().getResource(filenameOrUrl);
if (null != resource)
Expand Down
36 changes: 32 additions & 4 deletions agrona/src/test/java/org/agrona/SystemUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package org.agrona;

import org.hamcrest.Matchers;
import org.junit.Test;

import static org.agrona.SystemUtil.parseDuration;
import static org.agrona.SystemUtil.parseSize;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;

import org.hamcrest.Matchers;
import org.junit.Test;

public class SystemUtilTest
{
Expand Down Expand Up @@ -68,4 +68,32 @@ public void shouldThrowWhenParseSizeOverflows()
{
parseSize("", 8589934592L + "g");
}

@Test
public void shouldDoNothingToSystemPropsWhenLoadingFileWhichDoesNotExist() {
final int originalSystemPropSize = System.getProperties().size();
SystemUtil.loadPropertiesFile("$unknown-file$");
assertThat(originalSystemPropSize, Matchers.equalTo(System.getProperties().size()));
}

@Test
public void shouldMergeMultiplePropFilesTogether() {
assertThat(System.getProperty("TestFileA.foo"), Matchers.isEmptyOrNullString());
assertThat(System.getProperty("TestFileB.foo"), Matchers.isEmptyOrNullString());

SystemUtil.loadPropertiesFiles(new String[]{"TestFileA.properties", "TestFileB.properties"});

assertThat(System.getProperty("TestFileA.foo"), Matchers.equalTo("AAA"));
assertThat(System.getProperty("TestFileB.foo"), Matchers.equalTo("BBB"));
}

@Test
public void shouldOverrideSystemPropertiesWithConfigFromPropFile() {
System.setProperty("TestFileA.foo", "ToBeOverridden");
assertThat(System.getProperty("TestFileA.foo"), Matchers.equalTo("ToBeOverridden"));

SystemUtil.loadPropertiesFile("TestFileA.properties");

assertThat(System.getProperty("TestFileA.foo"), Matchers.equalTo("AAA"));
}
}
1 change: 1 addition & 0 deletions agrona/src/test/resources/TestFileA.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TestFileA.foo=AAA
1 change: 1 addition & 0 deletions agrona/src/test/resources/TestFileB.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TestFileB.foo=BBB

0 comments on commit b4951af

Please sign in to comment.