1 19 20 21 package org.apache.james.transport.mailets; 22 23 import junit.framework.TestCase; 24 import org.apache.james.test.mock.mailet.MockMailContext; 25 import org.apache.james.test.mock.mailet.MockMailetConfig; 26 import org.apache.james.test.util.Util; 27 import org.apache.mailet.Mail; 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 import java.io.UnsupportedEncodingException ; 34 35 public class AddSubjectPrefixTest extends TestCase { 36 37 private MimeMessage mockedMimeMessage; 38 39 private Mail mockedMail; 40 41 private Mailet mailet; 42 43 private final String SUBJECT_PREFIX = "JUNIT"; 44 45 private String subject = null; 46 47 public AddSubjectPrefixTest(String arg0) 48 throws UnsupportedEncodingException { 49 super(arg0); 50 } 51 52 private void setSubject(String subject) { 53 this.subject = subject; 54 } 55 56 private void setupMockedMimeMessage() throws MessagingException { 57 mockedMimeMessage = Util.createMimeMessageWithSubject(subject); 58 } 59 60 private void setupMockedMail(MimeMessage m) throws ParseException { 61 mockedMail = Util.createMockMail2Recipients(m); 62 } 63 64 private void setupMailet() throws MessagingException { 65 setupMockedMimeMessage(); 66 mailet = new AddSubjectPrefix(); 67 MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext()); 68 mci.setProperty("subjectPrefix", SUBJECT_PREFIX); 69 70 mailet.init(mci); 71 } 72 73 private void setupInvalidMailet() throws MessagingException { 74 setupMockedMimeMessage(); 75 mailet = new AddSubjectPrefix(); 76 MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext()); 77 mci.setProperty("subjectPrefix", ""); 78 79 mailet.init(mci); 80 } 81 82 public void testSubjectPrefixWasAdded() throws MessagingException { 84 setSubject("test"); 85 setupMockedMimeMessage(); 86 setupMockedMail(mockedMimeMessage); 87 setupMailet(); 88 89 mailet.service(mockedMail); 90 91 assertEquals(SUBJECT_PREFIX + " " + subject, mockedMail.getMessage().getSubject()); 92 93 } 94 95 public void testSubjectPrefixWasAddedWithoutSubject() 97 throws MessagingException { 98 setupMockedMimeMessage(); 99 setupMockedMail(mockedMimeMessage); 100 setupMailet(); 101 102 mailet.service(mockedMail); 103 104 assertEquals(SUBJECT_PREFIX, mockedMail.getMessage().getSubject()); 105 106 } 107 108 public void testThrowException() throws MessagingException { 110 boolean exceptionThrown = false; 111 setupMockedMimeMessage(); 112 setupMockedMail(mockedMimeMessage); 113 114 try { 115 setupInvalidMailet(); 116 } catch (MessagingException m) { 117 exceptionThrown = true; 118 } 119 assertTrue(exceptionThrown); 120 121 } 122 } 123 | Popular Tags |