james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r413143 - in /james/server/trunk/src/test/org/apache/james/transport/matchers: AbstractRemoteAddrInNetworkTest.java RemoteAddrInNetworkTest.java
Date Fri, 09 Jun 2006 20:00:00 GMT
Author: norman
Date: Fri Jun  9 12:59:59 2006
New Revision: 413143

URL: http://svn.apache.org/viewvc?rev=413143&view=rev
Log:
Add junit test for RemoteAddrInNetwork matcher. Test for RemoteAddrNotInNetwork will follow

Added:
    james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
    james/server/trunk/src/test/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java

Added: james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java?rev=413143&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
(added)
+++ james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractRemoteAddrInNetworkTest.java
Fri Jun  9 12:59:59 2006
@@ -0,0 +1,245 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.io.Serializable;
+import java.io.UnsupportedEncodingException;
+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 javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.Constants;
+import org.apache.james.services.DNSServer;
+import org.apache.james.test.mock.avalon.MockServiceManager;
+import org.apache.james.test.mock.mailet.MockMailContext;
+import org.apache.james.test.mock.mailet.MockMatcherConfig;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+
+public abstract class AbstractRemoteAddrInNetworkTest extends TestCase {
+
+    protected Mail mockedMail;
+
+    protected AbstractNetworkMatcher matcher;
+
+    private String remoteAddr;
+
+    private DNSServer dnsServer;
+
+    private MockServiceManager m_serviceManager;
+
+    public AbstractRemoteAddrInNetworkTest(String arg0)
+            throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    protected void setRemoteAddr(String remoteAddr) {
+        this.remoteAddr = remoteAddr;
+    }
+
+    protected void setupMockedMail() {
+        mockedMail = new Mail() {
+
+            private static final long serialVersionUID = 1L;
+
+            public String getName() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void setName(String newName) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public MimeMessage getMessage() throws MessagingException {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Collection getRecipients() {
+                ArrayList r = new ArrayList();
+                try {
+                    r = new ArrayList(Arrays
+                            .asList(new MailAddress[] { new MailAddress(
+                                    "test@james.apache.org") }));
+                } catch (ParseException e) {
+                }
+                return r;
+            }
+
+            public void setRecipients(Collection recipients) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public MailAddress getSender() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public String getState() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public String getRemoteHost() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public String getRemoteAddr() {
+                return remoteAddr;
+            }
+
+            public String getErrorMessage() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void setErrorMessage(String msg) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void setMessage(MimeMessage message) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void setState(String state) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Serializable getAttribute(String name) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Iterator getAttributeNames() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public boolean hasAttributes() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Serializable removeAttribute(String name) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void removeAllAttributes() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Serializable setAttribute(String name, Serializable object) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public long getMessageSize() throws MessagingException {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public Date getLastUpdated() {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+            public void setLastUpdated(Date lastUpdated) {
+                throw new UnsupportedOperationException(
+                        "Unimplemented mock service");
+            }
+
+        };
+
+    }
+
+    protected void setupDNSServer() {
+        dnsServer = new DNSServer() {
+
+            public Collection findMXRecords(String hostname) {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public Collection findTXTRecords(String hostname) {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public Iterator getSMTPHostAddresses(String domainName) {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public InetAddress[] getAllByName(String host)
+                    throws UnknownHostException {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public InetAddress getByName(String host)
+                    throws UnknownHostException {
+                // TODO Auto-generated method stub
+                return InetAddress.getByName(host);
+            }
+
+        };
+    }
+
+    protected void setupMatcher() throws MessagingException {
+        m_serviceManager = new MockServiceManager();
+        m_serviceManager.put(DNSServer.ROLE, dnsServer);
+
+        MockMailContext mmc = new MockMailContext();
+        mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER, m_serviceManager);
+        matcher = createMatcher();
+        MockMatcherConfig mci = new MockMatcherConfig(getConfigOption()
+                + getAllowedNetworks(), mmc);
+        matcher.init(mci);
+    }
+
+    protected void setupAll() throws MessagingException {
+        setupDNSServer();
+        setupMockedMail();
+        setupMatcher();
+    }
+
+    protected abstract String getConfigOption();
+
+    protected abstract String getAllowedNetworks();
+
+    protected abstract AbstractNetworkMatcher createMatcher();
+}

Added: james/server/trunk/src/test/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java?rev=413143&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
(added)
+++ james/server/trunk/src/test/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
Fri Jun  9 12:59:59 2006
@@ -0,0 +1,69 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.io.UnsupportedEncodingException;
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+public class RemoteAddrInNetworkTest extends AbstractRemoteAddrInNetworkTest {
+
+    private final String ALLOWED_NETWORK = "192.168.200.0/24";
+
+    public RemoteAddrInNetworkTest(String arg0)
+            throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    // test if the recipients get returned as matched
+    public void RemoteAddrInNetworkMatched() throws MessagingException {
+        setRemoteAddr("192.168.200.1");
+
+        setupAll();
+
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNotNull(matchedRecipients);
+        assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
+                .size());
+    }
+
+    // test if no recipient get returned cause it not match
+    public void RemoteAddrInNetworkNotMatch() throws MessagingException {
+        setRemoteAddr("192.168.1.1");
+
+        setupAll();
+
+        Collection matchedRecipients = matcher.match(mockedMail);
+
+        assertNull(matchedRecipients);
+    }
+
+    protected AbstractNetworkMatcher createMatcher() {
+        return new RemoteAddrInNetwork();
+    }
+
+    protected String getConfigOption() {
+        return "AllowedNetworkIs=";
+    }
+
+    protected String getAllowedNetworks() {
+        return ALLOWED_NETWORK;
+    }
+}



---------------------------------------------------------------------
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