1 19 20 21 package org.apache.james.transport.matchers; 22 23 import junit.framework.TestCase; 24 import org.apache.james.test.mock.mailet.MockMail; 25 import org.apache.james.test.mock.mailet.MockMailContext; 26 import org.apache.james.test.mock.mailet.MockMatcherConfig; 27 import org.apache.james.test.util.Util; 28 import org.apache.mailet.Matcher; 29 30 import javax.mail.MessagingException ; 31 import javax.mail.internet.MimeMessage ; 32 import javax.mail.internet.ParseException ; 33 import java.io.UnsupportedEncodingException ; 34 import java.util.Collection ; 35 36 public class FetchedFromTest extends TestCase { 37 38 private MimeMessage mockedMimeMessage; 39 40 private MockMail mockedMail; 41 42 private Matcher matcher; 43 44 private final String HEADER_NAME = "X-fetched-from"; 45 46 private final String HEADER_VALUE = "james-user"; 47 48 private String headerName = "defaultHeaderName"; 49 50 private String headerValue = "defaultHeaderValue"; 51 52 public FetchedFromTest(String arg0) throws UnsupportedEncodingException { 53 super(arg0); 54 } 55 56 private void setHeaderName(String headerName) { 57 this.headerName = headerName; 58 } 59 60 private void setHeaderValue(String headerValue) { 61 this.headerValue = headerValue; 62 } 63 64 private void setupMockedMimeMessage() throws MessagingException { 65 mockedMimeMessage = Util.createMimeMessage(headerName, headerValue); 66 } 67 68 private void setupMockedMail(MimeMessage m) throws ParseException { 69 mockedMail = Util.createMockMail2Recipients(m); 70 } 71 72 private void setupMatcher() throws MessagingException { 73 setupMockedMimeMessage(); 74 matcher = new FetchedFrom(); 75 MockMatcherConfig mci = new MockMatcherConfig("FetchedFrom=" 76 + HEADER_VALUE, new MockMailContext()); 77 matcher.init(mci); 78 } 79 80 public void testHeaderIsMatched() throws MessagingException { 82 setHeaderName(HEADER_NAME); 83 setHeaderValue(HEADER_VALUE); 84 85 setupMockedMimeMessage(); 86 setupMockedMail(mockedMimeMessage); 87 setupMatcher(); 88 89 Collection matchedRecipients = matcher.match(mockedMail); 90 assertNotNull(matchedRecipients); 91 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 92 .size()); 93 } 94 95 public void testHeaderIsNotMatched() throws MessagingException { 97 setHeaderName(HEADER_NAME); 98 setHeaderValue(headerValue); 99 100 setupMockedMimeMessage(); 101 setupMockedMail(mockedMimeMessage); 102 setupMatcher(); 103 104 Collection matchedRecipients = matcher.match(mockedMail); 105 assertNull(matchedRecipients); 106 } 107 108 public void testHeaderWasRemovedAfterMatched() throws MessagingException { 110 setHeaderName(HEADER_NAME); 111 setHeaderValue(HEADER_VALUE); 112 113 setupMockedMimeMessage(); 114 setupMockedMail(mockedMimeMessage); 115 setupMatcher(); 116 117 Collection matchedRecipients = matcher.match(mockedMail); 118 Collection matchedRecipients2 = matcher.match(mockedMail); 119 120 assertNotNull(matchedRecipients); 121 assertNull(matchedRecipients2); 122 } 123 } 124 | Popular Tags |