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 RecipientIsRegexTest extends AbstractRecipientIsTest { 32 33 private String regex = ".*"; 34 35 public RecipientIsRegexTest(String arg0) 36 throws UnsupportedEncodingException { 37 super(arg0); 38 } 39 40 private void setRegex(String regex) { 41 this.regex = regex; 42 } 43 44 public void testRegexIsMatchedAllRecipients() throws MessagingException { 46 setRecipients(new MailAddress[] { new MailAddress( 47 "test@james.apache.org") }); 48 setRegex(".*@.*"); 49 50 setupAll(); 51 52 Collection matchedRecipients = matcher.match(mockedMail); 53 54 assertNotNull(matchedRecipients); 55 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 56 .size()); 57 } 58 59 public void testRegexIsMatchedOneRecipient() throws MessagingException { 61 setRecipients(new MailAddress[] { 62 new MailAddress("test@james.apache.org"), 63 new MailAddress("test2@james.apache.org") }); 64 setRegex("^test@.*"); 65 66 setupAll(); 67 68 Collection matchedRecipients = matcher.match(mockedMail); 69 70 assertNotNull(matchedRecipients); 71 assertEquals(matchedRecipients.size(), 1); 72 } 73 74 public void testRegexIsNotMatch() throws MessagingException { 76 setRecipients(new MailAddress[] { 77 new MailAddress("test@james2.apache.org"), 78 new MailAddress("test2@james2.apache.org") }); 79 setRegex(".*\\+"); 80 81 setupAll(); 82 83 Collection matchedRecipients = matcher.match(mockedMail); 84 85 assertEquals(matchedRecipients.size(), 0); 86 } 87 88 public void testRegexIsNotMatchedCauseError() throws MessagingException { 90 Collection matchedRecipients = null; 91 String invalidRegex = "(!("; 92 String regexException = null; 93 String exception = "Malformed pattern: " + invalidRegex; 94 95 setRecipients(new MailAddress[] { 96 new MailAddress("test@james2.apache.org"), 97 new MailAddress("test2@james2.apache.org") }); 98 setRegex(invalidRegex); 99 100 try { 101 setupAll(); 102 matchedRecipients = matcher.match(mockedMail); 103 } catch (MessagingException m) { 104 regexException = m.getMessage(); 105 } 106 107 assertNull(matchedRecipients); 108 assertEquals(regexException, exception); 109 110 } 111 112 public void testThrowExceptionWithEmptyPattern() throws MessagingException { 114 boolean catchException = false; 115 116 setRecipients(new MailAddress[] { 117 new MailAddress("test@james2.apache.org"), 118 new MailAddress("test2@james2.apache.org") }); 119 setRegex(""); 120 121 try { 122 setupAll(); 123 } catch (MessagingException m) { 124 catchException = true; 125 } 126 assertTrue(catchException); 127 128 } 129 130 protected String getRecipientName() { 131 return regex; 132 } 133 134 protected Matcher createMatcher() { 135 return new RecipientIsRegex(); 136 } 137 } 138 | Popular Tags |