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 java.io.UnsupportedEncodingException ; 33 import java.util.Collection ; 34 35 public class HasHeaderTest extends TestCase { 36 37 private MimeMessage mockedMimeMessage; 38 39 private MockMail mockedMail; 40 41 private Matcher matcher; 42 43 private final String HEADER_NAME = "JUNIT"; 44 45 private final String HEADER_VALUE = "test-value"; 46 47 private String headerName = "defaultHeaderName"; 48 49 private String headerValue = "defaultHeaderValue"; 50 51 public HasHeaderTest(String arg0) throws UnsupportedEncodingException { 52 super(arg0); 53 } 54 55 private void setHeaderName(String headerName) { 56 this.headerName = headerName; 57 } 58 59 private void setHeaderValue(String headerValue) { 60 this.headerValue = headerValue; 61 } 62 63 private void setupMockedMimeMessage() throws MessagingException { 64 mockedMimeMessage = Util.createMimeMessage(headerName, headerValue); 65 } 66 67 private void setupMatcher() throws MessagingException { 68 setupMockedMimeMessage(); 69 matcher = new HasHeader(); 70 MockMatcherConfig mci = new MockMatcherConfig("HasHeader=" 71 + HEADER_NAME, new MockMailContext()); 72 matcher.init(mci); 73 } 74 75 public void testHeaderIsMatched() throws MessagingException { 77 setHeaderName(HEADER_NAME); 78 setHeaderValue(HEADER_VALUE); 79 80 setupMockedMimeMessage(); 81 mockedMail = Util.createMockMail2Recipients(mockedMimeMessage); 82 setupMatcher(); 83 84 Collection matchedRecipients = matcher.match(mockedMail); 85 86 assertNotNull(matchedRecipients); 87 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 88 .size()); 89 } 90 91 public void testHeaderIsNotMatched() throws MessagingException { 93 setupMockedMimeMessage(); 94 mockedMail = Util.createMockMail2Recipients(mockedMimeMessage); 95 setupMatcher(); 96 97 Collection matchedRecipients = matcher.match(mockedMail); 98 99 assertNull(matchedRecipients); 100 } 101 } 102 | Popular Tags |