Skip to content

Commit

Permalink
JAMES-1587 Improve domain list tests
Browse files Browse the repository at this point in the history
	   Contributed by Benoit Tellier and Raphael Ouazana

git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1712027 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Nov 2, 2015
1 parent 122e215 commit ec5771d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 62 deletions.
4 changes: 4 additions & 0 deletions server/data/data-hbase/pom.xml
Expand Up @@ -167,6 +167,10 @@
<artifactId>hadoop-test</artifactId> <artifactId>hadoop-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
Expand Down
Expand Up @@ -20,11 +20,13 @@


import java.io.IOException; import java.io.IOException;
import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.api.DomainList;
import org.apache.james.domainlist.api.DomainListException;
import org.apache.james.domainlist.lib.AbstractDomainListTest; import org.apache.james.domainlist.lib.AbstractDomainListTest;
import org.apache.james.mailbox.hbase.HBaseClusterSingleton; import org.apache.james.mailbox.hbase.HBaseClusterSingleton;
import org.apache.james.system.hbase.TablePool; import org.apache.james.system.hbase.TablePool;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


/** /**
Expand Down Expand Up @@ -54,4 +56,11 @@ protected DomainList createDomainList() {
domainList.setAutoDetectIP(false); domainList.setAutoDetectIP(false);
return domainList; return domainList;
} }

@Ignore
@Test
@Override
public void removeDomainShouldThrowIfTheDomainIsAbsent() throws DomainListException {

}
} }
4 changes: 4 additions & 0 deletions server/data/data-jpa/pom.xml
Expand Up @@ -123,6 +123,10 @@
<scope>test</scope> <scope>test</scope>
<type>test-jar</type> <type>test-jar</type>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>commons-dbcp</groupId> <groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId> <artifactId>commons-dbcp</artifactId>
Expand Down
Expand Up @@ -18,125 +18,161 @@
****************************************************************/ ****************************************************************/
package org.apache.james.domainlist.lib; package org.apache.james.domainlist.lib;


import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
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.DomainList; import org.apache.james.domainlist.api.DomainList;
import org.apache.james.domainlist.api.DomainListException; import org.apache.james.domainlist.api.DomainListException;
import static org.hamcrest.CoreMatchers.nullValue; import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


/**
* Test the implementation of the DomainList.
*/
public abstract class AbstractDomainListTest { public abstract class AbstractDomainListTest {


// Domains we will play with.
private final String DOMAIN_1 = "domain1.tld"; private final String DOMAIN_1 = "domain1.tld";
private final String DOMAIN_2 = "domain2.tld"; private final String DOMAIN_2 = "domain2.tld";
private final String DOMAIN_3 = "domain3.tld"; private final String DOMAIN_3 = "domain3.tld";
private final String DOMAIN_4 = "domain4.tld"; private final String DOMAIN_4 = "domain4.tld";
private final String DOMAIN_5 = "domain5.tld"; private final String DOMAIN_5 = "domain5.tld";
/** private final String DOMAIN_UPPER_5 = "Domain5.tld";
* The JPA DomainList service.
*/
private DomainList domainList; private DomainList domainList;


@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
domainList = createDomainList(); domainList = createDomainList();
}

@After
public void tearDown() throws Exception {
deleteAll(); deleteAll();
} }


/**
* Add 3 domains and list them.
*
* @throws DomainListException
*/
@Test @Test
public void createListDomains() throws DomainListException { public void createListDomains() throws DomainListException {
domainList.addDomain(DOMAIN_3); domainList.addDomain(DOMAIN_3);
domainList.addDomain(DOMAIN_4); domainList.addDomain(DOMAIN_4);
domainList.addDomain(DOMAIN_5); domainList.addDomain(DOMAIN_5);
assertEquals(3, domainList.getDomains().length); assertThat(domainList.getDomains()).containsOnly(DOMAIN_3, DOMAIN_4, DOMAIN_5);
} }


/**
* Add a domain and check it is present.
*
* @throws DomainListException
*/
@Test @Test
public void testAddContainsDomain() throws DomainListException { public void domainsShouldBeListedInLowerCase() throws DomainListException {
domainList.addDomain(DOMAIN_UPPER_5);
assertThat(domainList.getDomains()).containsOnly(DOMAIN_5);
}

@Test
public void containShouldReturnTrueWhenThereIsADomain() throws DomainListException {
domainList.addDomain(DOMAIN_2); domainList.addDomain(DOMAIN_2);
domainList.containsDomain(DOMAIN_2); assertThat(domainList.containsDomain(DOMAIN_2)).isTrue();
}

@Test
public void containShouldBeCaseSensitive() throws DomainListException {
domainList.addDomain(DOMAIN_5);
assertThat(domainList.containsDomain(DOMAIN_UPPER_5)).isTrue();
}

@Test
public void listDomainsShouldReturnNullWhenThereIsNoDomains() throws DomainListException {
assertThat(domainList.getDomains()).isNull();
} }


