You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2016/08/29 10:39:27 UTC
[4/5] james-project git commit: MAILET-108 HostIsLocalTest should
match our coding conventions
MAILET-108 HostIsLocalTest should match our coding conventions
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/262f0f2c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/262f0f2c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/262f0f2c
Branch: refs/heads/master
Commit: 262f0f2c2c5206278601525283dab497c61b921c
Parents: b1e0f3e
Author: Benoit Tellier <bt...@linagora.com>
Authored: Wed Aug 17 13:09:23 2016 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Mon Aug 29 17:38:41 2016 +0700
----------------------------------------------------------------------
mailet/pom.xml | 5 +
mailet/standard/pom.xml | 5 +
.../transport/matchers/HostIsLocalTest.java | 220 +++----------------
3 files changed, 41 insertions(+), 189 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/262f0f2c/mailet/pom.xml
----------------------------------------------------------------------
diff --git a/mailet/pom.xml b/mailet/pom.xml
index f4ac702..8639392 100644
--- a/mailet/pom.xml
+++ b/mailet/pom.xml
@@ -156,6 +156,11 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.9.5</version>
+ </dependency>
</dependencies>
</dependencyManagement>
http://git-wip-us.apache.org/repos/asf/james-project/blob/262f0f2c/mailet/standard/pom.xml
----------------------------------------------------------------------
diff --git a/mailet/standard/pom.xml b/mailet/standard/pom.xml
index 28cbf6d..5b9f776 100644
--- a/mailet/standard/pom.xml
+++ b/mailet/standard/pom.xml
@@ -63,6 +63,11 @@
<artifactId>mail</artifactId>
</dependency>
<dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/james-project/blob/262f0f2c/mailet/standard/src/test/java/org/apache/james/transport/matchers/HostIsLocalTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HostIsLocalTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HostIsLocalTest.java
index 78e5cf3..11965ff 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HostIsLocalTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HostIsLocalTest.java
@@ -20,233 +20,75 @@
package org.apache.james.transport.matchers;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
import java.util.List;
import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-import org.apache.mailet.HostAddress;
-import org.apache.mailet.LookupException;
-import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.apache.mailet.MailetContext;
import org.apache.mailet.Matcher;
-import org.apache.mailet.TemporaryLookupException;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMatcherConfig;
-import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
-@SuppressWarnings("deprecation")
+import com.google.common.collect.ImmutableList;
+
public class HostIsLocalTest {
- private FakeMail mockedMail;
+ public static final String JAMES_APACHE_ORG = "james.apache.org";
+ public static final String JAMES2_APACHE_ORG = "james2.apache.org";
+ private FakeMail mockedMail;
private Matcher matcher;
- private final String[] LOCALSERVER = new String[]{"james.apache.org"};
-
- private MailAddress[] recipients;
-
- private void setRecipients(MailAddress[] recipients) {
- this.recipients = recipients;
- }
-
- private void setupMockedMail() {
+ @Before
+ public void setUp() throws Exception {
mockedMail = new FakeMail();
- mockedMail.setRecipients(Arrays.asList(recipients));
- }
-
- private void setupMatcher() throws MessagingException {
-
- MailetContext FakeMailContext = new MailetContext() {
-
- Collection<String> localServer = new ArrayList<String>(Arrays.asList(LOCALSERVER));
-
- public void bounce(Mail mail, String message)
- throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
-
- }
-
- public void bounce(Mail mail, String message, MailAddress bouncer)
- throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
-
- }
-
- public Collection<String> getMailServers(String host) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public MailAddress getPostmaster() {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public Object getAttribute(String name) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public Iterator<String> getAttributeNames() {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public int getMajorVersion() {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public int getMinorVersion() {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public String getServerInfo() {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public boolean isLocalServer(String serverName) {
- return localServer.contains(serverName);
- }
-
- public boolean isLocalUser(String userAccount) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public boolean isLocalEmail(MailAddress mailAddress) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void log(String message) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void log(String message, Throwable t) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void removeAttribute(String name) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void sendMail(MimeMessage msg) throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void sendMail(MailAddress sender, Collection<MailAddress> recipients,
- MimeMessage msg) throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void sendMail(MailAddress sender, Collection<MailAddress> recipients,
- MimeMessage msg, String state) throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void sendMail(Mail mail) throws MessagingException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void setAttribute(String name, Object object) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public Iterator<HostAddress> getSMTPHostAddresses(String domainName) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void log(LogLevel level, String message) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public void log(LogLevel level, String message, Throwable t) {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
- public List<String> dnsLookup(String name, RecordType type) throws LookupException {
- throw new UnsupportedOperationException(
- "Unimplemented mock service");
- }
-
-
- };
+ MailetContext mailContext = mock(MailetContext.class);
+ when(mailContext.isLocalServer(JAMES_APACHE_ORG)).thenReturn(true);
+ when(mailContext.isLocalServer(JAMES2_APACHE_ORG)).thenReturn(false);
matcher = new HostIsLocal();
- FakeMatcherConfig mci = new FakeMatcherConfig("HostIsLocal",
- FakeMailContext);
+ FakeMatcherConfig mci = new FakeMatcherConfig("HostIsLocal", mailContext);
matcher.init(mci);
}
// test if all recipients get returned as matched
@Test
public void testHostIsMatchedAllRecipients() throws MessagingException {
- setRecipients(new MailAddress[]{
- new MailAddress("test@james.apache.org"),
- new MailAddress("test2@james.apache.org")});
-
- setupMockedMail();
- setupMatcher();
-
- Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+ MailAddress mailAddress1 = new MailAddress("test@" + JAMES_APACHE_ORG);
+ MailAddress mailAddress2 = new MailAddress("test2@" + JAMES_APACHE_ORG);
+ mockedMail.setRecipients(ImmutableList.of(
+ mailAddress1,
+ mailAddress2));
- Assert.assertNotNull(matchedRecipients);
- Assert.assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
- .size());
+ assertThat( matcher.match(mockedMail)).containsExactly(mailAddress1, mailAddress2);
}
// test if one recipients get returned as matched
@Test
public void testHostIsMatchedOneRecipient() throws MessagingException {
- setRecipients(new MailAddress[]{
- new MailAddress("test@james2.apache.org"),
- new MailAddress("test2@james.apache.org")});
+ MailAddress matchingAddress = new MailAddress("test2@" +JAMES_APACHE_ORG);
+ mockedMail.setRecipients(ImmutableList.of(
+ new MailAddress("test@" + JAMES2_APACHE_ORG),
+ matchingAddress));
- setupMockedMail();
- setupMatcher();
-
- Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
- Assert.assertNotNull(matchedRecipients);
- Assert.assertEquals(matchedRecipients.size(), 1);
+ assertThat( matcher.match(mockedMail)).containsExactly(matchingAddress);
}
// test if no recipient get returned cause it not match
@Test
public void testHostIsNotMatch() throws MessagingException {
- setRecipients(new MailAddress[]{
- new MailAddress("test@james2.apache.org"),
- new MailAddress("test2@james2.apache.org")});
-
- setupMockedMail();
- setupMatcher();
-
- Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+ mockedMail.setRecipients(
+ ImmutableList.of(new MailAddress("test@" + JAMES2_APACHE_ORG),
+ new MailAddress("test2@" + JAMES2_APACHE_ORG)));
- Assert.assertEquals(matchedRecipients.size(), 0);
+ assertThat(matcher.match(mockedMail)).isEmpty();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org