KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > LinearProcessorTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

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 JavaDoc;
44 import javax.mail.internet.MimeMessage JavaDoc;
45 import javax.mail.util.SharedByteArrayInputStream;
46
47 import java.io.IOException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Collection JavaDoc;
50
51 import junit.framework.TestCase;
52
53 /**
54  * contains a proof of JAMES-421.
55  */

56 public class LinearProcessorTest extends TestCase {
57     LinearProcessor linearProcessor;
58
59     MimeMessage JavaDoc mimeMessage = null;
60
61     String JavaDoc content = "Subject: test\r\nContent-Transfer-Encoding: plain";
62
63     String JavaDoc sep = "\r\n\r\n";
64
65     String JavaDoc 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 JavaDoc receivedMails = new ArrayList JavaDoc();
74
75         public void service(Mail mail) throws MessagingException JavaDoc {
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 JavaDoc {
91             try {
92                 MimeMessage JavaDoc message = mail.getMessage();
93                 // Set the header name and value (supplied at init time).
94
String JavaDoc 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 JavaDoc me) {
101                 log(me.getMessage());
102             }
103         }
104     }
105
106     private class DummyMatcherConfig implements MatcherConfig {
107         private String JavaDoc condition;
108
109         public DummyMatcherConfig(String JavaDoc config) {
110             this.condition = config;
111         }
112
113         public String JavaDoc getCondition() {
114             return condition;
115         }
116
117         public MailetContext getMailetContext() {
118             return mockContext;
119         }
120
121         public String JavaDoc getMatcherName() {
122             return "All";
123         }
124     }
125
126     public void testCopyOnWrite() throws IOException JavaDoc, MessagingException JavaDoc {
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 JavaDoc recipients = new ArrayList JavaDoc();
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 JavaDoc a = checkerMailet.receivedMails;
159             assertEquals(2, a.size());
160             Mail mail1 = ((Mail) a.get(0));
161             Mail mail2 = ((Mail) a.get(1));
162             MimeMessage JavaDoc m1 = mail1.getMessage();
163             MimeMessage JavaDoc 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 JavaDoc e) {
171             // TODO Auto-generated catch block
172
e.printStackTrace();
173         }
174     }
175
176     public void testStateChange() throws IOException JavaDoc, MessagingException JavaDoc {
177         linearProcessor.setSpool(new InMemorySpoolRepository() {
178             public void store(Mail mc) throws MessagingException JavaDoc {
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 JavaDoc recipients = new ArrayList JavaDoc();
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 JavaDoc a = checkerMailet.receivedMails;
208             assertEquals(2, a.size());
209             Mail mail1 = ((Mail) a.get(0));
210             Mail mail2 = ((Mail) a.get(1));
211             MimeMessage JavaDoc m1 = mail1.getMessage();
212             MimeMessage JavaDoc 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 JavaDoc e) {
220             // TODO Auto-generated catch block
221
e.printStackTrace();
222         }
223     }
224
225     public void setUp() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
241         if (mimeMessage != null) {
242             ContainerUtil.dispose(mimeMessage);
243         }
244         ContainerUtil.dispose(linearProcessor);
245         super.tearDown();
246     }
247
248 }
249
Popular Tags