You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/05/20 11:00:29 UTC

[james-project] 03/11: JAMES-3140 Generify configuration

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 63f4c6a8635a529bd4bfd967c4dccd880ccb94af
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu May 14 17:34:35 2020 +0700

    JAMES-3140 Generify configuration
    
    We will be to add per-products fields to it.
    
    This will enable passing per-product configuration modules option as part of it.
---
 .../server/core/configuration/Configuration.java   | 111 ++++++++++----------
 .../core/configuration/BasicConfigurationTest.java | 113 +++++++++++++++++++++
 .../jmap/rfc8621/memory/MemoryEchoMethodTest.java  |   2 +-
 3 files changed, 174 insertions(+), 52 deletions(-)

diff --git a/server/container/core/src/main/java/org/apache/james/server/core/configuration/Configuration.java b/server/container/core/src/main/java/org/apache/james/server/core/configuration/Configuration.java
index 233913c..050d210 100644
--- a/server/container/core/src/main/java/org/apache/james/server/core/configuration/Configuration.java
+++ b/server/container/core/src/main/java/org/apache/james/server/core/configuration/Configuration.java
@@ -27,75 +27,84 @@ import org.apache.james.filesystem.api.JamesDirectoriesProvider;
 import org.apache.james.server.core.JamesServerResourceLoader;
 import org.apache.james.server.core.MissingArgumentException;
 
