james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/2] james-project git commit: JAMES-2553 Solve circular initialization problem
Date Thu, 04 Oct 2018 07:11:52 GMT
JAMES-2553 Solve circular initialization problem

To build JamesServerExtension we need to specify extensions that needs a
temporary folder to be generated, only available once JamesServerExtension::beforeEach
is called.

To break this dependencies, each extension is responsible of its own 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/9a5e1e51
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9a5e1e51
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9a5e1e51

Branch: refs/heads/master
Commit: 9a5e1e51ea953a939bb97907ec20de5903b2fc42
Parents: 98c2921
Author: Benoit Tellier <btellier@linagora.com>
Authored: Tue Oct 2 16:10:32 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Thu Oct 4 14:11:06 2018 +0700

----------------------------------------------------------------------
 .../apache/james/CassandraJamesServerTest.java  |  4 +--
 .../org/apache/james/CassandraWithTikaTest.java |  6 ++--
 .../james/EmbeddedElasticSearchExtension.java   | 17 +++++----
 .../org/apache/james/JamesServerExtension.java  | 31 ++++++++++------
 .../james/JamesServerExtensionBuilder.java      | 38 ++++++--------------
 .../TemporaryFolderRegistrableExtension.java    |  3 +-
 .../james/junit/TemporaryFolderExtension.java   |  4 +++
 7 files changed, 51 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java
