This is an automated email from the ASF dual-hosted git repository. aduprat pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 74c9dca0b34879a9ad622d333b3abf2870b4f508 Author: Gautier DI FOLCO <gdifo...@linagora.com> AuthorDate: Fri Mar 8 15:14:05 2019 +0100 JAMES-2671 Create a generic SmtpTestRule --- .../smtp/CassandraRabbitMQSwiftSmtpTestRule.java | 123 +++------------------ .../james/mpt/smtp/RabbitMQForwardSmtpTest.java | 2 +- .../mpt/smtp/RabbitMQSmtpStarttlsCommandTest.java | 2 +- .../james/mpt/smtp/CassandraForwardSmtpTest.java | 2 +- .../mpt/smtp/CassandraSmtpStarttlsCommandTest.java | 2 +- .../mpt/smtp/CassandraSmtpTestRuleFactory.java | 63 +++++++++++ mpt/impl/smtp/core/pom.xml | 8 ++ .../org/apache/james/mpt/smtp/SmtpTestRule.java} | 56 +++------- 8 files changed, 104 insertions(+), 154 deletions(-) diff --git a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/CassandraRabbitMQSwiftSmtpTestRule.java b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/CassandraRabbitMQSwiftSmtpTestRule.java index 850ad68..44e840a 100644 --- a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/CassandraRabbitMQSwiftSmtpTestRule.java +++ b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/CassandraRabbitMQSwiftSmtpTestRule.java @@ -18,8 +18,6 @@ ****************************************************************/ package org.apache.james.mpt.smtp; -import java.util.Iterator; - import org.apache.commons.configuration.DefaultConfigurationBuilder; import org.apache.james.CassandraJamesServerMain; import org.apache.james.CleanupTasksPerformer; @@ -27,142 +25,51 @@ import org.apache.james.GuiceJamesServer; import org.apache.james.backend.rabbitmq.DockerRabbitMQSingleton; import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration; import org.apache.james.dnsservice.api.DNSService; -import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.modules.TestRabbitMQModule; import org.apache.james.modules.TestSwiftBlobStoreModule; import org.apache.james.modules.blobstore.BlobStoreChoosingModule; -import org.apache.james.modules.protocols.ProtocolHandlerModule; -import org.apache.james.modules.protocols.SMTPServerModule; -import org.apache.james.modules.protocols.SmtpGuiceProbe; import org.apache.james.modules.protocols.SmtpGuiceProbe.SmtpServerConnectedType; import org.apache.james.modules.rabbitmq.RabbitMQModule; import org.apache.james.modules.server.CamelMailetContainerModule; -import org.apache.james.mpt.api.Continuation; -import org.apache.james.mpt.api.Session; -import org.apache.james.mpt.monitor.SystemLoggingMonitor; -import org.apache.james.mpt.session.ExternalSessionFactory; import org.apache.james.queue.api.MailQueueItemDecoratorFactory; import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.util.Host; -import org.apache.james.util.Port; -import org.apache.james.utils.DataProbeImpl; import org.junit.rules.TemporaryFolder; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -import com.google.common.base.Preconditions; -import com.google.common.base.Splitter; -import com.google.inject.Module; -import com.google.inject.util.Modules; - -public class CassandraRabbitMQSwiftSmtpTestRule implements TestRule, SmtpHostSystem { - - private static final Module SMTP_PROTOCOL_MODULE = Modules.combine( - new ProtocolHandlerModule(), - new SMTPServerModule()); - - private final Host cassandraHost; - private final SmtpServerConnectedType smtpServerConnectedType; - - private TemporaryFolder folder; - private GuiceJamesServer jamesServer; - private InMemoryDNSService inMemoryDNSService; - private ExternalSessionFactory sessionFactory; - - public CassandraRabbitMQSwiftSmtpTestRule(SmtpServerConnectedType smtpServerConnectedType, Host cassandraHost) { - this.smtpServerConnectedType = smtpServerConnectedType; - this.cassandraHost = cassandraHost; - } - - @Override - public Statement apply(Statement base, Description description) { - return base; - } - - @Override - public boolean addUser(String userAtDomain, String password) throws Exception { - Preconditions.checkArgument(userAtDomain.contains("@"), "The 'user' should contain the 'domain'"); - Iterator<String> split = Splitter.on("@").split(userAtDomain).iterator(); - split.next(); - String domain = split.next(); - - createDomainIfNeeded(domain); - jamesServer.getProbe(DataProbeImpl.class).addUser(userAtDomain, password); - return true; - } - - @Override - public Session newSession(Continuation continuation) throws Exception { - return sessionFactory.newSession(continuation); - } - - private void createDomainIfNeeded(String domain) throws Exception { - if (!jamesServer.getProbe(DataProbeImpl.class).containsDomain(domain)) { - jamesServer.getProbe(DataProbeImpl.class).addDomain(domain); - } - } - - @Override - public void addAddressMapping(String user, String domain, String address) throws Exception { - jamesServer.getProbe(DataProbeImpl.class).addAddressMapping(user, domain, address); - } - - @Override - public void beforeTest() throws Exception { - inMemoryDNSService = new InMemoryDNSService(); - folder = new TemporaryFolder(); - folder.create(); - jamesServer = createJamesServer(); - jamesServer.start(); - - createSessionFactory(); - } - @Override - public void afterTest() { - jamesServer.stop(); - folder.delete(); - } +public final class CassandraRabbitMQSwiftSmtpTestRule { + public static SmtpTestRule create(SmtpServerConnectedType smtpServerConnectedType, Host cassandraHost) { + SmtpTestRule.ServerBuilder createJamesServer = (folder, dnsService) -> createJamesServer(cassandraHost, folder, dnsService); - @Override - public InMemoryDNSService getInMemoryDnsService() { - return inMemoryDNSService; + return new SmtpTestRule(smtpServerConnectedType, createJamesServer); } - private GuiceJamesServer createJamesServer() throws Exception { + private static GuiceJamesServer createJamesServer(Host cassandraHost, TemporaryFolder folder, DNSService dnsService) throws Exception { Configuration configuration = Configuration.builder() .workingDirectory(folder.newFolder()) .configurationFromClasspath() .build(); return GuiceJamesServer.forConfiguration(configuration) - .combineWith( - Modules - .override(Modules.combine(CassandraJamesServerMain.CASSANDRA_SERVER_CORE_MODULE)) - .with(new RabbitMQModule(), new BlobStoreChoosingModule()), - SMTP_PROTOCOL_MODULE, + .combineWith(CassandraJamesServerMain.CASSANDRA_SERVER_CORE_MODULE, + SmtpTestRule.SMTP_PROTOCOL_MODULE, binder -> binder.bind(MailQueueItemDecoratorFactory.class).to(RawMailQueueItemDecoratorFactory.class), binder -> binder.bind(CamelMailetContainerModule.DefaultProcessorsConfigurationSupplier.class) .toInstance(DefaultConfigurationBuilder::new)) - .overrideWith(new TestRabbitMQModule(DockerRabbitMQSingleton.SINGLETON)) - .overrideWith(new TestSwiftBlobStoreModule()) .overrideWith( + new RabbitMQModule(), + new BlobStoreChoosingModule()) + .overrideWith( + new TestRabbitMQModule(DockerRabbitMQSingleton.SINGLETON), + new TestSwiftBlobStoreModule(), binder -> binder.bind(ClusterConfiguration.class).toInstance( ClusterConfiguration.builder() .host(cassandraHost) .keyspace("testing") .replicationFactor(1) .build()), - binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)) - .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())); - } - - private void createSessionFactory() { - SmtpGuiceProbe smtpProbe = jamesServer.getProbe(SmtpGuiceProbe.class); - Port smtpPort = smtpServerConnectedType.getPortExtractor().apply(smtpProbe); - - sessionFactory = new ExternalSessionFactory("localhost", smtpPort, new SystemLoggingMonitor(), "220 mydomain.tld smtp"); + binder -> binder.bind(DNSService.class).toInstance(dnsService), + binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton()); } } + diff --git a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQForwardSmtpTest.java b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQForwardSmtpTest.java index 17eb90e..74229a6 100644 --- a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQForwardSmtpTest.java +++ b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQForwardSmtpTest.java @@ -32,7 +32,7 @@ public class RabbitMQForwardSmtpTest extends ForwardSmtpTest { @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); @Rule - public CassandraRabbitMQSwiftSmtpTestRule cassandraRabbitMQSwiftSmtpTestRule = new CassandraRabbitMQSwiftSmtpTestRule(SMTP_GLOBAL_SERVER, cassandraServer.getHost()); + public SmtpTestRule cassandraRabbitMQSwiftSmtpTestRule = CassandraRabbitMQSwiftSmtpTestRule.create(SMTP_GLOBAL_SERVER, cassandraServer.getHost()); @Before @Override diff --git a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQSmtpStarttlsCommandTest.java b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQSmtpStarttlsCommandTest.java index 34329e1..6ce89c3 100644 --- a/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQSmtpStarttlsCommandTest.java +++ b/mpt/impl/smtp/cassandra-rabbitmq-object-storage/src/test/java/org/apache/james/mpt/smtp/RabbitMQSmtpStarttlsCommandTest.java @@ -32,7 +32,7 @@ public class RabbitMQSmtpStarttlsCommandTest extends SmtpStarttlsCommandTest { @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); @Rule - public CassandraRabbitMQSwiftSmtpTestRule cassandraRabbitMQSwiftSmtpTestRule = new CassandraRabbitMQSwiftSmtpTestRule(SMTP_START_TLS_SERVER, cassandraServer.getHost()); + public SmtpTestRule cassandraRabbitMQSwiftSmtpTestRule = CassandraRabbitMQSwiftSmtpTestRule.create(SMTP_START_TLS_SERVER, cassandraServer.getHost()); @Before @Override diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java index 3a7b330..96ea702 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java @@ -32,7 +32,7 @@ public class CassandraForwardSmtpTest extends ForwardSmtpTest { @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); @Rule - public CassandraSmtpTestRule cassandraSmtpTestRule = new CassandraSmtpTestRule(SMTP_GLOBAL_SERVER, cassandraServer.getHost()); + public SmtpTestRule cassandraSmtpTestRule = CassandraSmtpTestRuleFactory.create(SMTP_GLOBAL_SERVER, cassandraServer.getHost()); @Before @Override diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java index 10eca13..c609133 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java @@ -32,7 +32,7 @@ public class CassandraSmtpStarttlsCommandTest extends SmtpStarttlsCommandTest { @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); @Rule - public CassandraSmtpTestRule cassandraSmtpTestRule = new CassandraSmtpTestRule(SMTP_START_TLS_SERVER, cassandraServer.getHost()); + public SmtpTestRule cassandraSmtpTestRule = CassandraSmtpTestRuleFactory.create(SMTP_START_TLS_SERVER, cassandraServer.getHost()); @Before @Override diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRuleFactory.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRuleFactory.java new file mode 100644 index 0000000..4b62952 --- /dev/null +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRuleFactory.java @@ -0,0 +1,63 @@ +/**************************************************************** + * 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.mpt.smtp; + +import org.apache.commons.configuration.DefaultConfigurationBuilder; +import org.apache.james.CassandraJamesServerMain; +import org.apache.james.GuiceJamesServer; +import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration; +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.modules.protocols.SmtpGuiceProbe.SmtpServerConnectedType; +import org.apache.james.modules.server.CamelMailetContainerModule; +import org.apache.james.queue.api.MailQueueItemDecoratorFactory; +import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory; +import org.apache.james.server.core.configuration.Configuration; +import org.apache.james.util.Host; +import org.junit.rules.TemporaryFolder; + +public final class CassandraSmtpTestRuleFactory { + public static SmtpTestRule create(SmtpServerConnectedType smtpServerConnectedType, Host cassandraHost) { + SmtpTestRule.ServerBuilder createJamesServer = (folder, dnsService) -> createJamesServer(cassandraHost, folder, dnsService); + + return new SmtpTestRule(smtpServerConnectedType, createJamesServer); + } + + private static GuiceJamesServer createJamesServer(Host cassandraHost, TemporaryFolder folder, DNSService dnsService) throws Exception { + Configuration configuration = Configuration.builder() + .workingDirectory(folder.newFolder()) + .configurationFromClasspath() + .build(); + + return GuiceJamesServer.forConfiguration(configuration) + .combineWith(CassandraJamesServerMain.CASSANDRA_SERVER_CORE_MODULE, + SmtpTestRule.SMTP_PROTOCOL_MODULE, + binder -> binder.bind(MailQueueItemDecoratorFactory.class).to(RawMailQueueItemDecoratorFactory.class), + binder -> binder.bind(CamelMailetContainerModule.DefaultProcessorsConfigurationSupplier.class) + .toInstance(DefaultConfigurationBuilder::new)) + .overrideWith( + binder -> binder.bind(ClusterConfiguration.class).toInstance( + ClusterConfiguration.builder() + .host(cassandraHost) + .keyspace("testing") + .replicationFactor(1) + .build()), + binder -> binder.bind(DNSService.class).toInstance(dnsService)); + } +} + diff --git a/mpt/impl/smtp/core/pom.xml b/mpt/impl/smtp/core/pom.xml index 4cb8f19..dd6c082 100644 --- a/mpt/impl/smtp/core/pom.xml +++ b/mpt/impl/smtp/core/pom.xml @@ -49,6 +49,14 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>james-server-guice-common</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>james-server-guice-smtp</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-testing</artifactId> </dependency> <dependency> diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRule.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpTestRule.java similarity index 67% rename from mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRule.java rename to mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpTestRule.java index 25119e7..551d86b 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpTestRule.java +++ b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/SmtpTestRule.java @@ -19,26 +19,17 @@ package org.apache.james.mpt.smtp; import java.util.Iterator; +import java.util.Optional; -import org.apache.commons.configuration.DefaultConfigurationBuilder; -import org.apache.james.CassandraJamesServerMain; import org.apache.james.GuiceJamesServer; -import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration; -import org.apache.james.dnsservice.api.DNSService; import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.modules.protocols.ProtocolHandlerModule; import org.apache.james.modules.protocols.SMTPServerModule; import org.apache.james.modules.protocols.SmtpGuiceProbe; -import org.apache.james.modules.protocols.SmtpGuiceProbe.SmtpServerConnectedType; -import org.apache.james.modules.server.CamelMailetContainerModule; import org.apache.james.mpt.api.Continuation; import org.apache.james.mpt.api.Session; import org.apache.james.mpt.monitor.SystemLoggingMonitor; import org.apache.james.mpt.session.ExternalSessionFactory; -import org.apache.james.queue.api.MailQueueItemDecoratorFactory; -import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory; -import org.apache.james.server.core.configuration.Configuration; -import org.apache.james.util.Host; import org.apache.james.util.Port; import org.apache.james.utils.DataProbeImpl; import org.junit.rules.TemporaryFolder; @@ -51,23 +42,27 @@ import com.google.common.base.Splitter; import com.google.inject.Module; import com.google.inject.util.Modules; -public class CassandraSmtpTestRule implements TestRule, SmtpHostSystem { - private static final Module SMTP_PROTOCOL_MODULE = Modules.combine( +public class SmtpTestRule implements TestRule, SmtpHostSystem { + @FunctionalInterface + public interface ServerBuilder { + GuiceJamesServer build(TemporaryFolder folder, DNSService dnsService) throws Exception; + } + + public static final Module SMTP_PROTOCOL_MODULE = Modules.combine( new ProtocolHandlerModule(), new SMTPServerModule()); - private final Host cassandraHost; - private final SmtpServerConnectedType smtpServerConnectedType; - + private final SmtpGuiceProbe.SmtpServerConnectedType smtpServerConnectedType; + private final ServerBuilder createJamesServer; private TemporaryFolder folder; - private GuiceJamesServer jamesServer; private InMemoryDNSService inMemoryDNSService; + private GuiceJamesServer jamesServer; private ExternalSessionFactory sessionFactory; - public CassandraSmtpTestRule(SmtpServerConnectedType smtpServerConnectedType, Host cassandraHost) { + public SmtpTestRule(SmtpGuiceProbe.SmtpServerConnectedType smtpServerConnectedType, ServerBuilder createJamesServer) { this.smtpServerConnectedType = smtpServerConnectedType; - this.cassandraHost = cassandraHost; + this.createJamesServer = createJamesServer; } @Override @@ -108,7 +103,7 @@ public class CassandraSmtpTestRule implements TestRule, SmtpHostSystem { inMemoryDNSService = new InMemoryDNSService(); folder = new TemporaryFolder(); folder.create(); - jamesServer = createJamesServer(); + jamesServer = createJamesServer.build(folder, inMemoryDNSService); jamesServer.start(); createSessionFactory(); @@ -125,29 +120,6 @@ public class CassandraSmtpTestRule implements TestRule, SmtpHostSystem { return inMemoryDNSService; } - private GuiceJamesServer createJamesServer() throws Exception { - Configuration configuration = Configuration.builder() - .workingDirectory(folder.newFolder()) - .configurationFromClasspath() - .build(); - - return GuiceJamesServer.forConfiguration(configuration) - .combineWith( - CassandraJamesServerMain.CASSANDRA_SERVER_CORE_MODULE, - SMTP_PROTOCOL_MODULE, - binder -> binder.bind(MailQueueItemDecoratorFactory.class).to(RawMailQueueItemDecoratorFactory.class), - binder -> binder.bind(CamelMailetContainerModule.DefaultProcessorsConfigurationSupplier.class) - .toInstance(DefaultConfigurationBuilder::new)) - .overrideWith( - binder -> binder.bind(ClusterConfiguration.class).toInstance( - ClusterConfiguration.builder() - .host(cassandraHost) - .keyspace("testing") - .replicationFactor(1) - .build()), - binder -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)); - } - private void createSessionFactory() { SmtpGuiceProbe smtpProbe = jamesServer.getProbe(SmtpGuiceProbe.class); Port smtpPort = smtpServerConnectedType.getPortExtractor().apply(smtpProbe); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org