/**
* Add and remove a domain, and check database is empty.
*
* @throws DomainListException
*/
@Test @Test
public void testAddRemoveContainsSameDomain() throws DomainListException { public void testAddRemoveContainsSameDomain() throws DomainListException {
domainList.addDomain(DOMAIN_1); domainList.addDomain(DOMAIN_1);
domainList.removeDomain(DOMAIN_1); domainList.removeDomain(DOMAIN_1);
assertThat(domainList.getDomains(), nullValue()); assertThat(domainList.getDomains()).isNull();
}

@Test(expected = DomainListException.class)
public void addShouldBeCaseSensitive() throws DomainListException {
try {
domainList.addDomain(DOMAIN_5);
} catch (Exception e) {
fail(e.getMessage());
}
domainList.addDomain(DOMAIN_UPPER_5);
} }


/**
* Add two same domains with different cases, and check we've got an exception.
*
* @throws DomainListException
*/
@Test @Test
public void testUpperCaseSameDomain() throws DomainListException { public void deletingADomainShouldNotDeleteOtherDomains() throws DomainListException {
domainList.addDomain(DOMAIN_1); domainList.addDomain(DOMAIN_1);
assertEquals(1, domainList.getDomains().length);
try { try {
String DOMAIN_1_UPPER_CASE = "Domain1.tld"; domainList.removeDomain(DOMAIN_2);
domainList.addDomain(DOMAIN_1_UPPER_CASE); } catch (DomainListException e) {
fail("We should not be able to insert same domains, even with different cases");
} catch (DomainListException domainListException) {
assertTrue(domainListException.getMessage().contains(DOMAIN_1));
} }
assertThat(domainList.getDomains()).containsOnly(DOMAIN_1);
}

@Test
public void containShouldReturnFalseWhenThereIsNoDomain() throws DomainListException {
assertThat(domainList.containsDomain(DOMAIN_1)).isFalse();
}

@Test
public void ContainsShouldReturnFalseWhenDomainIsRemoved() throws DomainListException {
domainList.addDomain(DOMAIN_1);
domainList.removeDomain(DOMAIN_1);
assertThat(domainList.containsDomain(DOMAIN_1)).isFalse();
} }


/**
* Add a domain and remove another domain, and check first domain is still
* present.
*
* @throws DomainListException
*/
@Test @Test
public void testAddRemoveContainsDifferentDomain() throws DomainListException { public void RemoveShouldRemoveDomainsUsingUpperCases() throws DomainListException {
domainList.addDomain(DOMAIN_UPPER_5);
domainList.removeDomain(DOMAIN_UPPER_5);
assertThat(domainList.containsDomain(DOMAIN_UPPER_5)).isFalse();
}

@Test
public void RemoveShouldRemoveDomainsUsingLowerCases() throws DomainListException {
domainList.addDomain(DOMAIN_UPPER_5);
domainList.removeDomain(DOMAIN_5);
assertThat(domainList.containsDomain(DOMAIN_UPPER_5)).isFalse();
}

@Test(expected = DomainListException.class)
public void addDomainShouldThrowIfWeAddTwoTimesTheSameDomain() throws DomainListException {
try {
domainList.addDomain(DOMAIN_1);
} catch (Exception e) {
fail(e.getMessage());
}
domainList.addDomain(DOMAIN_1); domainList.addDomain(DOMAIN_1);
domainList.removeDomain(DOMAIN_2); }
assertEquals(1, domainList.getDomains().length);
assertEquals(true, domainList.containsDomain(DOMAIN_1)); @Test(expected = DomainListException.class)
public void removeDomainShouldThrowIfTheDomainIsAbsent() throws DomainListException {
domainList.removeDomain(DOMAIN_1);
} }


/** /**
* Delete all possible domains from database. * Delete all possible domains from database.
*
* @throws DomainListException
*/ */
private void deleteAll() throws DomainListException { private void deleteAll() {
domainList.removeDomain(DOMAIN_1); deleteWithoutError(DOMAIN_1);
domainList.removeDomain(DOMAIN_2); deleteWithoutError(DOMAIN_2);
domainList.removeDomain(DOMAIN_3); deleteWithoutError(DOMAIN_3);
domainList.removeDomain(DOMAIN_4); deleteWithoutError(DOMAIN_4);
domainList.removeDomain(DOMAIN_5); deleteWithoutError(DOMAIN_5);
}

private void deleteWithoutError(String domain) {
try {
domainList.removeDomain(domain);
} catch(DomainListException e) {

}
} }


/** /**
* Return a fake DNSServer. * Return a fake DNSServer.
*
* @param hostName
* @return
*/ */
protected DNSService getDNSServer(final String hostName) { protected DNSService getDNSServer(final String hostName) {
return new MockDNSService() { return new MockDNSService() {
Expand Down

0 comments on commit ec5771d

Please sign in to comment.