b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java
index 886353f..8db9fe1 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java
@@ -31,8 +31,8 @@ class CassandraJamesServerTest implements JamesServerContract {
 
     @RegisterExtension
     static JamesServerExtension testExtension = new JamesServerExtensionBuilder()
-        .extension(EmbeddedElasticSearchExtension::new)
-        .extension(CassandraExtension::new)
+        .extension(new EmbeddedElasticSearchExtension())
+        .extension(new CassandraExtension())
         .server(configuration -> GuiceJamesServer.forConfiguration(configuration)
             .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE)
             .overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class))

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java
b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java
index 9f30523..d82e6ee 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java
@@ -30,9 +30,9 @@ class CassandraWithTikaTest implements JamesServerContract {
     @RegisterExtension
     static JamesServerExtension testExtension =
         new JamesServerExtensionBuilder()
-            .extension(CassandraExtension::new)
-            .extension(TikaExtension::new)
-            .extension(EmbeddedElasticSearchExtension::new)
+            .extension(new CassandraExtension())
+            .extension(new  TikaExtension())
+            .extension(new EmbeddedElasticSearchExtension())
             .server(configuration -> GuiceJamesServer.forConfiguration(configuration)
                 .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE)
                 .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java
b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java
index b3c61cf..743dbc3 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java
@@ -19,29 +19,32 @@
 
 package org.apache.james;
 
-import java.io.File;
-
 import org.apache.james.backends.es.EmbeddedElasticSearch;
+import org.apache.james.junit.TemporaryFolderExtension;
 import org.apache.james.modules.TestElasticSearchModule;
 import org.junit.jupiter.api.extension.ExtensionContext;
 
 import com.google.inject.Module;
 
 public class EmbeddedElasticSearchExtension implements GuiceModuleTestExtension {
-    private final EmbeddedElasticSearch embeddedElasticSearch;
+    private final TemporaryFolderExtension folderExtension;
+    private EmbeddedElasticSearch embeddedElasticSearch;
 
-    public EmbeddedElasticSearchExtension(File temporaryFolder) {
-        this.embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder.toPath());
+    public EmbeddedElasticSearchExtension() {
+        this.folderExtension = new TemporaryFolderExtension();
     }
 
     @Override
-    public void beforeEach(ExtensionContext extensionContext) {
+    public void beforeEach(ExtensionContext extensionContext) throws Exception {
+        folderExtension.beforeEach(extensionContext);
+        embeddedElasticSearch = new EmbeddedElasticSearch(folderExtension.getTemporaryFolder().getTempDir().toPath());
         embeddedElasticSearch.before();
     }
 
     @Override
-    public void afterEach(ExtensionContext extensionContext) {
+    public void afterEach(ExtensionContext extensionContext) throws Exception {
         embeddedElasticSearch.after();
+        folderExtension.afterEach(extensionContext);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtension.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtension.java
b/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtension.java
index 5551fcc..6ba6b0a 100644
--- a/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtension.java
+++ b/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtension.java
@@ -19,10 +19,10 @@
 
 package org.apache.james;
 
-import java.util.List;
-import java.util.function.Supplier;
+import java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
 
-import org.apache.james.util.Runnables;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
@@ -32,22 +32,21 @@ import org.junit.jupiter.api.extension.ParameterContext;
 import org.junit.jupiter.api.extension.ParameterResolutionException;
 import org.junit.jupiter.api.extension.ParameterResolver;
 
-import com.github.fge.lambdas.Throwing;
-import com.google.common.collect.Lists;
-
 public class JamesServerExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback,
AfterAllCallback, ParameterResolver {
 
-    interface ThrowingSupplier<T> {
-        T get() throws Exception;
+    interface ThrowingFunction<P, T> {
+        T apply(P parameter) throws Exception;
     }
 
-    private final ThrowingSupplier<GuiceJamesServer> serverSupplier;
+    private final TemporaryFolderRegistrableExtension folderRegistrableExtension;
+    private final ThrowingFunction<File, GuiceJamesServer> serverSupplier;
     private final RegistrableExtension registrableExtension;
     private GuiceJamesServer guiceJamesServer;
 
-    JamesServerExtension(RegistrableExtension registrableExtension, ThrowingSupplier<GuiceJamesServer>
serverSupplier) {
+    JamesServerExtension(RegistrableExtension registrableExtension, ThrowingFunction<File,
GuiceJamesServer> serverSupplier) {
         this.registrableExtension = registrableExtension;
         this.serverSupplier = serverSupplier;
+        this.folderRegistrableExtension = new TemporaryFolderRegistrableExtension();
     }
 
     @Override
@@ -57,8 +56,9 @@ public class JamesServerExtension implements BeforeAllCallback, BeforeEachCallba
 
     @Override
     public void beforeEach(ExtensionContext extensionContext) throws Exception {
+        folderRegistrableExtension.beforeEach(extensionContext);
         registrableExtension.beforeEach(extensionContext);
-        guiceJamesServer = serverSupplier.get();
+        guiceJamesServer = serverSupplier.apply(createTmpDir());
         guiceJamesServer.start();
     }
 
@@ -66,6 +66,7 @@ public class JamesServerExtension implements BeforeAllCallback, BeforeEachCallba
     public void afterEach(ExtensionContext extensionContext) throws Exception {
         guiceJamesServer.stop();
         registrableExtension.afterEach(extensionContext);
+        folderRegistrableExtension.afterEach(extensionContext);
     }
 
     @Override
@@ -82,4 +83,12 @@ public class JamesServerExtension implements BeforeAllCallback, BeforeEachCallba
     public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
         return guiceJamesServer;
     }
+
+    private File createTmpDir() {
+        try {
+            return folderRegistrableExtension.getTemporaryFolder().newFolder();
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtensionBuilder.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtensionBuilder.java
b/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtensionBuilder.java
index 7d305b3..1831f72 100644
--- a/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtensionBuilder.java
+++ b/server/container/guice/guice-common/src/test/java/org/apache/james/JamesServerExtensionBuilder.java
@@ -20,10 +20,8 @@
 package org.apache.james;
 
 import java.io.File;
-import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.Optional;
-import java.util.function.Supplier;
 
 import org.apache.james.server.core.configuration.Configuration;
 
@@ -35,11 +33,6 @@ import com.google.inject.Module;
 public class JamesServerExtensionBuilder {
 
     @FunctionalInterface
-    interface ExtensionProvider {
-        GuiceModuleTestExtension buildExtension(File tempDirectory);
-    }
-
-    @FunctionalInterface
     interface ConfigurationProvider {
         Configuration buildConfiguration(File tempDirectory);
     }
@@ -65,14 +58,8 @@ public class JamesServerExtensionBuilder {
         return this;
     }
 
-    public JamesServerExtensionBuilder extension(Supplier<GuiceModuleTestExtension>
extension) {
-        this.extensions.add(extension.get());
-        return this;
-    }
-
-    public JamesServerExtensionBuilder extension(ExtensionProvider extension) {
-        this.extensions.add(extension.buildExtension(createTmpDir()));
-        return this;
+    public JamesServerExtensionBuilder extension(GuiceModuleTestExtension extension) {
+        return this.extensions(extension);
     }
 
     public JamesServerExtensionBuilder configuration(ConfigurationProvider configuration)
throws UncheckedIOException {
@@ -88,7 +75,8 @@ public class JamesServerExtensionBuilder {
     public JamesServerExtension build() {
         Preconditions.checkNotNull(server);
         ConfigurationProvider configuration = this.configuration.orElse(defaultConfigurationProvider());
-        return new JamesServerExtension(buildAggregateJunitExtension(), () -> overrideServerWithExtensionsModules(configuration));
+        return new JamesServerExtension(buildAggregateJunitExtension(),
+            file -> overrideServerWithExtensionsModules(file, configuration));
     }
 
     private ConfigurationProvider defaultConfigurationProvider() {
@@ -99,14 +87,6 @@ public class JamesServerExtensionBuilder {
                 .build();
     }
 
-    private File createTmpDir() {
-        try {
-            return folderRegistrableExtension.getTemporaryFolder().newFolder();
-        } catch (IOException e) {
-            throw new UncheckedIOException(e);
-        }
-    }
-
     private AggregateJunitExtension buildAggregateJunitExtension() {
         ImmutableList<GuiceModuleTestExtension> extensions = this.extensions.build();
         return new AggregateJunitExtension(
@@ -116,9 +96,13 @@ public class JamesServerExtensionBuilder {
                 .build());
     }
 
-    private GuiceJamesServer overrideServerWithExtensionsModules(ConfigurationProvider configurationProvider)
{
-        ImmutableList<Module> modules = extensions.build().stream().map(x -> x.getModule()).collect(Guavate.toImmutableList());
-        return server.buildServer(configurationProvider.buildConfiguration(createTmpDir())).overrideWith(modules);
+    private GuiceJamesServer overrideServerWithExtensionsModules(File file, ConfigurationProvider
configurationProvider) {
+        ImmutableList<Module> modules = extensions.build()
+            .stream()
+            .map(GuiceModuleTestExtension::getModule)
+            .collect(Guavate.toImmutableList());
+
+        return server.buildServer(configurationProvider.buildConfiguration(file)).overrideWith(modules);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/container/guice/guice-common/src/test/java/org/apache/james/TemporaryFolderRegistrableExtension.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/TemporaryFolderRegistrableExtension.java
b/server/container/guice/guice-common/src/test/java/org/apache/james/TemporaryFolderRegistrableExtension.java
index 5bc52f2..4a1570f 100644
--- a/server/container/guice/guice-common/src/test/java/org/apache/james/TemporaryFolderRegistrableExtension.java
+++ b/server/container/guice/guice-common/src/test/java/org/apache/james/TemporaryFolderRegistrableExtension.java
@@ -24,11 +24,10 @@ import org.junit.rules.TemporaryFolder;
 
 public class TemporaryFolderRegistrableExtension implements RegistrableExtension {
 
-    private TemporaryFolder temporaryFolder;
+    private TemporaryFolder temporaryFolder = new TemporaryFolder();
 
     @Override
     public void beforeEach(ExtensionContext extensionContext) throws Exception {
-        temporaryFolder = new TemporaryFolder();
         temporaryFolder.create();
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/9a5e1e51/server/testing/src/main/java/org/apache/james/junit/TemporaryFolderExtension.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/junit/TemporaryFolderExtension.java
b/server/testing/src/main/java/org/apache/james/junit/TemporaryFolderExtension.java
index b5ed368..bddd2da 100644
--- a/server/testing/src/main/java/org/apache/james/junit/TemporaryFolderExtension.java
+++ b/server/testing/src/main/java/org/apache/james/junit/TemporaryFolderExtension.java
@@ -55,6 +55,10 @@ public class TemporaryFolderExtension implements ParameterResolver, BeforeEachCa
         FileUtils.deleteDirectory(temporaryFolder.getTempDir());
     }
 
+    public TemporaryFolder getTemporaryFolder() {
+        return temporaryFolder;
+    }
+
     public static class TemporaryFolder {
         private final File tempDir;
         private final String folderPath;


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


Mime
View raw message