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:12 UTC

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

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