james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [3/4] james-project git commit: MAILET-129 Simplify AbstractRemoteAddrInNetworkTest before removing it
Date Thu, 08 Sep 2016 14:32:13 GMT
MAILET-129 Simplify AbstractRemoteAddrInNetworkTest before removing it


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/92642bd1
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/92642bd1
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/92642bd1

Branch: refs/heads/master
Commit: 92642bd1c9a10c54fbc97b5e87fd79c7494df6fd
Parents: f88464d
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Wed Sep 7 15:00:16 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Thu Sep 8 16:29:53 2016 +0200

----------------------------------------------------------------------
 .../org/apache/mailet/base/test/FakeMail.java   |   7 +
 .../AbstractRemoteAddrInNetworkTest.java        | 150 ++-----------------
 .../matchers/RemoteAddrInNetworkTest.java       |   6 +-
 .../matchers/RemoteAddrNotInNetworkTest.java    |   6 +-
 4 files changed, 26 insertions(+), 143 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/92642bd1/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMail.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMail.java b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMail.java
index 12b0a7c..77a1288 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMail.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMail.java
@@ -66,6 +66,7 @@ public class FakeMail implements Mail {
         private Optional<MimeMessage> mimeMessage = Optional.absent();
         private List<MailAddress> recipients = new ArrayList<MailAddress>();
         private MailAddress sender;
+        private String remoteAddr;
 
         public Builder fileName(String fileName) {
             this.fileName = Optional.of(fileName);
@@ -97,6 +98,11 @@ public class FakeMail implements Mail {
             return this;
         }
 
+        public Builder remoteAddr(String remoteAddr) {
+            this.remoteAddr = remoteAddr;
+            return this;
+        }
+
         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");
             FakeMail mail = new FakeMail();
@@ -108,6 +114,7 @@ public class FakeMail implements Mail {
             }
             mail.setSender(sender);
             mail.setRecipients(recipients);
+            mail.setRemoteAddr(remoteAddr);
             return mail;
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/92642bd1/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
index cd845ec..5e4c741 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
@@ -18,29 +18,26 @@
  ****************************************************************/
 package org.apache.james.transport.matchers;
 
-import java.io.Serializable;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Iterator;
+import java.util.List;
 
 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.mock.MockDNSService;
 import org.apache.mailet.Mail;
 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.FakeMatcherConfig;
 
+import com.google.common.collect.ImmutableList;
+
 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;
     private String remoteAddr;
     private DNSService dnsServer;
@@ -49,128 +46,11 @@ public abstract class AbstractRemoteAddrInNetworkTest {
         this.remoteAddr = remoteAddr;
     }
 
-    protected void setupMockedMail() {
-        mockedMail = new Mail() {
-
-            private static final long serialVersionUID = 1L;
-
-            @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 setupFakeMail() throws MessagingException {
+        fakeMail = FakeMail.builder()
+                        .recipient(new MailAddress("test@james.apache.org"))
+                        .remoteAddr(remoteAddr)
+                        .build();
     }
 
     protected void setupDNSServer() {
@@ -178,11 +58,7 @@ public abstract class AbstractRemoteAddrInNetworkTest {
 
             @Override
             public InetAddress getByName(String host) throws UnknownHostException {
-                if ("192.168.200.0".equals(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)) {
+                if (KNOWN_ADDRESSES.contains(host)) {
                     // called with an IP it only check formal validity
                     return InetAddress.getByName(host);
                 }
@@ -203,7 +79,7 @@ public abstract class AbstractRemoteAddrInNetworkTest {
 
     protected void setupAll() throws MessagingException {
         setupDNSServer();
-        setupMockedMail();
+        setupFakeMail();
         setupMatcher();
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/92642bd1/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
index 737f620..9c21958 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
@@ -33,10 +33,10 @@ public class RemoteAddrInNetworkTest extends AbstractRemoteAddrInNetworkTest
{
 
         setupAll();
 
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
 
         assertNotNull(matchedRecipients);
-        assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+        assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
     }
 
     // test if no recipient get returned cause it not match
@@ -46,7 +46,7 @@ public class RemoteAddrInNetworkTest extends AbstractRemoteAddrInNetworkTest
{
 
         setupAll();
 
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
 
         assertNull(matchedRecipients);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/92642bd1/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
index ab459f8..ed8780e 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
@@ -33,10 +33,10 @@ public class RemoteAddrNotInNetworkTest extends AbstractRemoteAddrInNetworkTest
 
         setupAll();
 
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
 
         assertNotNull(matchedRecipients);
-        assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+        assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
     }
 
     // test if no recipient get returned cause it not match
@@ -46,7 +46,7 @@ public class RemoteAddrNotInNetworkTest extends AbstractRemoteAddrInNetworkTest
 
         setupAll();
 
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
+        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
 
         assertNull(matchedRecipients);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message