Skip to content

Commit

Permalink
JAMES-2513 Improve XMLDomainListTest
Browse files Browse the repository at this point in the history
Use DomainListConfiguration

We also sole an issue in 'configureShouldNotFailWhenConfiguringDefaultDomain'.
The assertion concerned the domain number count, affected by no more auto-detecting localhost default domain. Hence I reworked it.
  • Loading branch information
chibenwa committed Aug 3, 2018
1 parent 5bb6dde commit 7063734
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import javax.inject.Singleton; import javax.inject.Singleton;


import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.core.Domain; import org.apache.james.core.Domain;
import org.apache.james.dnsservice.api.DNSService; import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.domainlist.api.DomainListException; import org.apache.james.domainlist.api.DomainListException;
import org.apache.james.domainlist.lib.AbstractDomainList; import org.apache.james.domainlist.lib.AbstractDomainList;
import org.apache.james.domainlist.lib.DomainListConfiguration;
import org.apache.james.lifecycle.api.Configurable; import org.apache.james.lifecycle.api.Configurable;


/** /**
Expand All @@ -48,8 +48,8 @@ public XMLDomainList(DNSService dns) {
} }


@Override @Override
public void configure(HierarchicalConfiguration config) throws ConfigurationException { public void configure(DomainListConfiguration domainListConfiguration) throws ConfigurationException {
super.configure(config); super.configure(domainListConfiguration);
isConfigured = true; isConfigured = true;
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@


import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;


import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.core.Domain; import org.apache.james.core.Domain;
import org.apache.james.dnsservice.api.DNSService; import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.dnsservice.api.mock.MockDNSService; import org.apache.james.dnsservice.api.mock.MockDNSService;
import org.apache.james.domainlist.api.DomainListException; import org.apache.james.domainlist.api.DomainListException;
import org.apache.james.domainlist.lib.DomainListConfiguration;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
Expand All @@ -41,21 +38,10 @@
public class XMLDomainListTest { public class XMLDomainListTest {


public static final Domain DEFAULT_DOMAIN = Domain.of("default.domain"); public static final Domain DEFAULT_DOMAIN = Domain.of("default.domain");
public static final Domain DOMAIN_1 = Domain.of("domain1");


@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();

private HierarchicalConfiguration setUpConfiguration(boolean auto, boolean autoIP, List<String> names) {
DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder();

configuration.addProperty("autodetect", auto);
configuration.addProperty("autodetectIP", autoIP);
for (String name : names) {
configuration.addProperty("domainnames.domainname", name);
}
configuration.addProperty("defaultDomain", DEFAULT_DOMAIN.asString());
return configuration;
}


private DNSService setUpDNSServer(final String hostName) { private DNSService setUpDNSServer(final String hostName) {
return new MockDNSService() { return new MockDNSService() {
Expand All @@ -80,43 +66,49 @@ public InetAddress getLocalHost() throws UnknownHostException {
// See https://issues.apache.org/jira/browse/JAMES-998 // See https://issues.apache.org/jira/browse/JAMES-998
@Test @Test
public void testNoConfiguredDomains() throws Exception { public void testNoConfiguredDomains() throws Exception {
List<String> domains = new ArrayList<>();
XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost")); XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost"));
dom.configure(setUpConfiguration(false, false, domains));
dom.configure(DomainListConfiguration.builder()
.autoDetect(false)
.autoDetectIp(false)
.defaultDomain(DEFAULT_DOMAIN));



assertThat(dom.getDomains()).containsOnly(DEFAULT_DOMAIN); assertThat(dom.getDomains()).containsOnly(DEFAULT_DOMAIN);
} }


@Test @Test
public void testGetDomains() throws Exception { public void testGetDomains() throws Exception {
List<String> domains = new ArrayList<>();
domains.add("domain1.");
domains.add("domain2.");

XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost")); XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost"));
dom.configure(setUpConfiguration(false, false, domains)); dom.configure(DomainListConfiguration.builder()
.autoDetect(false)
.autoDetectIp(false)
.addConfiguredDomains(Domain.of("domain1."), Domain.of("domain2."))
.defaultDomain(DEFAULT_DOMAIN));


assertThat(dom.getDomains()).hasSize(3); assertThat(dom.getDomains()).hasSize(3);
} }


@Test @Test
public void testGetDomainsAutoDetectNotLocalHost() throws Exception { public void testGetDomainsAutoDetectNotLocalHost() throws Exception {
List<String> domains = new ArrayList<>();
domains.add("domain1.");

XMLDomainList dom = new XMLDomainList(setUpDNSServer("local")); XMLDomainList dom = new XMLDomainList(setUpDNSServer("local"));
dom.configure(setUpConfiguration(true, false, domains)); dom.configure(DomainListConfiguration.builder()
.autoDetect(true)
.autoDetectIp(false)
.addConfiguredDomains(Domain.of("domain1."))
.defaultDomain(DEFAULT_DOMAIN));


assertThat(dom.getDomains()).hasSize(3); assertThat(dom.getDomains()).hasSize(3);
} }


@Test @Test
public void testGetDomainsAutoDetectLocalHost() throws Exception { public void testGetDomainsAutoDetectLocalHost() throws Exception {
List<String> domains = new ArrayList<>();
domains.add("domain1.");

XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost")); XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost"));
dom.configure(setUpConfiguration(true, false, domains)); dom.configure(DomainListConfiguration.builder()
.autoDetect(false)
.autoDetectIp(false)
.addConfiguredDomains(Domain.of("domain1."))
.defaultDomain(DEFAULT_DOMAIN));


assertThat(dom.getDomains()).hasSize(2); assertThat(dom.getDomains()).hasSize(2);
} }
Expand All @@ -125,11 +117,12 @@ public void testGetDomainsAutoDetectLocalHost() throws Exception {
public void addDomainShouldFailWhenAlreadyConfigured() throws Exception { public void addDomainShouldFailWhenAlreadyConfigured() throws Exception {
expectedException.expect(DomainListException.class); expectedException.expect(DomainListException.class);


List<String> domains = new ArrayList<>();
domains.add("domain1");

XMLDomainList testee = new XMLDomainList(setUpDNSServer("hostname")); XMLDomainList testee = new XMLDomainList(setUpDNSServer("hostname"));
testee.configure(setUpConfiguration(true, false, domains)); testee.configure(DomainListConfiguration.builder()
.autoDetect(true)
.autoDetectIp(false)
.addConfiguredDomain(DOMAIN_1)
.defaultDomain(DEFAULT_DOMAIN));


testee.addDomain(Domain.of("newDomain")); testee.addDomain(Domain.of("newDomain"));
} }
Expand All @@ -138,27 +131,25 @@ public void addDomainShouldFailWhenAlreadyConfigured() throws Exception {
public void removeDomainShouldFailWhenAlreadyConfigured() throws Exception { public void removeDomainShouldFailWhenAlreadyConfigured() throws Exception {
expectedException.expect(DomainListException.class); expectedException.expect(DomainListException.class);


List<String> domains = new ArrayList<>();
domains.add("domain1");

XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost")); XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost"));
testee.configure(setUpConfiguration(true, false, domains)); testee.configure(DomainListConfiguration.builder()
.autoDetect(true)
.autoDetectIp(false)
.addConfiguredDomain(DOMAIN_1));


testee.removeDomain(Domain.of("newDomain")); testee.removeDomain(Domain.of("newDomain"));
} }


@Test @Test
public void configureShouldNotFailWhenConfiguringDefaultDomain() throws Exception { public void configureShouldNotFailWhenConfiguringDefaultDomain() throws Exception {
DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder();

configuration.addProperty("autodetect", false);
configuration.addProperty("autodetectIP", false);
configuration.addProperty("domainnames.domainname", "domain1");
configuration.addProperty("defaultDomain", "localhost");

XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost")); XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost"));
testee.configure(configuration); testee.configure(DomainListConfiguration.builder()

.autoDetect(false)
assertThat(testee.getDomainListInternal()).hasSize(3); .autoDetectIp(false)
.defaultDomain(Domain.LOCALHOST)
.addConfiguredDomain(DOMAIN_1));

assertThat(testee.getDomainListInternal())
.containsOnly(DOMAIN_1, Domain.LOCALHOST);
} }
} }

0 comments on commit 7063734

Please sign in to comment.