1 19 20 21 package org.apache.james.transport.matchers; 22 23 import java.io.UnsupportedEncodingException ; 24 import java.util.Collection ; 25 26 import javax.mail.MessagingException ; 27 28 import org.apache.mailet.MailAddress; 29 import org.apache.mailet.Matcher; 30 31 public class SenderIsRegexTest extends AbstractSenderIsTest { 32 33 private final String SENDER_NAME = "test@james.apache.org"; 34 35 private String regex = ".*"; 36 37 public SenderIsRegexTest(String arg0) throws UnsupportedEncodingException { 38 super(arg0); 39 } 40 41 private void setRegex(String regex) { 42 this.regex = regex; 43 } 44 45 public void testSenderIsRegexMatchedAllRecipients() 47 throws MessagingException { 48 setSender(new MailAddress(SENDER_NAME)); 49 setRegex(".*@.*"); 50 setupMockedMail(); 51 setupMatcher(); 52 53 Collection matchedRecipients = matcher.match(mockedMail); 54 55 assertNotNull(matchedRecipients); 56 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 57 .size()); 58 } 59 60 public void testSenderIsRegexNotMatchedAllRecipients() 62 throws MessagingException { 63 setSender(new MailAddress("t@james.apache.org")); 64 setRegex("^\\."); 65 66 setupMockedMail(); 67 setupMatcher(); 68 69 Collection matchedRecipients = matcher.match(mockedMail); 70 71 assertNull(matchedRecipients); 72 } 73 74 public void testThrowExceptionWithoutPattern() throws MessagingException { 76 boolean exceptionCatched = false; 77 setSender(new MailAddress("t@james.apache.org")); 78 setRegex(""); 79 80 setupMockedMail(); 81 82 try { 83 setupMatcher(); 84 } catch (MessagingException m) { 85 exceptionCatched = true; 86 } 87 assertTrue(exceptionCatched); 88 } 89 90 public void testThrowExceptionWithInvalidPattern() 92 throws MessagingException { 93 boolean exceptionCatched = false; 94 setSender(new MailAddress("t@james.apache.org")); 95 setRegex("(."); 96 97 setupMockedMail(); 98 99 try { 100 setupMatcher(); 101 } catch (MessagingException m) { 102 exceptionCatched = true; 103 } 104 assertTrue(exceptionCatched); 105 } 106 107 protected Matcher createMatcher() { 108 return new SenderIsRegex(); 109 } 110 111 protected String getConfigOption() { 112 return "SenderIsRegex="; 113 } 114 115 protected String getConfigValue() { 116 return regex; 117 } 118 } 119 | Popular Tags |