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 ro...@apache.org on 2016/09/08 14:32:11 UTC

[1/4] james-project git commit: MAILET-129 Increase coverage and remove unused method

Repository: james-project
Updated Branches:
  refs/heads/master f88464d77 -> e17d56791


MAILET-129 Increase coverage and remove unused method


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

Branch: refs/heads/master
Commit: 0a4187893d9243c88db0d01f92f532f55924f172
Parents: e1e037d
Author: Raphael Ouazana <ra...@linagora.com>
Authored: Wed Sep 7 15:58:00 2016 +0200
Committer: Raphael Ouazana <ra...@linagora.com>
Committed: Thu Sep 8 16:29:53 2016 +0200

----------------------------------------------------------------------
 .../matchers/AbstractNetworkMatcher.java        |  4 ----
 .../matchers/RemoteAddrInNetworkTest.java       | 20 ++++++++++++++++++++
 .../matchers/RemoteAddrNotInNetworkTest.java    | 20 ++++++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0a418789/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
index 8c8b43f..3739a4f 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
@@ -85,10 +85,6 @@ public abstract class AbstractNetworkMatcher extends GenericMatcher {
         return networks;
     }
 
-    protected boolean matchNetwork(java.net.InetAddress addr) {
-        return authorizedNetworks != null && authorizedNetworks.matchInetNetwork(addr);
-    }
-
     protected boolean matchNetwork(String addr) {
         return authorizedNetworks != null && authorizedNetworks.matchInetNetwork(addr);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0a418789/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 624eb0e..336fbe9 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
@@ -74,4 +74,24 @@ public class RemoteAddrInNetworkTest {
 
         assertThat(actual).isNull();
     }
+
+    @Test
+    public void matchShouldNotMatchWhenNoCondition() throws MessagingException {
+        FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
+        RemoteAddrInNetwork testee = new RemoteAddrInNetwork();
+        testee.init(matcherConfig);
+
+        Collection<MailAddress> actual = testee.match(fakeMail);
+
+        assertThat(actual).isNull();
+    }
+
+    @Test
+    public void matchShouldNotMatchWhenInvalidAddress() throws MessagingException {
+        fakeMail.setRemoteAddr("invalid");
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).isNull();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0a418789/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 484566f..2605084 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
@@ -74,4 +74,24 @@ public class RemoteAddrNotInNetworkTest {
 
         assertThat(actual).isNull();
     }
+
+    @Test
+    public void matchShouldMatchWhenNoCondition() throws MessagingException {
+        FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
+        RemoteAddrNotInNetwork testee = new RemoteAddrNotInNetwork();
+        testee.init(matcherConfig);
+
+        Collection<MailAddress> actual = testee.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
+
+    @Test
+    public void matchShouldMatchWhenInvalidAddress() throws MessagingException {
+        fakeMail.setRemoteAddr("invalid");
+
+        Collection<MailAddress> actual = matcher.match(fakeMail);
+
+        assertThat(actual).containsOnly(testRecipient);
+    }
 }


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


[2/4] james-project git commit: MAILET-129 Rewrite tests to remove AbstractRemoteAddrInNetworkTest

Posted by ro...@apache.org.
MAILET-129 Rewrite tests to remove AbstractRemoteAddrInNetworkTest


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

Branch: refs/heads/master
Commit: e1e037d86b1f143628f7d328ebff158622e77869
Parents: 92642bd
Author: Raphael Ouazana <ra...@linagora.com>
Authored: Wed Sep 7 15:30:46 2016 +0200
Committer: Raphael Ouazana <ra...@linagora.com>
Committed: Thu Sep 8 16:29:53 2016 +0200

----------------------------------------------------------------------
 .../AbstractRemoteAddrInNetworkTest.java        | 91 --------------------
 .../matchers/RemoteAddrInNetworkTest.java       | 71 ++++++++-------
 .../matchers/RemoteAddrNotInNetworkTest.java    | 71 ++++++++-------
 3 files changed, 80 insertions(+), 153 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e1e037d8/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
deleted file mode 100644
index 5e4c741..0000000
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you under the Apache License, Version 2.0 (the            *
- * "License"); you may not use this file except in compliance   *
- * with the License.  You may obtain a copy of the License at   *
- *                                                              *
- *   http://www.apache.org/licenses/LICENSE-2.0                 *
- *                                                              *
- * Unless required by applicable law or agreed to in writing,   *
- * software distributed under the License is distributed on an  *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
- * KIND, either express or implied.  See the License for the    *
- * specific language governing permissions and limitations      *
- * under the License.                                           *
- ****************************************************************/
-package org.apache.james.transport.matchers;
-
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.List;
-
-import javax.mail.MessagingException;
-
-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 fakeMail;
-    protected AbstractNetworkMatcher matcher;
-    private String remoteAddr;
-    private DNSService dnsServer;
-
-    protected void setRemoteAddr(String remoteAddr) {
-        this.remoteAddr = remoteAddr;
-    }
-
-    protected void setupFakeMail() throws MessagingException {
-        fakeMail = FakeMail.builder()
-                        .recipient(new MailAddress("test@james.apache.org"))
-                        .remoteAddr(remoteAddr)
-                        .build();
-    }
-
-    protected void setupDNSServer() {
-        dnsServer = new MockDNSService() {
-
-            @Override
-            public InetAddress getByName(String host) throws UnknownHostException {
-                if (KNOWN_ADDRESSES.contains(host)) {
-                    // called with an IP it only check formal validity
-                    return InetAddress.getByName(host);
-                }
-                throw new UnsupportedOperationException(
-                        "getByName(" + host + ") unimplemented in AbstractRemoteAddrInNetworkTest");
-            }
-        };
-    }
-
-    protected void setupMatcher() throws MessagingException {
-
-        FakeMailContext mmc = FakeMailContext.defaultContext();
-        matcher = createMatcher();
-        matcher.setDNSService(dnsServer);
-        FakeMatcherConfig mci = new FakeMatcherConfig(getConfigOption() + getAllowedNetworks(), mmc);
-        matcher.init(mci);
-    }
-
-    protected void setupAll() throws MessagingException {
-        setupDNSServer();
-        setupFakeMail();
-        setupMatcher();
-    }
-
-    protected abstract String getConfigOption();
-
-    protected abstract String getAllowedNetworks();
-
-    protected abstract AbstractNetworkMatcher createMatcher();
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/e1e037d8/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 9c21958..624eb0e 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
@@ -18,51 +18,60 @@
  ****************************************************************/
 package org.apache.james.transport.matchers;
 
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Collection;
+
 import javax.mail.MessagingException;
+
+import org.apache.james.dnsservice.api.mock.MockDNSService;
 import org.apache.mailet.MailAddress;
-import static org.junit.Assert.*;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.Before;
 import org.junit.Test;
 
-public class RemoteAddrInNetworkTest extends AbstractRemoteAddrInNetworkTest {
-
-    // test if the recipients get returned as matched
-    @Test
-    public void testRemoteAddrInNetworkMatched() throws MessagingException {
-        setRemoteAddr("192.168.200.1");
-
-        setupAll();
+public class RemoteAddrInNetworkTest {
+    private RemoteAddrInNetwork matcher;
+    private FakeMail fakeMail;
+    private MailAddress testRecipient;
 
-        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
-
-        assertNotNull(matchedRecipients);
-        assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
+    @Before
+    public void setup() throws MessagingException {
+        MockDNSService dnsServer = new MockDNSService() {
+            @Override
+            public InetAddress getByName(String host) throws UnknownHostException {
+                return InetAddress.getByName(host);
+            }
+        };
+        FakeMatcherConfig matcherConfig = new FakeMatcherConfig("AllowedNetworkIs=192.168.200.0/24", FakeMailContext.defaultContext());
+        matcher = new RemoteAddrInNetwork();
+        matcher.setDNSService(dnsServer);
+        matcher.init(matcherConfig);
+        testRecipient = new MailAddress("test@james.apache.org");
+        fakeMail = FakeMail.builder()
+                .recipient(testRecipient)
+                .build();
     }
 
-    // test if no recipient get returned cause it not match
     @Test
-    public void testRemoteAddrInNetworkNotMatch() throws MessagingException {
-        setRemoteAddr("192.168.1.1");
-
-        setupAll();
+    public void matchShouldMatchWhenOnSameNetwork() throws MessagingException {
+        fakeMail.setRemoteAddr("192.168.200.1");
 
-        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
+        Collection<MailAddress> actual = matcher.match(fakeMail);
 
-        assertNull(matchedRecipients);
+        assertThat(actual).containsOnly(testRecipient);
     }
 
-    @Override
-    protected AbstractNetworkMatcher createMatcher() {
-        return new RemoteAddrInNetwork();
-    }
+    @Test
+    public void matchShouldNotMatchWhenOnDifferentNetwork() throws MessagingException {
+        fakeMail.setRemoteAddr("192.168.1.1");
 
-    @Override
-    protected String getConfigOption() {
-        return "AllowedNetworkIs=";
-    }
+        Collection<MailAddress> actual = matcher.match(fakeMail);
 
-    @Override
-    protected String getAllowedNetworks() {
-        return "192.168.200.0/24";
+        assertThat(actual).isNull();
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e1e037d8/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 ed8780e..484566f 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
@@ -18,51 +18,60 @@
  ****************************************************************/
 package org.apache.james.transport.matchers;
 
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Collection;
+
 import javax.mail.MessagingException;
+
+import org.apache.james.dnsservice.api.mock.MockDNSService;
 import org.apache.mailet.MailAddress;
-import static org.junit.Assert.*;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.Before;
 import org.junit.Test;
 
-public class RemoteAddrNotInNetworkTest extends AbstractRemoteAddrInNetworkTest {
-
-    // test if the recipients get returned as matched
-    @Test
-    public void testRemoteAddrNotInNetworkMatched() throws MessagingException {
-        setRemoteAddr("192.168.0.1");
-
-        setupAll();
+public class RemoteAddrNotInNetworkTest {
+    private RemoteAddrNotInNetwork matcher;
+    private FakeMail fakeMail;
+    private MailAddress testRecipient;
 
-        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
-
-        assertNotNull(matchedRecipients);
-        assertEquals(matchedRecipients.size(), fakeMail.getRecipients().size());
+    @Before
+    public void setup() throws MessagingException {
+        MockDNSService dnsServer = new MockDNSService() {
+            @Override
+            public InetAddress getByName(String host) throws UnknownHostException {
+                return InetAddress.getByName(host);
+            }
+        };
+        FakeMatcherConfig matcherConfig = new FakeMatcherConfig("AllowedNetworkIs=192.168.200.0/24", FakeMailContext.defaultContext());
+        matcher = new RemoteAddrNotInNetwork();
+        matcher.setDNSService(dnsServer);
+        matcher.init(matcherConfig);
+        testRecipient = new MailAddress("test@james.apache.org");
+        fakeMail = FakeMail.builder()
+                .recipient(testRecipient)
+                .build();
     }
 
-    // test if no recipient get returned cause it not match
     @Test
-    public void testRemoteAddrNotInNetworkNotMatch() throws MessagingException {
-        setRemoteAddr("192.168.200.1");
-
-        setupAll();
+    public void matchShouldMatchWhenOnDifferentNetwork() throws MessagingException {
+        fakeMail.setRemoteAddr("192.168.0.1");
 
-        Collection<MailAddress> matchedRecipients = matcher.match(fakeMail);
+        Collection<MailAddress> actual = matcher.match(fakeMail);
 
-        assertNull(matchedRecipients);
+        assertThat(actual).containsOnly(testRecipient);
     }
 
-    @Override
-    protected AbstractNetworkMatcher createMatcher() {
-        return new RemoteAddrNotInNetwork();
-    }
+    @Test
+    public void matchShouldNotMatchWhenOnSameNetwork() throws MessagingException {
+        fakeMail.setRemoteAddr("192.168.200.1");
 
-    @Override
-    protected String getConfigOption() {
-        return "AllowedNetworkIs=";
-    }
+        Collection<MailAddress> actual = matcher.match(fakeMail);
 
-    @Override
-    protected String getAllowedNetworks() {
-        return "192.168.200.0/24";
+        assertThat(actual).isNull();
     }
 }


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


[3/4] james-project git commit: MAILET-129 Simplify AbstractRemoteAddrInNetworkTest before removing it

Posted by ro...@apache.org.
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 <ra...@linagora.com>
Authored: Wed Sep 7 15:00:16 2016 +0200
Committer: Raphael Ouazana <ra...@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


[4/4] james-project git commit: MAILET-129 Simplify NetworkMatchers

Posted by ro...@apache.org.
MAILET-129 Simplify NetworkMatchers


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

Branch: refs/heads/master
Commit: e17d5679156d6f57891ea2c1d268594a6ee4498b
Parents: 0a41878
Author: Raphael Ouazana <ra...@linagora.com>
Authored: Wed Sep 7 16:59:46 2016 +0200
Committer: Raphael Ouazana <ra...@linagora.com>
Committed: Thu Sep 8 16:29:58 2016 +0200

----------------------------------------------------------------------
 .../library/netmatcher/NetMatcher.java          | 10 +++++
 .../matchers/AbstractNetworkMatcher.java        | 39 +++-----------------
 .../transport/matchers/RemoteAddrInNetwork.java |  6 ++-
 .../matchers/RemoteAddrNotInNetwork.java        |  6 ++-
 .../matchers/RemoteAddrInNetworkTest.java       |  8 ++--
 .../matchers/RemoteAddrNotInNetworkTest.java    |  8 ++--
 6 files changed, 33 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
----------------------------------------------------------------------
diff --git a/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java b/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
index 6816a7e..fc93531 100644
--- a/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
+++ b/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
@@ -23,6 +23,7 @@ import java.net.UnknownHostException;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -30,6 +31,8 @@ import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.library.inetnetwork.InetNetworkBuilder;
 import org.apache.james.dnsservice.library.inetnetwork.model.InetNetwork;
 
+import com.google.common.base.Splitter;
+
 /**
  * NetMatcher Class is used to check if an ipAddress match a network.
  * 
@@ -37,6 +40,7 @@ import org.apache.james.dnsservice.library.inetnetwork.model.InetNetwork;
  * address or domain name is within a set of subnets.
  */
 public class NetMatcher {
+    public static final String NETS_SEPARATOR = ", ";
 
     /**
      * The DNS Service used to build InetNetworks.
@@ -74,6 +78,12 @@ public class NetMatcher {
         initInetNetworks(nets);
     }
 
+    public NetMatcher(String commaSeparatedNets, DNSService dnsServer) {
+        this.dnsServer = dnsServer;
+        List<String> nets = Splitter.on(NETS_SEPARATOR).splitToList(commaSeparatedNets);
+        initInetNetworks(nets);
+    }
+
     /**
      * The given String may represent an IP address or a host name.
      * 

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
index 3739a4f..083dd77 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
@@ -18,9 +18,6 @@
  ****************************************************************/
 package org.apache.james.transport.matchers;
 
-import java.util.Collection;
-import java.util.StringTokenizer;
-
 import javax.inject.Inject;
 import javax.mail.MessagingException;
 
@@ -41,31 +38,21 @@ import org.apache.mailet.base.GenericMatcher;
  * </p>
  * <p>
  * This abstract network matcher needs to be implemented by a concrete class.<br>
- * The implementing concrete class will call the allowedNetworks or matchNetwork
- * methods.
+ * The implementing concrete class will call the matchNetwork method.
  * </p>
  * 
  * @see org.apache.james.dnsservice.library.netmatcher.NetMatcher
  */
 public abstract class AbstractNetworkMatcher extends GenericMatcher {
 
-    /**
-     * This is a Network Matcher that should be configured to contain authorized
-     * networks
-     */
-    private NetMatcher authorizedNetworks = null;
+    private NetMatcher authorizedNetworks;
 
-    /**
-     * The DNSService
-     */
     private DNSService dnsServer;
 
+    @Override
     public void init() throws MessagingException {
-
-        Collection<String> nets = allowedNetworks();
-
-        if (nets != null) {
-            authorizedNetworks = new NetMatcher(allowedNetworks(), dnsServer) {
+        if (getCondition() != null) {
+            authorizedNetworks = new NetMatcher(getCondition(), dnsServer) {
                 protected void log(String s) {
                     AbstractNetworkMatcher.this.log(s);
                 }
@@ -74,26 +61,10 @@ public abstract class AbstractNetworkMatcher extends GenericMatcher {
         }
     }
 
-    protected Collection<String> allowedNetworks() {
-        Collection<String> networks = null;
-        if (getCondition() != null) {
-            StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
-            networks = new java.util.ArrayList<String>();
-            while (st.hasMoreTokens())
-                networks.add(st.nextToken());
-        }
-        return networks;
-    }
-
     protected boolean matchNetwork(String addr) {
         return authorizedNetworks != null && authorizedNetworks.matchInetNetwork(addr);
     }
 
-    /**
-     * Injection setter for the DNSService.
-     * 
-     * @param dnsService
-     */
     @Inject
     public void setDNSService(DNSService dnsService) {
         this.dnsServer = dnsService;

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
index 8f7a480..a75f6c2 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
@@ -34,7 +34,11 @@ import java.util.Collection;
  * </p>
  */
 public class RemoteAddrInNetwork extends AbstractNetworkMatcher {
+    @Override
     public Collection<MailAddress> match(Mail mail) {
-        return matchNetwork(mail.getRemoteAddr()) ? mail.getRecipients() : null;
+        if (matchNetwork(mail.getRemoteAddr())) {
+            return mail.getRecipients();
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
index 47471e5..331bc07 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
@@ -34,7 +34,11 @@ import java.util.Collection;
  * </p>
  */
 public class RemoteAddrNotInNetwork extends AbstractNetworkMatcher {
+    @Override
     public Collection<MailAddress> match(Mail mail) {
-        return matchNetwork(mail.getRemoteAddr()) ? null : mail.getRecipients();
+        if (! matchNetwork(mail.getRemoteAddr())) {
+            return mail.getRecipients();
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/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 336fbe9..8c67ee6 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
@@ -58,7 +58,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenOnSameNetwork() throws MessagingException {
+    public void shouldMatchWhenOnSameNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.200.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -67,7 +67,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenOnDifferentNetwork() throws MessagingException {
+    public void shouldNotMatchWhenOnDifferentNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.1.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -76,7 +76,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenNoCondition() throws MessagingException {
+    public void shouldNotMatchWhenNoCondition() throws MessagingException {
         FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
         RemoteAddrInNetwork testee = new RemoteAddrInNetwork();
         testee.init(matcherConfig);
@@ -87,7 +87,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenInvalidAddress() throws MessagingException {
+    public void shouldNotMatchWhenInvalidAddress() throws MessagingException {
         fakeMail.setRemoteAddr("invalid");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/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 2605084..c1a4dbd 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
@@ -58,7 +58,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenOnDifferentNetwork() throws MessagingException {
+    public void shouldMatchWhenOnDifferentNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.0.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -67,7 +67,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenOnSameNetwork() throws MessagingException {
+    public void shouldNotMatchWhenOnSameNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.200.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -76,7 +76,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenNoCondition() throws MessagingException {
+    public void shouldMatchWhenNoCondition() throws MessagingException {
         FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
         RemoteAddrNotInNetwork testee = new RemoteAddrNotInNetwork();
         testee.init(matcherConfig);
@@ -87,7 +87,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenInvalidAddress() throws MessagingException {
+    public void shouldMatchWhenInvalidAddress() throws MessagingException {
         fakeMail.setRemoteAddr("invalid");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);


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