Skip to content

Commit

Permalink
MAILET-129 Simplify AbstractRemoteAddrInNetworkTest before removing it
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Ouazana committed Sep 8, 2016
1 parent f88464d commit 92642bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 143 deletions.
Expand Up @@ -66,6 +66,7 @@ public static class Builder {
private Optional<MimeMessage> mimeMessage = Optional.absent(); private Optional<MimeMessage> mimeMessage = Optional.absent();
private List<MailAddress> recipients = new ArrayList<MailAddress>(); private List<MailAddress> recipients = new ArrayList<MailAddress>();
private MailAddress sender; private MailAddress sender;
private String remoteAddr;


public Builder fileName(String fileName) { public Builder fileName(String fileName) {
this.fileName = Optional.of(fileName); this.fileName = Optional.of(fileName);
Expand Down Expand Up @@ -97,6 +98,11 @@ public Builder sender(MailAddress sender) {
return this; return this;
} }


public Builder remoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr;
return this;
}

public FakeMail build() throws MessagingException { public FakeMail build() throws MessagingException {
Preconditions.checkState(!(fileName.isPresent() && mimeMessage.isPresent()), "You can not specify a MimeMessage object when you alredy set Content from a file"); Preconditions.checkState(!(fileName.isPresent() && mimeMessage.isPresent()), "You can not specify a MimeMessage object when you alredy set Content from a file");
FakeMail mail = new FakeMail(); FakeMail mail = new FakeMail();
Expand All @@ -108,6 +114,7 @@ public FakeMail build() throws MessagingException {
} }
mail.setSender(sender); mail.setSender(sender);
mail.setRecipients(recipients); mail.setRecipients(recipients);
mail.setRemoteAddr(remoteAddr);
return mail; return mail;
} }
} }
Expand Down
Expand Up @@ -18,29 +18,26 @@
****************************************************************/ ****************************************************************/
package org.apache.james.transport.matchers; package org.apache.james.transport.matchers;


import java.io.Serializable;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.List;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;


import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.ParseException;


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.mailet.Mail; import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress; import org.apache.mailet.MailAddress;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext; import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig; import org.apache.mailet.base.test.FakeMatcherConfig;


import com.google.common.collect.ImmutableList;

public abstract class AbstractRemoteAddrInNetworkTest { public abstract class AbstractRemoteAddrInNetworkTest {
protected static List<String> KNOWN_ADDRESSES = ImmutableList.of("192.168.200.0", "255.255.255.0", "192.168.200.1", "192.168.0.1", "192.168.1.1");


protected Mail mockedMail; protected Mail fakeMail;
protected AbstractNetworkMatcher matcher; protected AbstractNetworkMatcher matcher;
private String remoteAddr; private String remoteAddr;
private DNSService dnsServer; private DNSService dnsServer;
Expand All @@ -49,140 +46,19 @@ protected void setRemoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr; this.remoteAddr = remoteAddr;
} }


protected void setupMockedMail() { protected void setupFakeMail() throws MessagingException {
mockedMail = new Mail() { fakeMail = FakeMail.builder()

.recipient(new MailAddress("test@james.apache.org"))
private static final long serialVersionUID = 1L; .remoteAddr(remoteAddr)

.build();
@Override
public String getName() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void setName(String newName) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public MimeMessage getMessage() throws MessagingException {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Collection<MailAddress> getRecipients() {
ArrayList<MailAddress> r = new ArrayList<MailAddress>();
try {
r = new ArrayList<MailAddress>(Arrays.asList(new MailAddress[]{new MailAddress(
"test@james.apache.org")}));
} catch (ParseException e) {
}
return r;
}

@Override
public void setRecipients(Collection<MailAddress> recipients) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public MailAddress getSender() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public String getState() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public String getRemoteHost() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public String getRemoteAddr() {
return remoteAddr;
}

@Override
public String getErrorMessage() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void setErrorMessage(String msg) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void setMessage(MimeMessage message) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void setState(String state) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Serializable getAttribute(String name) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Iterator<String> getAttributeNames() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public boolean hasAttributes() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Serializable removeAttribute(String name) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void removeAllAttributes() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Serializable setAttribute(String name, Serializable object) {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public long getMessageSize() throws MessagingException {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public Date getLastUpdated() {
throw new UnsupportedOperationException("Unimplemented mock service");
}

@Override
public void setLastUpdated(Date lastUpdated) {
throw new UnsupportedOperationException("Unimplemented mock service");
}
};

} }


protected void setupDNSServer() { protected void setupDNSServer() {
dnsServer = new MockDNSService() { dnsServer = new MockDNSService() {


@Override @Override
public InetAddress getByName(String host) throws UnknownHostException { public InetAddress getByName(String host) throws UnknownHostException {
if ("192.168.200.0".equals(host) if (KNOWN_ADDRESSES.contains(host)) {
|| "255.255.255.0".equals(host)
|| "192.168.200.1".equals(host)
|| "192.168.0.1".equals(host)
|| "192.168.1.1".equals(host)) {
// called with an IP it only check formal validity // called with an IP it only check formal validity
return InetAddress.getByName(host); return InetAddress.getByName(host);
} }
Expand All @@ -203,7 +79,7 @@ protected void setupMatcher() throws MessagingException {


protected void setupAll() throws MessagingException { protected void setupAll() throws MessagingException {
setupDNSServer(); setupDNSServer();
setupMockedMail(); setupFakeMail();
setupMatcher(); setupMatcher();
} }


Expand Down
Expand Up @@ -33,10 +33,10 @@ public void testRemoteAddrInNetworkMatched() throws MessagingException {


setupAll(); setupAll();


Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);


assertNotNull(matchedRecipients); assertNotNull(matchedRecipients);
assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size()); assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
} }


// test if no recipient get returned cause it not match // test if no recipient get returned cause it not match
Expand All @@ -46,7 +46,7 @@ public void testRemoteAddrInNetworkNotMatch() throws MessagingException {


setupAll(); setupAll();


Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);


assertNull(matchedRecipients); assertNull(matchedRecipients);
} }
Expand Down
Expand Up @@ -33,10 +33,10 @@ public void testRemoteAddrNotInNetworkMatched() throws MessagingException {


setupAll(); setupAll();


Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);


assertNotNull(matchedRecipients); assertNotNull(matchedRecipients);
assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size()); assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
} }


// test if no recipient get returned cause it not match // test if no recipient get returned cause it not match
Expand All @@ -46,7 +46,7 @@ public void testRemoteAddrNotInNetworkNotMatch() throws MessagingException {


setupAll(); setupAll();


Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);


assertNull(matchedRecipients); assertNull(matchedRecipients);
} }
Expand Down

0 comments on commit 92642bd

Please sign in to comment.