1 19 20 21 package org.apache.james.smtpserver; 22 23 import junit.framework.TestCase; 24 import org.apache.avalon.framework.container.ContainerUtil; 25 import org.apache.james.smtpserver.core.SetMimeHeaderHandler; 26 import org.apache.james.test.mock.avalon.MockLogger; 27 import org.apache.james.test.util.Util; 28 import org.apache.mailet.Mail; 29 30 import javax.mail.MessagingException ; 31 import javax.mail.internet.MimeMessage ; 32 33 public class SetMimeHeaderHandlerTest extends TestCase { 34 35 private SMTPSession mockedSMTPSession; 36 37 private MimeMessage mockedMimeMessage; 38 39 private Mail mockedMail; 40 41 private final String HEADER_NAME = "JUNIT"; 42 43 private final String HEADER_VALUE = "test-value"; 44 45 private String headerName = "defaultHeaderName"; 46 47 private String headerValue = "defaultHeaderValue"; 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 setupMockedSMTPSession(); 52 } 53 54 private void setHeaderName(String headerName) { 55 this.headerName = headerName; 56 } 57 58 private void setHeaderValue(String headerValue) { 59 this.headerValue = headerValue; 60 } 61 62 private void setupMockedMimeMessage() throws MessagingException { 63 mockedMimeMessage = Util.createMimeMessage(headerName, headerValue); 64 } 65 66 private void setupMockedSMTPSession() { 67 mockedSMTPSession = new AbstractSMTPSession() { 68 69 public Mail getMail() { 70 return mockedMail; 71 } 72 73 public int getRcptCount() { 74 return 0; 75 } 76 77 }; 78 } 79 80 public void testHeaderIsPresent() throws MessagingException { 82 setHeaderName(HEADER_NAME); 83 setHeaderValue(HEADER_VALUE); 84 85 setupMockedMimeMessage(); 86 mockedMail = Util.createMockMail2Recipients(mockedMimeMessage); 87 88 SetMimeHeaderHandler header = new SetMimeHeaderHandler(); 89 90 ContainerUtil.enableLogging(header, new MockLogger()); 91 92 header.setHeaderName(HEADER_NAME); 93 header.setHeaderValue(HEADER_VALUE); 94 header.onMessage(mockedSMTPSession); 95 96 assertEquals(HEADER_VALUE, mockedSMTPSession.getMail().getMessage() 97 .getHeader(HEADER_NAME)[0]); 98 } 99 100 public void testHeaderIsReplaced() throws MessagingException { 102 setHeaderName(HEADER_NAME); 103 setHeaderValue(headerValue); 104 105 setupMockedMimeMessage(); 106 mockedMail = Util.createMockMail2Recipients(mockedMimeMessage); 107 108 SetMimeHeaderHandler header = new SetMimeHeaderHandler(); 109 110 ContainerUtil.enableLogging(header, new MockLogger()); 111 112 header.setHeaderName(HEADER_NAME); 113 header.setHeaderValue(HEADER_VALUE); 114 header.onMessage(mockedSMTPSession); 115 116 assertEquals(HEADER_VALUE, mockedSMTPSession.getMail().getMessage() 117 .getHeader(HEADER_NAME)[0]); 118 } 119 120 } 121 | Popular Tags |