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 no...@apache.org on 2006/11/27 20:29:40 UTC
svn commit: r479731 - in /james/server/trunk/src:
java/org/apache/james/transport/matchers/
test/org/apache/james/test/mock/mailet/
test/org/apache/james/transport/matchers/
Author: norman
Date: Mon Nov 27 11:29:39 2006
New Revision: 479731
URL: http://svn.apache.org/viewvc?view=rev&rev=479731
Log:
Some refactoring on SizeGreaterThen matcher and InSpammerBlacklist matcher
Add junit tests for SizeGreaterThen,InSpammerBlacklist and NESSpamCheck matchers
Added:
james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java (with props)
james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java (with props)
james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java (with props)
Modified:
james/server/trunk/src/java/org/apache/james/transport/matchers/InSpammerBlacklist.java
james/server/trunk/src/java/org/apache/james/transport/matchers/SizeGreaterThan.java
james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMail.java
Modified: james/server/trunk/src/java/org/apache/james/transport/matchers/InSpammerBlacklist.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/matchers/InSpammerBlacklist.java?view=diff&rev=479731&r1=479730&r2=479731
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/matchers/InSpammerBlacklist.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/matchers/InSpammerBlacklist.java Mon Nov 27 11:29:39 2006
@@ -44,18 +44,21 @@
* </ul>
*
* Example:
- * <mailet match="InSpammerBlacklist=blackholes.mail-abuse.org" class="ToProcessor">
+ * <mailet match="InSpammerBlacklist=blackholes.mail-abuse.org." class="ToProcessor">
* <processor>spam</processor>
* </mailet>
*
*/
public class InSpammerBlacklist extends GenericMatcher {
- String network = null;
+ private String network = null;
private DNSServer dnsServer;
public void init() throws MessagingException {
network = getCondition();
+
+ // check if the needed condition was given
+ if (network == null) throw new MessagingException("Please configure a blacklist");
ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
Modified: james/server/trunk/src/java/org/apache/james/transport/matchers/SizeGreaterThan.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/matchers/SizeGreaterThan.java?view=diff&rev=479731&r1=479730&r2=479731
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/matchers/SizeGreaterThan.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/matchers/SizeGreaterThan.java Mon Nov 27 11:29:39 2006
@@ -24,11 +24,8 @@
import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
-import javax.mail.Header;
import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
import java.util.Collection;
-import java.util.Enumeration;
import java.util.Locale;
/**
@@ -42,28 +39,34 @@
int cutoff = 0;
- public void init() {
- String amount = getCondition().trim().toLowerCase(Locale.US);
- if (amount.endsWith("k")) {
- amount = amount.substring(0, amount.length() - 1);
- cutoff = Integer.parseInt(amount) * 1024;
- } else if (amount.endsWith("m")) {
- amount = amount.substring(0, amount.length() - 1);
- cutoff = Integer.parseInt(amount) * 1024 * 1024;
+ public void init() throws MessagingException {
+ String amount = getCondition();
+
+ if (amount != null) {
+ amount = amount.trim().toLowerCase(Locale.US);
} else {
- cutoff = Integer.parseInt(amount);
+ throw new MessagingException("Please configure an amount");
+ }
+ try {
+ if (amount.endsWith("k")) {
+ amount = amount.substring(0, amount.length() - 1);
+ cutoff = Integer.parseInt(amount) * 1024;
+ } else if (amount.endsWith("m")) {
+ amount = amount.substring(0, amount.length() - 1);
+ cutoff = Integer.parseInt(amount) * 1024 * 1024;
+ } else {
+ cutoff = Integer.parseInt(amount);
+ }
+ } catch (NumberFormatException e) {
+ throw new MessagingException("Invalid amount: " + amount);
}
}
+ /**
+ * @see org.apache.mailet.GenericMatcher#match(org.apache.mailet.Mail)
+ */
public Collection match(Mail mail) throws MessagingException {
- MimeMessage message = mail.getMessage();
- //Calculate the size
- int size = message.getSize();
- Enumeration e = message.getAllHeaders();
- while (e.hasMoreElements()) {
- size += ((Header)e.nextElement()).toString().length();
- }
- if (size > cutoff) {
+ if (mail.getMessageSize() > cutoff) {
return mail.getRecipients();
} else {
return null;
Modified: james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMail.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMail.java?view=diff&rev=479731&r1=479730&r2=479731
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMail.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMail.java Mon Nov 27 11:29:39 2006
@@ -53,6 +53,10 @@
private static final long serialVersionUID = 1L;
+ private long size = 0;
+
+ private String remoteAddr ="127.0.0.1";
+
public String getName() {
return name;
}
@@ -86,7 +90,7 @@
}
public String getRemoteAddr() {
- return "my.mock.remote.host";
+ return remoteAddr;
}
public String getErrorMessage() {
@@ -132,7 +136,7 @@
}
public long getMessageSize() throws MessagingException {
- throw new UnsupportedOperationException("Unimplemented mock service");
+ return size;
}
public Date getLastUpdated() {
@@ -142,5 +146,12 @@
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
+
+ public void setMessageSize(long size) {
+ this.size = size;
+ }
+ public void setRemoteAddr(String remoteAddr) {
+ this.remoteAddr = remoteAddr;
+ }
}
Added: james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java?view=auto&rev=479731
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java Mon Nov 27 11:29:39 2006
@@ -0,0 +1,110 @@
+/****************************************************************
+ * 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 org.apache.james.Constants;
+import org.apache.james.services.AbstractDNSServer;
+import org.apache.james.services.DNSServer;
+import org.apache.james.test.mock.avalon.MockServiceManager;
+import org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.test.mock.mailet.MockMailContext;
+import org.apache.james.test.mock.mailet.MockMatcherConfig;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Matcher;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.ParseException;
+
+import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import junit.framework.TestCase;
+
+public class InSpammerBlacklistTest extends TestCase {
+
+ private MockMail mockedMail;
+
+ private Matcher matcher;
+
+ private final static String BLACKLIST = "my.black.list.";
+ private final static StringBuffer LISTED_HOST = new StringBuffer("111.222.111.222");
+
+ public InSpammerBlacklistTest(String arg0) throws UnsupportedEncodingException {
+ super(arg0);
+ }
+
+ private MockServiceManager setUpServiceManager() {
+ MockServiceManager sMan = new MockServiceManager();
+ sMan.put(DNSServer.ROLE, setUpDNSServer());
+ return sMan;
+ }
+
+ private DNSServer setUpDNSServer() {
+ DNSServer dns = new AbstractDNSServer() {
+ public InetAddress getByName(String name) throws UnknownHostException {
+ if (name.equals(LISTED_HOST.reverse() + "." + BLACKLIST)) {
+ return null;
+ } else {
+ throw new UnknownHostException("Not listed");
+ }
+ }
+ };
+ return dns;
+ }
+ private void setupMockedMail(String remoteAddr) throws ParseException {
+ mockedMail = new MockMail();
+ mockedMail.setRemoteAddr(remoteAddr);
+ mockedMail.setRecipients(Arrays.asList(new MailAddress[] {new MailAddress("test@email")}));
+
+ }
+
+ private void setupMatcher(String blacklist) throws MessagingException {
+ matcher = new InSpammerBlacklist();
+ MockMailContext context = new MockMailContext();
+ context.setAttribute(Constants.AVALON_COMPONENT_MANAGER, setUpServiceManager());
+ MockMatcherConfig mci = new MockMatcherConfig("InSpammerBlacklist=" + blacklist,context);
+ matcher.init(mci);
+ }
+
+
+ public void testInBlackList() throws MessagingException {
+ setupMockedMail(LISTED_HOST.toString());
+ setupMatcher(BLACKLIST);
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNotNull(matchedRecipients);
+ assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+ }
+
+ public void testNotInBlackList() throws MessagingException {
+ setupMockedMail("212.12.14.1");
+ setupMatcher(BLACKLIST);
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNull(matchedRecipients);
+ }
+}
Propchange: james/server/trunk/src/test/org/apache/james/transport/matchers/InSpammerBlacklistTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java?view=auto&rev=479731
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java Mon Nov 27 11:29:39 2006
@@ -0,0 +1,94 @@
+/****************************************************************
+ * 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 junit.framework.TestCase;
+import org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.test.mock.mailet.MockMailContext;
+import org.apache.james.test.mock.mailet.MockMatcherConfig;
+import org.apache.james.test.util.Util;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.RFC2822Headers;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.io.UnsupportedEncodingException;
+import java.util.Collection;
+
+public class NESSpamCheckTest extends TestCase {
+
+ private MimeMessage mockedMimeMessage;
+
+ private MockMail mockedMail;
+
+ private Matcher matcher;
+
+ private String headerName = "defaultHeaderName";
+
+ private String headerValue = "defaultHeaderValue";
+
+ public NESSpamCheckTest(String arg0) throws UnsupportedEncodingException {
+ super(arg0);
+ }
+
+ private void setHeaderName(String headerName) {
+ this.headerName = headerName;
+ }
+
+ private void setHeaderValue(String headerValue) {
+ this.headerValue = headerValue;
+ }
+
+ private void setupMockedMimeMessage() throws MessagingException {
+ mockedMimeMessage = Util.createMimeMessage(headerName, headerValue);
+ }
+
+ private void setupMatcher() throws MessagingException {
+ setupMockedMimeMessage();
+ matcher = new NESSpamCheck();
+ MockMatcherConfig mci = new MockMatcherConfig("NESSpamCheck", new MockMailContext());
+ matcher.init(mci);
+ }
+
+ public void testNESSpamCheckMatched() throws MessagingException {
+ setHeaderName(RFC2822Headers.RECEIVED);
+ setHeaderValue("xxxxxxxxxxxxxxxxxxxxx");
+
+ setupMockedMimeMessage();
+ mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
+ setupMatcher();
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNotNull(matchedRecipients);
+ assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+ }
+
+ public void testNESSpamCheckNotMatched() throws MessagingException {
+ setupMockedMimeMessage();
+ mockedMail = Util.createMockMail2Recipients(mockedMimeMessage);
+ setupMatcher();
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNull(matchedRecipients);
+ }
+}
Propchange: james/server/trunk/src/test/org/apache/james/transport/matchers/NESSpamCheckTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java?view=auto&rev=479731
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java Mon Nov 27 11:29:39 2006
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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 org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.test.mock.mailet.MockMailContext;
+import org.apache.james.test.mock.mailet.MockMatcherConfig;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Matcher;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.ParseException;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import junit.framework.TestCase;
+
+public class SizeGreaterThanTest extends TestCase {
+
+ private MockMail mockedMail;
+
+ private Matcher matcher;
+
+ public SizeGreaterThanTest(String arg0) throws UnsupportedEncodingException {
+ super(arg0);
+ }
+
+ private void setupMockedMail(long size) throws ParseException {
+ mockedMail = new MockMail();
+ mockedMail.setMessageSize(size);
+ mockedMail.setRecipients(Arrays.asList(new MailAddress[] {new MailAddress("test@email")}));
+
+ }
+
+ private void setupMatcher(String size) throws MessagingException {
+ matcher = new SizeGreaterThan();
+ MockMatcherConfig mci = new MockMatcherConfig("SizeGreaterThan=" + size,
+ new MockMailContext());
+ matcher.init(mci);
+ }
+
+
+ public void testSizeGreater() throws MessagingException {
+ setupMockedMail(2000000);
+ setupMatcher("1m");
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNotNull(matchedRecipients);
+ assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size());
+ }
+
+ public void testSizeNotGreater() throws MessagingException {
+ setupMockedMail(200000);
+ setupMatcher("1m");
+
+ Collection matchedRecipients = matcher.match(mockedMail);
+
+ assertNull(matchedRecipients);
+ }
+
+ public void testThrowExceptionOnInvalidAmount(){
+ boolean exception = false;
+ try {
+ setupMatcher("1mb");
+ } catch (MessagingException e) {
+ exception = true;
+ }
+ assertTrue("Exception thrown", exception);
+ }
+}
Propchange: james/server/trunk/src/test/org/apache/james/transport/matchers/SizeGreaterThanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org