This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 2e61afdff93ec4d5881c4ed4035c3ab61fca081f
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Mon Apr 8 16:06:10 2019 +0700

    JAMES-2708 PropertiesProvider should allow fetching the first configuration 
among the names specified
    
    Then updating a configuration name, and falling back to the previous name 
becomes really easy.
---
 server/container/guice/configuration/pom.xml       | 10 ++++
 .../org/apache/james/utils/PropertiesProvider.java | 31 ++++++++--
 .../apache/james/utils/PropertiesProviderTest.java | 68 ++++++++++++++++++++++
 .../configuration/src/test/resources/a.properties  |  1 +
 .../configuration/src/test/resources/b.properties  |  1 +
 .../org/apache/james/FakePropertiesProvider.java   | 20 ++++++-
 6 files changed, 125 insertions(+), 6 deletions(-)

diff --git a/server/container/guice/configuration/pom.xml 
b/server/container/guice/configuration/pom.xml
index 0fec456..ac09511 100644
--- a/server/container/guice/configuration/pom.xml
+++ b/server/container/guice/configuration/pom.xml
@@ -48,6 +48,16 @@
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git 
a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java
 
b/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java
index dcf88da..c474a87 100644
--- 
a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java
+++ 
b/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java
@@ -21,6 +21,8 @@ package org.apache.james.utils;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.util.Arrays;
+import java.util.Optional;
 
 import javax.inject.Inject;
 
@@ -28,7 +30,9 @@ import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.util.OptionalUtils;
 
+import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 
@@ -43,12 +47,31 @@ public class PropertiesProvider {
         this.configurationPrefix = configuration.configurationPath();
     }
 
+    public Configuration getConfigurations(String... filenames) throws 
FileNotFoundException, ConfigurationException {
+        File file = Arrays.stream(filenames)
+            .map(this::getConfigurationFile)
+            .flatMap(OptionalUtils::toStream)
+            .findFirst()
+            .orElseThrow(() -> new 
FileNotFoundException(Joiner.on(",").join(filenames) + " not found"));
+
+        return new PropertiesConfiguration(file);
+    }
+
     public Configuration getConfiguration(String fileName) throws 
FileNotFoundException, ConfigurationException {
         Preconditions.checkArgument(!Strings.isNullOrEmpty(fileName));
-        File file = fileSystem.getFile(configurationPrefix + fileName + 
".properties");
-        if (!file.exists()) {
-            throw new FileNotFoundException();
-        }
+
+        File file = getConfigurationFile(fileName)
+            .orElseThrow(() -> new FileNotFoundException(fileName));
+
         return new PropertiesConfiguration(file);
     }
+
+    private Optional<File> getConfigurationFile(String fileName) {
+        try {
+            return Optional.of(fileSystem.getFile(configurationPrefix + 
fileName + ".properties"))
+                .filter(File::exists);
+        } catch (FileNotFoundException e) {
+            return Optional.empty();
+        }
+    }
 }
\ No newline at end of file
diff --git 
a/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java
 
b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java
new file mode 100644
index 0000000..19589ab
--- /dev/null
+++ 
b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.FileNotFoundException;
+
+import org.apache.james.server.core.configuration.Configuration;
+import org.apache.james.server.core.filesystem.FileSystemImpl;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class PropertiesProviderTest {
+    private PropertiesProvider testee;
+
+    @BeforeEach
+    void setUp() {
+        Configuration configuration = Configuration.builder()
+            .workingDirectory("../")
+            .configurationFromClasspath()
+            .build();
+        FileSystemImpl fileSystem = new 
FileSystemImpl(configuration.directories());
+        testee = new PropertiesProvider(fileSystem, configuration);
+    }
+
+    @Test
+    void getConfigurationsShouldThrowWhenEmpty() {
+        assertThatThrownBy(() -> testee.getConfigurations())
+            .isInstanceOf(FileNotFoundException.class);
+    }
+
+    @Test
+    void getConfigurationsShouldThrowWhenOnlyNotExistingFiles() throws 
Exception {
+        assertThatThrownBy(() -> testee.getConfigurations("c", "d"))
+            .isInstanceOf(FileNotFoundException.class);
+    }
+
+    @Test
+    void getConfigurationShouldReturnFirstExistingFile() throws Exception {
+        assertThat(testee.getConfigurations("a", "b").getString("prop"))
+            .isEqualTo("value1");
+    }
+
+    @Test
+    void 
getConfigurationShouldReturnFirstExistingFileWhenAfterNonExistingFiles() throws 
Exception {
+        assertThat(testee.getConfigurations("c", "b").getString("prop"))
+            .isEqualTo("value2");
+    }
+}
\ No newline at end of file
diff --git 
a/server/container/guice/configuration/src/test/resources/a.properties 
b/server/container/guice/configuration/src/test/resources/a.properties
new file mode 100644
index 0000000..452d1aa
--- /dev/null
+++ b/server/container/guice/configuration/src/test/resources/a.properties
@@ -0,0 +1 @@
+prop=value1
\ No newline at end of file
diff --git 
a/server/container/guice/configuration/src/test/resources/b.properties 
b/server/container/guice/configuration/src/test/resources/b.properties
new file mode 100644
index 0000000..7a76e09
--- /dev/null
+++ b/server/container/guice/configuration/src/test/resources/b.properties
@@ -0,0 +1 @@
+prop=value2
\ No newline at end of file
diff --git 
a/server/container/guice/guice-common/src/test/java/org/apache/james/FakePropertiesProvider.java
 
b/server/container/guice/guice-common/src/test/java/org/apache/james/FakePropertiesProvider.java
index c559c77..ac1d943 100644
--- 
a/server/container/guice/guice-common/src/test/java/org/apache/james/FakePropertiesProvider.java
+++ 
b/server/container/guice/guice-common/src/test/java/org/apache/james/FakePropertiesProvider.java
@@ -20,11 +20,13 @@
 package org.apache.james;
 
 import java.io.FileNotFoundException;
+import java.util.Arrays;
+import java.util.Optional;
 
 import org.apache.commons.configuration.Configuration;
-import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.util.OptionalUtils;
 import org.apache.james.utils.PropertiesProvider;
 
 import com.google.common.collect.ImmutableMap;
@@ -44,7 +46,7 @@ public class FakePropertiesProvider extends 
PropertiesProvider {
 
 
     @Override
-    public Configuration getConfiguration(String fileName) throws 
FileNotFoundException, ConfigurationException {
+    public Configuration getConfiguration(String fileName) throws 
FileNotFoundException {
         if (configurations.containsKey(fileName)) {
             return configurations.get(fileName);
         } else {
@@ -57,6 +59,20 @@ public class FakePropertiesProvider extends 
PropertiesProvider {
         }
     }
 
+    @Override
+    public Configuration getConfigurations(String... filenames) throws 
FileNotFoundException {
+        return Arrays.stream(filenames)
+            .map(filename -> Optional.ofNullable(configurations.get(filename)))
+            .flatMap(OptionalUtils::toStream)
+            .findFirst()
+            .orElseThrow(() -> new FileNotFoundException(
+                "no configuration defined for " +
+                    StringUtils.join(filenames, ",") +
+                    " know configurations are (" +
+                    StringUtils.join(configurations.keySet(), ",") +
+                    ")"));
+    }
+
     public static FakePropertiesProviderBuilder builder() {
         return new FakePropertiesProviderBuilder();
     }


---------------------------------------------------------------------
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