1 19 20 package org.apache.james.transport; 21 22 import org.apache.avalon.framework.container.ContainerUtil; 23 import org.apache.avalon.framework.logger.ConsoleLogger; 24 import org.apache.avalon.framework.logger.Logger; 25 import org.apache.james.core.MailImpl; 26 import org.apache.james.core.MimeMessageCopyOnWriteProxy; 27 import org.apache.james.core.MimeMessageInputStreamSource; 28 import org.apache.james.test.mock.james.InMemorySpoolRepository; 29 import org.apache.james.test.mock.mailet.MockMailContext; 30 import org.apache.james.test.mock.mailet.MockMailetConfig; 31 import org.apache.james.transport.mailets.debug.DumpSystemErr; 32 import org.apache.james.transport.matchers.All; 33 import org.apache.james.transport.matchers.RecipientIs; 34 import org.apache.mailet.GenericMailet; 35 import org.apache.mailet.Mail; 36 import org.apache.mailet.MailAddress; 37 import org.apache.mailet.Mailet; 38 import org.apache.mailet.MailetConfig; 39 import org.apache.mailet.MailetContext; 40 import org.apache.mailet.Matcher; 41 import org.apache.mailet.MatcherConfig; 42 43 import javax.mail.MessagingException ; 44 import javax.mail.internet.MimeMessage ; 45 import javax.mail.util.SharedByteArrayInputStream; 46 47 import java.io.IOException ; 48 import java.util.ArrayList ; 49 import java.util.Collection ; 50 51 import junit.framework.TestCase; 52 53 56 public class LinearProcessorTest extends TestCase { 57 LinearProcessor linearProcessor; 58 59 MimeMessage mimeMessage = null; 60 61 String content = "Subject: test\r\nContent-Transfer-Encoding: plain"; 62 63 String sep = "\r\n\r\n"; 64 65 String body = "original body LinearProcessorTest\r\n.\r\n"; 66 67 MailetContext mockContext = new MockMailContext(); 68 69 public static int counter = 0; 70 71 private class CheckerMailet extends GenericMailet { 72 73 public ArrayList receivedMails = new ArrayList (); 74 75 public void service(Mail mail) throws MessagingException { 76 Mail m2 = new MailImpl(mail, mail.getName()); 77 m2.setState(mail.getState()); 78 receivedMails.add(m2); 79 } 80 81 } 82 83 private MailetConfig mockMailetConfig = new MockMailetConfig("Dummy", 84 mockContext); 85 86 private CheckerMailet checkerMailet; 87 88 private class MyMailet extends GenericMailet { 89 90 public void service(Mail mail) throws MessagingException { 91 try { 92 MimeMessage message = mail.getMessage(); 93 String newText = "new text " + counter++; 95 System.err.println("Setting body to " + newText); 96 message.addHeader("x-Header", newText); 97 message.setText(newText); 98 message.setSubject(newText); 99 message.saveChanges(); 100 } catch (javax.mail.MessagingException me) { 101 log(me.getMessage()); 102 } 103 } 104 } 105 106 private class DummyMatcherConfig implements MatcherConfig { 107 private String condition; 108 109 public DummyMatcherConfig(String config) { 110 this.condition = config; 111 } 112 113 public String getCondition() { 114 return condition; 115 } 116 117 public MailetContext getMailetContext() { 118 return mockContext; 119 } 120 121 public String getMatcherName() { 122 return "All"; 123 } 124 } 125 126 public void testCopyOnWrite() throws IOException , MessagingException { 127 linearProcessor.setSpool(new InMemorySpoolRepository()); 128 Matcher recipientIs = new RecipientIs(); 129 recipientIs.init(new DummyMatcherConfig("rec1@domain.com")); 130 131 Matcher all = new All(); 132 all.init(new DummyMatcherConfig("")); 133 134 Mailet changeBody = new MyMailet(); 135 Mailet changeBody2 = new MyMailet(); 136 137 changeBody.init(mockMailetConfig); 138 changeBody2.init(mockMailetConfig); 139 140 Mailet dumpSystemErr = new DumpSystemErr(); 141 changeBody.init(mockMailetConfig); 142 143 checkerMailet = new CheckerMailet(); 144 linearProcessor.openProcessorList(); 145 linearProcessor.add(recipientIs, changeBody); 146 linearProcessor.add(all, changeBody); 147 linearProcessor.add(all, dumpSystemErr); 148 linearProcessor.add(all, checkerMailet); 149 linearProcessor.closeProcessorLists(); 150 151 Collection recipients = new ArrayList (); 152 recipients.add(new MailAddress("rec1", "domain.com")); 153 recipients.add(new MailAddress("rec2", "domain.com")); 154 try { 155 MailImpl m = new MailImpl("mail1", new MailAddress("sender", 156 "domain.com"), recipients, mimeMessage); 157 linearProcessor.service(m); 158 ArrayList a = checkerMailet.receivedMails; 159 assertEquals(2, a.size()); 160 Mail mail1 = ((Mail) a.get(0)); 161 Mail mail2 = ((Mail) a.get(1)); 162 MimeMessage m1 = mail1.getMessage(); 163 MimeMessage m2 = mail2.getMessage(); 164 assertNotSame(m1, m2); 165 assertEquals(m1.getSubject(), "new text 1"); 166 assertEquals(m2.getSubject(), "new text 2"); 167 m.dispose(); 168 ContainerUtil.dispose(mail1); 169 ContainerUtil.dispose(mail2); 170 } catch (MessagingException e) { 171 e.printStackTrace(); 173 } 174 } 175 176 public void testStateChange() throws IOException , MessagingException { 177 linearProcessor.setSpool(new InMemorySpoolRepository() { 178 public void store(Mail mc) throws MessagingException { 179 assertEquals("MYSTATE", mc.getState()); 180 super.store(mc); 181 } 182 }); 183 184 Matcher recipientIs = new RecipientIs(); 185 recipientIs.init(new DummyMatcherConfig("rec1@domain.com")); 186 187 Matcher all = new All(); 188 all.init(new DummyMatcherConfig("")); 189 190 Mailet dumpSystemErr = new DumpSystemErr(); 191 192 checkerMailet = new CheckerMailet(); 193 linearProcessor.openProcessorList(); 194 linearProcessor.add(recipientIs, dumpSystemErr); 195 linearProcessor.add(all, dumpSystemErr); 196 linearProcessor.add(all, checkerMailet); 197 linearProcessor.closeProcessorLists(); 198 199 Collection recipients = new ArrayList (); 200 recipients.add(new MailAddress("rec1", "domain.com")); 201 recipients.add(new MailAddress("rec2", "domain.com")); 202 try { 203 MailImpl m = new MailImpl("mail1", new MailAddress("sender", 204 "domain.com"), recipients, mimeMessage); 205 m.setState("MYSTATE"); 206 linearProcessor.service(m); 207 ArrayList a = checkerMailet.receivedMails; 208 assertEquals(2, a.size()); 209 Mail mail1 = ((Mail) a.get(0)); 210 Mail mail2 = ((Mail) a.get(1)); 211 MimeMessage m1 = mail1.getMessage(); 212 MimeMessage m2 = mail2.getMessage(); 213 assertNotSame(m1, m2); 214 assertEquals("MYSTATE", ((Mail) a.get(0)).getState()); 215 assertEquals("MYSTATE", ((Mail) a.get(1)).getState()); 216 m.dispose(); 217 ContainerUtil.dispose(mail1); 218 ContainerUtil.dispose(mail2); 219 } catch (MessagingException e) { 220 e.printStackTrace(); 222 } 223 } 224 225 public void setUp() throws Exception { 226 super.setUp(); 227 MimeMessageInputStreamSource mmis = null; 228 try { 229 mmis = new MimeMessageInputStreamSource("test", 230 new SharedByteArrayInputStream((content + sep + body) 231 .getBytes())); 232 } catch (MessagingException e) { 233 } 234 mimeMessage = new MimeMessageCopyOnWriteProxy(mmis); 235 linearProcessor = new LinearProcessor(); 236 Logger l = new ConsoleLogger(); 237 ContainerUtil.enableLogging(linearProcessor, l); 238 } 239 240 public void tearDown() throws Exception { 241 if (mimeMessage != null) { 242 ContainerUtil.dispose(mimeMessage); 243 } 244 ContainerUtil.dispose(linearProcessor); 245 super.tearDown(); 246 } 247 248 } 249 | Popular Tags |