1 19 20 21 package org.apache.james.transport.mailets; 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.MockMailetConfig; 27 import org.apache.james.test.util.Util; 28 import org.apache.mailet.Mailet; 29 30 import javax.mail.MessagingException ; 31 import javax.mail.internet.MimeMessage ; 32 import javax.mail.internet.ParseException ; 33 34 import java.io.UnsupportedEncodingException ; 35 36 public class MailAttributesToMimeHeadersTest extends TestCase { 37 38 private Mailet mailet; 39 40 private final String HEADER_NAME1 = "JUNIT"; 41 42 private final String HEADER_NAME2 = "JUNIT2"; 43 44 private final String MAIL_ATTRIBUTE_VALUE1 = "test1"; 45 46 private final String MAIL_ATTRIBUTE_VALUE2 = "test2"; 47 48 private final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test"; 49 50 private final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test2"; 51 52 private String config1 = MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1; 53 54 private String config2 = MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2; 55 56 public MailAttributesToMimeHeadersTest(String arg0) 57 throws UnsupportedEncodingException { 58 super(arg0); 59 } 60 61 private void setConfig1(String config1) { 62 this.config1 = config1; 63 } 64 65 private String getConfig1() { 66 return config1; 67 } 68 69 private void setupMailet() throws MessagingException { 70 mailet = new MailAttributesToMimeHeaders(); 71 MockMailetConfig mci = new MockMailetConfig("Test", 72 new MockMailContext()); 73 mci.setProperty("simplemapping", getConfig1()); 74 mci.setProperty("simplemapping", config2); 75 mailet.init(mci); 76 } 77 78 private MockMail setupMail(MimeMessage m) throws ParseException { 79 MockMail mockedMail = Util.createMockMail2Recipients(m); 80 mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1); 81 mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2); 82 return mockedMail; 83 } 84 85 public void testHeadersArePresent() throws MessagingException { 87 MockMail mockedMail = setupMail(Util.createMimeMessage()); 88 setupMailet(); 89 90 mailet.service(mockedMail); 91 92 assertEquals(MAIL_ATTRIBUTE_VALUE1, mockedMail.getMessage().getHeader( 93 HEADER_NAME1)[0]); 94 95 assertEquals(MAIL_ATTRIBUTE_VALUE2, mockedMail.getMessage().getHeader( 96 HEADER_NAME2)[0]); 97 98 } 99 100 public void testInvalidConfig() throws MessagingException { 102 boolean exception = false; 103 MockMail mockedMail = setupMail(Util.createMimeMessage()); 104 setConfig1("test"); 105 106 try { 107 setupMailet(); 108 mailet.service(mockedMail); 109 } catch (MessagingException e) { 110 exception = true; 111 } 112 113 assertTrue(exception); 114 115 } 116 } 117 | Popular Tags |