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