1 19 20 21 package org.apache.james.transport.mailets; 22 23 import org.apache.james.test.mock.mailet.MockMail; 24 import org.apache.james.test.mock.mailet.MockMailContext; 25 import org.apache.james.test.mock.mailet.MockMailetConfig; 26 import org.apache.mailet.Mail; 27 import org.apache.mailet.MailAddress; 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 import java.util.Arrays ; 36 37 import junit.framework.TestCase; 38 39 public class ToProcessorTest extends TestCase { 40 41 private MimeMessage mockedMimeMessage; 42 43 private Mail mockedMail; 44 45 private Mailet mailet; 46 47 private String processor = null; 48 49 private String notice = null; 50 51 public ToProcessorTest(String arg0) throws UnsupportedEncodingException { 52 super(arg0); 53 } 54 55 private void setProcessor(String processor) { 56 this.processor = processor; 57 } 58 59 private void setNotice(String notice) { 60 this.notice = notice; 61 } 62 63 private void setupMockedMail(MimeMessage m) throws ParseException { 64 mockedMail = new MockMail(); 65 mockedMail.setMessage(m); 66 mockedMail.setRecipients(Arrays.asList(new MailAddress[] { 67 new MailAddress("test@james.apache.org"), 68 new MailAddress("test2@james.apache.org") })); 69 70 } 71 72 private void setupMailet() throws MessagingException { 73 mailet = new ToProcessor(); 74 MockMailetConfig mci = new MockMailetConfig("Test", 75 new MockMailContext()); 76 if (processor != null) { 77 mci.setProperty("processor", processor); 78 } 79 if (notice != null) { 80 mci.setProperty("notice", notice); 81 } 82 mailet.init(mci); 83 } 84 85 public void testValidToProcessor() throws MessagingException { 87 setProcessor("error"); 88 setNotice("error in message"); 89 setupMockedMail(mockedMimeMessage); 90 setupMailet(); 91 92 mailet.service(mockedMail); 93 94 assertEquals(processor, mockedMail.getState()); 95 assertEquals(notice, mockedMail.getErrorMessage()); 96 97 } 98 99 public void testExceptionThrown() throws MessagingException { 101 boolean exceptionThrown = false; 102 setProcessor(null); 103 setNotice("error in message"); 104 setupMockedMail(mockedMimeMessage); 105 106 try { 107 setupMailet(); 108 mailet.service(mockedMail); 109 } catch (MessagingException m) { 110 exceptionThrown = true; 111 } 112 assertTrue(exceptionThrown); 113 } 114 115 } 116 | Popular Tags |