Repository: james-project
Updated Branches:
  refs/heads/master 175f1f29c -> 946c68bea


JAMES-2422 Test support for env variables in configuration files


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1694051f
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1694051f
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1694051f

Branch: refs/heads/master
Commit: 1694051f6c40801cdb09d26bb6cb0b10a3e22e58
Parents: 175f1f2
Author: Raphael Ouazana <raphael.ouaz...@linagora.com>
Authored: Tue Jun 12 10:04:05 2018 +0200
Committer: Matthieu Baechler <matth...@apache.org>
Committed: Wed Jun 13 16:56:17 2018 +0200

----------------------------------------------------------------------
 server/container/guice/guice-common/pom.xml     |  6 +++++
 .../utils/FileConfigurationProviderTest.java    | 25 +++++++++++++++++++-
 .../guice-common/src/test/resources/test.xml    |  4 +++-
 3 files changed, 33 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1694051f/server/container/guice/guice-common/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/pom.xml 
b/server/container/guice/guice-common/pom.xml
index 2c0be52..9f03660 100644
--- a/server/container/guice/guice-common/pom.xml
+++ b/server/container/guice/guice-common/pom.xml
@@ -153,6 +153,12 @@
             <artifactId>metrics-logger</artifactId>
         </dependency>
         <dependency>
+            <groupId>com.github.stefanbirkner</groupId>
+            <artifactId>system-rules</artifactId>
+            <version>1.18.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>com.github.steveash.guavate</groupId>
             <artifactId>guavate</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/1694051f/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FileConfigurationProviderTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FileConfigurationProviderTest.java
 
b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FileConfigurationProviderTest.java
index 6120ff7..74678ec 100644
--- 
a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FileConfigurationProviderTest.java
+++ 
b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FileConfigurationProviderTest.java
@@ -26,7 +26,9 @@ import 
org.apache.james.server.core.configuration.Configuration;
 import org.apache.james.server.core.configuration.FileConfigurationProvider;
 import org.apache.james.server.core.filesystem.FileSystemImpl;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.contrib.java.lang.system.EnvironmentVariables;
 
 public class FileConfigurationProviderTest {
 
@@ -34,17 +36,26 @@ public class FileConfigurationProviderTest {
     private static final String CONFIG_KEY_2 = "property";
     private static final String CONFIG_KEY_4 = "james";
     private static final String CONFIG_KEY_5 = "internal";
+    private static final String CONFIG_KEY_ENV = "env";
+    private static final String CONFIG_KEY_NOT_ENV = "notEnv";
     private static final String VALUE_1 = "0";
     private static final String VALUE_2 = "awesome";
     private static final String VALUE_3 = "james";
+    private static final String VALUE_NOT_ENV = "${env:MY_NOT_IN_ENV_VAR}";
+    private static final String ENVIRONMENT_SET_VALUE = "testvalue";
     private static final String FAKE_CONFIG_KEY = "fake";
     private static final String ROOT_CONFIG_KEY = "test";
     private static final String CONFIG_SEPARATOR = ".";
 
     private FileConfigurationProvider configurationProvider;
 
+    @Rule
+    public final EnvironmentVariables environmentVariables = new 
EnvironmentVariables();
+
     @Before
     public void setUp() {
+        environmentVariables.set("MY_ENV_VAR", ENVIRONMENT_SET_VALUE);
+        environmentVariables.clear("MY_NOT_IN_ENV_VAR");
         Configuration configuration = Configuration.builder()
             .workingDirectory("../")
             .configurationFromClasspath()
@@ -78,7 +89,8 @@ public class FileConfigurationProviderTest {
         HierarchicalConfiguration hierarchicalConfiguration = 
configurationProvider.getConfiguration(ROOT_CONFIG_KEY);
         
assertThat(hierarchicalConfiguration.getKeys()).containsOnly(CONFIG_KEY_1,
                 String.join(CONFIG_SEPARATOR, CONFIG_KEY_4, CONFIG_KEY_2),
-                String.join(CONFIG_SEPARATOR, CONFIG_KEY_4, CONFIG_KEY_5, 
CONFIG_KEY_2));
+                String.join(CONFIG_SEPARATOR, CONFIG_KEY_4, CONFIG_KEY_5, 
CONFIG_KEY_2),
+                CONFIG_KEY_ENV, CONFIG_KEY_NOT_ENV);
         
assertThat(hierarchicalConfiguration.getProperty(CONFIG_KEY_1)).isEqualTo(VALUE_1);
     }
 
@@ -118,4 +130,15 @@ public class FileConfigurationProviderTest {
         configurationProvider.getConfiguration(String.join(CONFIG_SEPARATOR, 
ROOT_CONFIG_KEY, FAKE_CONFIG_KEY));
     }
 
+    @Test
+    public void 
getConfigurationShouldNotReplaceEnvironmentVariableWhenNotSet() throws 
Exception {
+        HierarchicalConfiguration hierarchicalConfiguration = 
configurationProvider.getConfiguration(ROOT_CONFIG_KEY);
+        
assertThat(hierarchicalConfiguration.getString(CONFIG_KEY_NOT_ENV)).isEqualTo(VALUE_NOT_ENV);
+    }
+
+    @Test
+    public void getConfigurationShouldReplaceEnvironmentVariableWhenSet() 
throws Exception {
+        HierarchicalConfiguration hierarchicalConfiguration = 
configurationProvider.getConfiguration(ROOT_CONFIG_KEY);
+        
assertThat(hierarchicalConfiguration.getString(CONFIG_KEY_ENV)).isEqualTo(ENVIRONMENT_SET_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1694051f/server/container/guice/guice-common/src/test/resources/test.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/test/resources/test.xml 
b/server/container/guice/guice-common/src/test/resources/test.xml
index 8d89ecc..d34c3c4 100644
--- a/server/container/guice/guice-common/src/test/resources/test.xml
+++ b/server/container/guice/guice-common/src/test/resources/test.xml
@@ -25,4 +25,6 @@
           <property>james</property>
         </internal>
     </james>
-</test>
\ No newline at end of file
+    <env>${env:MY_ENV_VAR}</env>
+    <notEnv>${env:MY_NOT_IN_ENV_VAR}</notEnv>
+</test>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to