JAMES-1735 Set the default domain when starting if not given by the configuration
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fed833a8 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fed833a8 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fed833a8 Branch: refs/heads/master Commit: fed833a8fb07b21b6d21e4a8a6b4081c2880b71e Parents: b2d4150 Author: Antoine Duprat <[email protected]> Authored: Wed May 11 16:42:38 2016 +0200 Committer: Antoine Duprat <[email protected]> Committed: Tue May 17 14:47:11 2016 +0200 ---------------------------------------------------------------------- .../apache/james/CassandraJamesServerTest.java | 8 +- .../apache/james/AbstractJamesServerTest.java | 20 +++ .../org/apache/james/MemoryJamesServerTest.java | 5 +- server/data/data-library/pom.xml | 5 + .../domainlist/lib/AbstractDomainList.java | 40 +++++- .../AbstractDomainListPrivateMethodsTest.java | 122 +++++++++++++++++++ 6 files changed, 196 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/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 6407262..3870b42 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 @@ -39,6 +39,7 @@ public class CassandraJamesServerTest extends AbstractJamesServerTest { private TemporaryFolder temporaryFolder = new TemporaryFolder(); private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder); + private CassandraCluster cassandra; @Rule public RuleChain chain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch); @@ -59,11 +60,16 @@ public class CassandraJamesServerTest extends AbstractJamesServerTest { @Provides @Singleton Session provideSession(CassandraModule cassandraModule) { - CassandraCluster cassandra = CassandraCluster.create(cassandraModule); + cassandra = CassandraCluster.create(cassandraModule); return cassandra.getConf(); } }); } + + @Override + protected void clean() { + cassandra.clearAllTables(); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJamesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJamesServerTest.java b/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJamesServerTest.java index b179cf2..ad525e8 100644 --- a/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJamesServerTest.java +++ b/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJamesServerTest.java @@ -24,6 +24,7 @@ import static com.jayway.restassured.config.RestAssuredConfig.newConfig; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; @@ -60,9 +61,28 @@ public abstract class AbstractJamesServerTest { protected abstract GuiceJamesServer createJamesServer(); + protected abstract void clean(); + @After public void tearDown() throws Exception { server.stop(); + clean(); + } + + @Test + public void hostnameShouldBeUsedAsDefaultDomain() throws Exception { + String expectedDefaultDomain = InetAddress.getLocalHost().getHostName(); + + assertThat(server.serverProbe().getDefaultDomain()).isEqualTo(expectedDefaultDomain); + } + + @Test + public void hostnameShouldBeRetrievedWhenRestarting() throws Exception { + server.stop(); + server.start(); + String expectedDefaultDomain = InetAddress.getLocalHost().getHostName(); + + assertThat(server.serverProbe().getDefaultDomain()).isEqualTo(expectedDefaultDomain); } @Test http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/server/container/guice/memory-guice/src/test/java/org/apache/james/MemoryJamesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/memory-guice/src/test/java/org/apache/james/MemoryJamesServerTest.java b/server/container/guice/memory-guice/src/test/java/org/apache/james/MemoryJamesServerTest.java index 0dd9f95..4dc6fae 100644 --- a/server/container/guice/memory-guice/src/test/java/org/apache/james/MemoryJamesServerTest.java +++ b/server/container/guice/memory-guice/src/test/java/org/apache/james/MemoryJamesServerTest.java @@ -37,5 +37,8 @@ public class MemoryJamesServerTest extends AbstractJamesServerTest { .overrideWith(new TestFilesystemModule(temporaryFolder), new TestJMAPServerModule(GetMessageListMethod.DEFAULT_MAXIMUM_LIMIT)); } - + + @Override + protected void clean() { + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/server/data/data-library/pom.xml ---------------------------------------------------------------------- diff --git a/server/data/data-library/pom.xml b/server/data/data-library/pom.xml index 259282e..27ffeee 100644 --- a/server/data/data-library/pom.xml +++ b/server/data/data-library/pom.xml @@ -104,6 +104,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java ---------------------------------------------------------------------- diff --git a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java index 739a980..54311ff 100644 --- a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java +++ b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java @@ -35,12 +35,21 @@ import org.apache.james.domainlist.api.DomainListException; import org.apache.james.lifecycle.api.Configurable; import org.apache.james.lifecycle.api.LogEnabled; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; /** * All implementations of the DomainList interface should extends this abstract * class */ public abstract class AbstractDomainList implements DomainList, LogEnabled, Configurable { + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDomainList.class); + + protected static final String LOCALHOST = "localhost"; + private DNSService dns; private boolean autoDetect = true; private boolean autoDetectIP = true; @@ -62,12 +71,39 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf @Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { - defaultDomain = config.getString("defaultDomain", "localhost"); + configureDefaultDomain(config); setAutoDetect(config.getBoolean("autodetect", true)); setAutoDetectIP(config.getBoolean("autodetectIP", true)); } + @VisibleForTesting void configureDefaultDomain(HierarchicalConfiguration config) throws ConfigurationException { + + try { + defaultDomain = config.getString("defaultDomain", LOCALHOST); + + String hostName = InetAddress.getLocalHost().getHostName(); + if (mayChangeDefaultDomain()) { + setDefaultDomain(hostName); + } + } catch (UnknownHostException e) { + LOGGER.warn("Unable to retrieve hostname.", e); + } catch (DomainListException e) { + LOGGER.error("An error occured while creating the default domain", e); + } + } + + private boolean mayChangeDefaultDomain() { + return LOCALHOST.equals(defaultDomain); + } + + private void setDefaultDomain(String defaultDomain) throws DomainListException { + if (!containsDomain(defaultDomain)) { + addDomain(defaultDomain); + } + this.defaultDomain = defaultDomain; + } + @Override public String getDefaultDomain() throws DomainListException { if (defaultDomain!= null) { @@ -105,7 +141,7 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf } } } - return domains; + return ImmutableList.copyOf(domains); } /** http://git-wip-us.apache.org/repos/asf/james-project/blob/fed833a8/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java new file mode 100644 index 0000000..eb82d76 --- /dev/null +++ b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java @@ -0,0 +1,122 @@ +/**************************************************************** + * 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.domainlist.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.InetAddress; +import java.util.List; + +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.james.domainlist.api.DomainListException; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class AbstractDomainListPrivateMethodsTest { + + private MyDomainList domainList; + + @Before + public void setup() { + domainList = new MyDomainList(); + } + + private static class MyDomainList extends AbstractDomainList { + + private List<String> domains; + + public MyDomainList() { + domains = Lists.newArrayList(); + } + + @Override + public boolean containsDomain(String domain) throws DomainListException { + return domains.contains(domain); + } + + @Override + public void addDomain(String domain) throws DomainListException { + domains.add(domain); + } + + @Override + public void removeDomain(String domain) throws DomainListException { + domains.remove(domain); + } + + @Override + protected List<String> getDomainListInternal() throws DomainListException { + return domains; + } + } + + @Test + public void setDefaultDomainShouldSetFromConfigurationWhenDifferentFromLocalhost() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + String expectedDefaultDomain = "myDomain.org"; + when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST)) + .thenReturn(expectedDefaultDomain); + + domainList.configureDefaultDomain(configuration); + + assertThat(domainList.getDefaultDomain()).isEqualTo(expectedDefaultDomain); + } + + @Test + public void setDefaultDomainShouldSetFromHostnameWhenEqualsToLocalhost() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST)) + .thenReturn(AbstractDomainList.LOCALHOST); + + String expectedDefaultDomain = InetAddress.getLocalHost().getHostName(); + domainList.configureDefaultDomain(configuration); + + assertThat(domainList.getDefaultDomain()).isEqualTo(expectedDefaultDomain); + } + + @Test + public void setDefaultDomainShouldCreateFromHostnameWhenEqualsToLocalhost() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST)) + .thenReturn(AbstractDomainList.LOCALHOST); + + String expectedDefaultDomain = InetAddress.getLocalHost().getHostName(); + domainList.configureDefaultDomain(configuration); + + assertThat(domainList.getDomainListInternal()).contains(expectedDefaultDomain); + } + + @Test + public void setDefaultDomainShouldNotCreateTwiceWhenCallingTwoTimes() throws Exception { + HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class); + when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST)) + .thenReturn(AbstractDomainList.LOCALHOST); + + String expectedDefaultDomain = InetAddress.getLocalHost().getHostName(); + domainList.configureDefaultDomain(configuration); + domainList.configureDefaultDomain(configuration); + + assertThat(domainList.getDomainListInternal()).containsOnlyOnce(expectedDefaultDomain); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
