Author: norman Date: Sun Jun 11 07:17:27 2006 New Revision: 413449 URL: http://svn.apache.org/viewvc?rev=413449&view=rev Log: Throw an MessagingException if pattern is empty Add junit test for SenderIsRegex and SenderIsNull matcher
Added: james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsNullTest.java james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsRegexTest.java Modified: james/server/trunk/src/java/org/apache/james/transport/matchers/SenderIsRegex.java Modified: james/server/trunk/src/java/org/apache/james/transport/matchers/SenderIsRegex.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/matchers/SenderIsRegex.java?rev=413449&r1=413448&r2=413449&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/transport/matchers/SenderIsRegex.java (original) +++ james/server/trunk/src/java/org/apache/james/transport/matchers/SenderIsRegex.java Sun Jun 11 07:17:27 2006 @@ -56,7 +56,7 @@ public void init() throws MessagingException { String patternString = getCondition(); - if (patternString == null) { + if ((patternString == null) || (patternString.equals(""))) { throw new MessagingException("Pattern is missing"); } Added: james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsNullTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsNullTest.java?rev=413449&view=auto ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsNullTest.java (added) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsNullTest.java Sun Jun 11 07:17:27 2006 @@ -0,0 +1,71 @@ +/*********************************************************************** + * 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; + +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; + +public class SenderIsNullTest extends AbstractSenderIsTest { + + public SenderIsNullTest(String arg0) throws UnsupportedEncodingException { + super(arg0); + } + + // test if matched + public void testSenderIsMatchedAllRecipients() throws MessagingException { + setSender(null); + + setupMockedMail(); + setupMatcher(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(matchedRecipients.size(), mockedMail.getRecipients() + .size()); + } + + // test if not matched + public void testSenderIsNotMatchedAllRecipients() throws MessagingException { + setSender(new MailAddress("[EMAIL PROTECTED]")); + + setupMockedMail(); + setupMatcher(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNull(matchedRecipients); + } + + protected Matcher createMatcher() { + return new SenderIsNull(); + } + + protected String getConfigOption() { + return "SenderIsNull"; + } + + protected String getConfigValue() { + return ""; + } +} Added: james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsRegexTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsRegexTest.java?rev=413449&view=auto ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsRegexTest.java (added) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/SenderIsRegexTest.java Sun Jun 11 07:17:27 2006 @@ -0,0 +1,115 @@ +/*********************************************************************** + * 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; + +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; + +public class SenderIsRegexTest extends AbstractSenderIsTest { + + private final String SENDER_NAME = "[EMAIL PROTECTED]"; + + private String regex = ".*"; + + public SenderIsRegexTest(String arg0) throws UnsupportedEncodingException { + super(arg0); + } + + private void setRegex(String regex) { + this.regex = regex; + } + + // test if matched + public void testSenderIsRegexMatchedAllRecipients() + throws MessagingException { + setSender(new MailAddress(SENDER_NAME)); + setRegex("[EMAIL PROTECTED]"); + setupMockedMail(); + setupMatcher(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(matchedRecipients.size(), mockedMail.getRecipients() + .size()); + } + + // test if not matched + public void testSenderIsRegexNotMatchedAllRecipients() + throws MessagingException { + setSender(new MailAddress("[EMAIL PROTECTED]")); + setRegex("^\\."); + + setupMockedMail(); + setupMatcher(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNull(matchedRecipients); + } + + // test if throw exception if no pattern is used + public void testThrowExceptionWithoutPattern() throws MessagingException { + boolean exceptionCatched = false; + setSender(new MailAddress("[EMAIL PROTECTED]")); + setRegex(""); + + setupMockedMail(); + + try { + setupMatcher(); + } catch (MessagingException m) { + exceptionCatched = true; + } + assertTrue(exceptionCatched); + } + + // test if throw exception if invalid pattern is used + public void testThrowExceptionWithInvalidPattern() + throws MessagingException { + boolean exceptionCatched = false; + setSender(new MailAddress("[EMAIL PROTECTED]")); + setRegex("(."); + + setupMockedMail(); + + try { + setupMatcher(); + } catch (MessagingException m) { + exceptionCatched = true; + } + assertTrue(exceptionCatched); + } + + protected Matcher createMatcher() { + return new SenderIsRegex(); + } + + protected String getConfigOption() { + return "SenderIsRegex="; + } + + protected String getConfigValue() { + return regex; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]