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 import javax.mail.internet.ParseException ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.james.test.mock.mailet.MockMail; 32 import org.apache.james.test.mock.mailet.MockMailContext; 33 import org.apache.james.test.mock.mailet.MockMatcherConfig; 34 import org.apache.james.test.util.Util; 35 import org.apache.mailet.Matcher; 36 37 public class SMTPIsAuthNetworkTest extends TestCase { 38 39 private MockMail mockedMail; 40 41 private Matcher matcher; 42 43 private boolean isAuthorized = false; 44 45 private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork"; 46 47 public SMTPIsAuthNetworkTest(String arg0) 48 throws UnsupportedEncodingException { 49 super(arg0); 50 } 51 52 private void setIsAuthorized(boolean isAuthorized) { 53 this.isAuthorized = isAuthorized; 54 } 55 56 private void setupMockedMail() throws ParseException { 57 mockedMail = Util.createMockMail2Recipients(null); 58 if (isAuthorized) { 59 mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true"); 60 } 61 } 62 63 private void setupMatcher() throws MessagingException { 64 matcher = new SMTPIsAuthNetwork(); 65 MockMatcherConfig mci = new MockMatcherConfig("SMTPIsAuthNetwork", 66 new MockMailContext()); 67 matcher.init(mci); 68 } 69 70 public void testIsAuthNetwork() throws MessagingException { 71 setIsAuthorized(true); 72 setupMockedMail(); 73 setupMatcher(); 74 75 Collection matchedRecipients = matcher.match(mockedMail); 76 77 assertNotNull(matchedRecipients); 78 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 79 .size()); 80 } 81 82 public void testIsNotAuthNetwork() throws MessagingException { 83 setIsAuthorized(false); 84 setupMockedMail(); 85 setupMatcher(); 86 87 Collection matchedRecipients = matcher.match(mockedMail); 88 89 assertNull(matchedRecipients); 90 } 91 } 92 | Popular Tags |