-public class Configuration {
+public interface Configuration {
 
-    public static final String WORKING_DIRECTORY = "working.directory";
+    String WORKING_DIRECTORY = "working.directory";
 
-    public static Builder builder() {
-        return new Builder();
+    static Basic.Builder builder() {
+        return new Basic.Builder();
     }
 
-    public static class Builder {
+    class Basic implements Configuration {
+        public static class Builder {
+            private Optional<String> rootDirectory;
+            private Optional<String> configurationPath;
 
-        private Optional<String> rootDirectory;
-        private Optional<String> configurationPath;
+            private Builder() {
+                rootDirectory = Optional.empty();
+                configurationPath = Optional.empty();
+            }
 
-        private Builder() {
-            rootDirectory = Optional.empty();
-            configurationPath = Optional.empty();
-        }
+            public Builder workingDirectory(String path) {
+                rootDirectory = Optional.of(path);
+                return this;
+            }
 
-        public Builder workingDirectory(String path) {
-            rootDirectory = Optional.of(path);
-            return this;
-        }
+            public Builder workingDirectory(File file) {
+                rootDirectory = Optional.of(file.getAbsolutePath());
+                return this;
+            }
 
-        public Builder workingDirectory(File file) {
-            rootDirectory = Optional.of(file.getAbsolutePath());
-            return this;
-        }
+            public Builder useWorkingDirectoryEnvProperty() {
+                rootDirectory = Optional.ofNullable(System.getProperty(WORKING_DIRECTORY));
+                if (!rootDirectory.isPresent()) {
+                    throw new MissingArgumentException("Server needs a working.directory env entry");
+                }
+                return this;
+            }
 
-        public Builder useWorkingDirectoryEnvProperty() {
-            rootDirectory = Optional
-                .ofNullable(System.getProperty(WORKING_DIRECTORY));
-            if (!rootDirectory.isPresent()) {
-                throw new MissingArgumentException("Server needs a working.directory env entry");
+            public Builder configurationPath(String path) {
+                configurationPath = Optional.of(path);
+                return this;
             }
-            return this;
-        }
 
-        public Builder configurationPath(String path) {
-            configurationPath = Optional.of(path);
-            return this;
-        }
+            public Builder configurationFromClasspath() {
+                configurationPath = Optional.of(FileSystem.CLASSPATH_PROTOCOL);
+                return this;
+            }
 
-        public Builder configurationFromClasspath() {
-            configurationPath = Optional.of(FileSystem.CLASSPATH_PROTOCOL);
-            return this;
-        }
+            public Configuration.Basic build() {
+
+                String configurationPath = this.configurationPath.orElse(FileSystem.FILE_PROTOCOL_AND_CONF);
+                JamesServerResourceLoader directories = new JamesServerResourceLoader(rootDirectory
+                    .orElseThrow(() -> new MissingArgumentException("Server needs a working.directory env entry")));
 
-        public Configuration build() {
-            return new Configuration(
-                rootDirectory
-                    .orElseThrow(() -> new MissingArgumentException("Server needs a working.directory env entry")),
-                configurationPath.orElse(FileSystem.FILE_PROTOCOL_AND_CONF));
+                return new Basic(
+                    configurationPath,
+                    directories);
+            }
         }
-    }
 
-    private final String configurationPath;
-    private final JamesDirectoriesProvider directoriesProvider;
+        private final String configurationPath;
+        private final JamesDirectoriesProvider directories;
 
-    private Configuration(String rootDirectory, String configurationPath) {
-        this.configurationPath = configurationPath;
-        this.directoriesProvider = new JamesServerResourceLoader(rootDirectory);
-    }
+        public Basic(String configurationPath, JamesDirectoriesProvider directories) {
+            this.configurationPath = configurationPath;
+            this.directories = directories;
+        }
 
-    public String configurationPath() {
-        return configurationPath;
-    }
+        @Override
+        public String configurationPath() {
+            return configurationPath;
+        }
 
-    public JamesDirectoriesProvider directories() {
-        return directoriesProvider;
+        @Override
+        public JamesDirectoriesProvider directories() {
+            return directories;
+        }
     }
 
+    String configurationPath();
+
+    JamesDirectoriesProvider directories();
 }
diff --git a/server/container/core/src/test/java/org/apache/james/server/core/configuration/BasicConfigurationTest.java b/server/container/core/src/test/java/org/apache/james/server/core/configuration/BasicConfigurationTest.java
new file mode 100644
index 0000000..d1c1105
--- /dev/null
+++ b/server/container/core/src/test/java/org/apache/james/server/core/configuration/BasicConfigurationTest.java
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.server.core.configuration;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.server.core.MissingArgumentException;
+import org.assertj.core.api.SoftAssertions;
+import org.junit.jupiter.api.Test;
+
+class BasicConfigurationTest {
+    @Test
+    void buildShouldThrowWhenWorkingDirectoryMissing() {
+        assertThatThrownBy(() -> Configuration.builder().build())
+            .isInstanceOf(MissingArgumentException.class)
+            .hasMessage("Server needs a working.directory env entry");
+    }
+
+    @Test
+    void useWorkingDirectoryEnvPropertyShouldThrowWhenEnvVariableIsUnspecified() {
+        assertThatThrownBy(() ->
+            Configuration.builder()
+                .useWorkingDirectoryEnvProperty())
+            .isInstanceOf(MissingArgumentException.class)
+            .hasMessage("Server needs a working.directory env entry");
+    }
+
+    @Test
+    void buildShouldReturnConfigurationWithSuppliedValues() {
+        Configuration.Basic configuration = Configuration.builder()
+            .workingDirectory("/path")
+            .configurationPath("file://myconf/")
+            .build();
+
+        SoftAssertions.assertSoftly(softly -> {
+            softly.assertThat(configuration.directories().getRootDirectory()).isEqualTo("/path");
+            softly.assertThat(configuration.configurationPath()).isEqualTo("file://myconf/");
+        });
+    }
+
+    @Test
+    void buildShouldReturnConfigurationWithClassPathConfigurationPathWhenSpecified() {
+        Configuration.Basic configuration = Configuration.builder()
+            .workingDirectory("/path")
+            .configurationFromClasspath()
+            .build();
+
+        SoftAssertions.assertSoftly(softly -> {
+            softly.assertThat(configuration.directories().getRootDirectory()).isEqualTo("/path");
+            softly.assertThat(configuration.configurationPath()).isEqualTo("classpath:");
+        });
+    }
+
+    @Test
+    void configurationPathShouldDefaultToFileConf() {
+        Configuration.Basic configuration = Configuration.builder()
+            .workingDirectory("/path")
+            .build();
+
+        SoftAssertions.assertSoftly(softly -> {
+            softly.assertThat(configuration.directories().getRootDirectory()).isEqualTo("/path");
+            softly.assertThat(configuration.configurationPath()).isEqualTo("file://conf/");
+        });
+    }
+
+    @Test
+    void useWorkingDirectoryEnvPropertyShouldReadSystemProperty() {
+        try {
+            System.setProperty("working.directory", "/path");
+
+            Configuration.Basic configuration = Configuration.builder()
+                .useWorkingDirectoryEnvProperty()
+                .build();
+
+            assertThat(configuration.directories().getRootDirectory()).isEqualTo("/path");
+        } finally {
+            System.clearProperty("working.directory");
+        }
+    }
+
+    @Test
+    void getConfDirectoryShouldReturnConfFolderOfRootDir() {
+        try {
+            System.setProperty("working.directory", "/path");
+
+            Configuration.Basic configuration = Configuration.builder()
+                .useWorkingDirectoryEnvProperty()
+                .build();
+
+            assertThat(configuration.directories().getConfDirectory()).isEqualTo("/path/conf/");
+        } finally {
+            System.clearProperty("working.directory");
+        }
+    }
+}
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryEchoMethodTest.java b/server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryEchoMethodTest.java
index f7aa3c7..7f21ab1 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryEchoMethodTest.java
+++ b/server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemoryEchoMethodTest.java
@@ -30,7 +30,7 @@ import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class MemoryEchoMethodTest implements EchoMethodContract {
     @RegisterExtension
-    static JamesServerExtension testExtension = new JamesServerBuilder()
+    static JamesServerExtension testExtension = new JamesServerBuilder<>(JamesServerBuilder.defaultConfigurationProvider())
         .server(configuration -> GuiceJamesServer.forConfiguration(configuration)
             .combineWith(IN_MEMORY_SERVER_AGGREGATE_MODULE)
             .overrideWith(new TestJMAPServerModule()))


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