1 19 20 21 22 package org.apache.james.transport.mailets; 23 24 import javax.mail.MessagingException ; 25 import javax.mail.internet.MimeMessage ; 26 27 import org.apache.james.test.mock.mailet.MockMail; 28 import org.apache.james.test.mock.mailet.MockMailContext; 29 import org.apache.james.test.mock.mailet.MockMailetConfig; 30 import org.apache.james.test.util.Util; 31 import org.apache.mailet.GenericMailet; 32 import org.apache.mailet.Mail; 33 34 import junit.framework.TestCase; 35 36 public class RemoveMimeHeaderTest extends TestCase { 37 38 private final static String HEADER1 = "header1"; 39 40 private final static String HEADER2 = "header2"; 41 42 private GenericMailet setupMockedMailet(String name1,String name2) throws MessagingException { 43 GenericMailet mailet = new RemoveMimeHeader(); 44 MockMailetConfig mci = new MockMailetConfig("Test", new MockMailContext()); 45 if (name1 != null) mci.setProperty("name", name1); 46 if (name2 != null) mci.setProperty("name", name2); 47 48 mailet.init(mci); 49 return mailet; 50 } 51 52 private MimeMessage getMockedMimeMessage() throws MessagingException { 53 MimeMessage mockedMimeMessage = Util.createMimeMessage(); 54 mockedMimeMessage.setHeader(HEADER1, "true"); 55 mockedMimeMessage.setHeader(HEADER2, "true"); 56 mockedMimeMessage.saveChanges(); 57 return mockedMimeMessage; 58 } 59 60 private Mail getMockedMail(MimeMessage message) { 61 Mail m = new MockMail(); 62 m.setMessage(message); 63 return m; 64 } 65 66 public void testOneHeaderRemoved() throws MessagingException { 67 GenericMailet mailet = setupMockedMailet(HEADER1,null); 68 Mail mail = getMockedMail(getMockedMimeMessage()); 69 70 assertNotNull("Header present",mail.getMessage().getHeader(HEADER1)); 72 assertNotNull("Header present",mail.getMessage().getHeader(HEADER2)); 73 74 mailet.service(mail); 75 76 assertNull("Header removed",mail.getMessage().getHeader(HEADER1)); 78 assertNotNull("Header present",mail.getMessage().getHeader(HEADER2)); 79 } 80 81 public void testTwoHeaderRemoved() throws MessagingException { 82 GenericMailet mailet = setupMockedMailet(HEADER1,HEADER2); 83 Mail mail = getMockedMail(getMockedMimeMessage()); 84 85 assertNotNull("Header present",mail.getMessage().getHeader(HEADER1)); 87 assertNotNull("Header present",mail.getMessage().getHeader(HEADER2)); 88 89 mailet.service(mail); 90 91 assertNull("Header removed",mail.getMessage().getHeader(HEADER1)); 93 assertNull("Header removed",mail.getMessage().getHeader(HEADER2)); 94 } 95 96 public void testNoHeaderRemoved() throws MessagingException { 97 GenericMailet mailet = setupMockedMailet("h1","h2"); 98 Mail mail = getMockedMail(getMockedMimeMessage()); 99 100 assertNotNull("Header present",mail.getMessage().getHeader(HEADER1)); 102 assertNotNull("Header present",mail.getMessage().getHeader(HEADER2)); 103 104 mailet.service(mail); 105 106 assertNotNull("Header present",mail.getMessage().getHeader(HEADER1)); 108 assertNotNull("header present", mail.getMessage().getHeader(HEADER2)); 109 } 110 111 public void testInvalidConfig() throws MessagingException { 112 boolean exception = false; 113 try { 114 setupMockedMailet(null,null); 115 } catch (MessagingException e) { 116 exception = true; 117 } 118 assertTrue("Exception thrown",exception); 119 } 120 121 122 } 123 | Popular Tags |