001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport;
019:
020: import org.apache.avalon.framework.logger.ConsoleLogger;
021: import org.apache.avalon.framework.logger.Logger;
022: import org.apache.james.core.MailImpl;
023: import org.apache.james.core.MimeMessageCopyOnWriteProxy;
024: import org.apache.james.core.MimeMessageInputStreamSource;
025: import org.apache.james.test.mock.james.MockSpoolRepository;
026: import org.apache.james.test.mock.mailet.MockMailContext;
027: import org.apache.james.test.mock.mailet.MockMailetConfig;
028: import org.apache.james.transport.mailets.debug.DumpSystemErr;
029: import org.apache.james.transport.matchers.All;
030: import org.apache.james.transport.matchers.RecipientIs;
031: import org.apache.mailet.GenericMailet;
032: import org.apache.mailet.Mail;
033: import org.apache.mailet.MailAddress;
034: import org.apache.mailet.Mailet;
035: import org.apache.mailet.MailetConfig;
036: import org.apache.mailet.MailetContext;
037: import org.apache.mailet.Matcher;
038: import org.apache.mailet.MatcherConfig;
039:
040: import javax.mail.MessagingException;
041: import javax.mail.internet.MimeMessage;
042: import javax.mail.util.SharedByteArrayInputStream;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Collection;
047:
048: import junit.framework.TestCase;
049:
050: /**
051: * Currently here as a proof of JAMES-421.
052: * Fixing code will follow
053: */
054: public class LinearProcessorTest extends TestCase {
055: Collection c;
056: LinearProcessor t;
057: MimeMessage mw = null;
058: String content = "Subject: test\r\nContent-Transfer-Encoding: plain";
059: String sep = "\r\n\r\n";
060: String body = "original body\r\n.\r\n";
061: MailetContext mockContext = new MockMailContext();
062:
063: public static int counter = 0;
064:
065: private class CheckerMailet extends GenericMailet {
066:
067: public ArrayList receivedMails = new ArrayList();
068:
069: public void service(Mail mail) throws MessagingException {
070: Mail m2 = new MailImpl(mail, mail.getName());
071: m2.setState(mail.getState());
072: receivedMails.add(m2);
073: }
074:
075: }
076:
077: private MailetConfig mockMailetConfig = new MockMailetConfig(
078: "Dummy", mockContext);
079:
080: private CheckerMailet checkerMailet;
081:
082: private class MyMailet extends GenericMailet {
083:
084: public void service(Mail mail) throws MessagingException {
085: try {
086: MimeMessage message = mail.getMessage();
087: //Set the header name and value (supplied at init time).
088: String newText = "new text " + counter++;
089: System.err.println("Setting body to " + newText);
090: message.addHeader("x-Header", newText);
091: message.setText(newText);
092: message.setSubject(newText);
093: message.saveChanges();
094: } catch (javax.mail.MessagingException me) {
095: log(me.getMessage());
096: }
097: }
098: }
099:
100: public LinearProcessorTest(String arg0) throws Exception {
101: super (arg0);
102:
103: MimeMessageInputStreamSource mmis = null;
104: try {
105: mmis = new MimeMessageInputStreamSource("test",
106: new SharedByteArrayInputStream(
107: (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:
121: public String getCondition() {
122: return condition;
123: }
124:
125: public MailetContext getMailetContext() {
126: return mockContext;
127: }
128:
129: public String getMatcherName() {
130: return "All";
131: }
132: }
133:
134: public void testCopyOnWrite() throws IOException,
135: MessagingException {
136: t.setSpool(new MockSpoolRepository());
137: Matcher recipientIs = new RecipientIs();
138: recipientIs.init(new DummyMatcherConfig("rec1@domain.com"));
139:
140: Matcher all = new All();
141: all.init(new DummyMatcherConfig(""));
142:
143: Mailet changeBody = new MyMailet();
144: Mailet changeBody2 = new MyMailet();
145:
146: changeBody.init(mockMailetConfig);
147: changeBody2.init(mockMailetConfig);
148:
149: Mailet dumpSystemErr = new DumpSystemErr();
150: changeBody.init(mockMailetConfig);
151:
152: checkerMailet = new CheckerMailet();
153: t.add(recipientIs, changeBody);
154: t.add(all, changeBody);
155: t.add(all, dumpSystemErr);
156: t.add(all, checkerMailet);
157: t.closeProcessorLists();
158:
159: Collection recipients = new ArrayList();
160: recipients.add(new MailAddress("rec1", "domain.com"));
161: recipients.add(new MailAddress("rec2", "domain.com"));
162: try {
163: MailImpl m = new MailImpl("mail1", new MailAddress(
164: "sender", "domain.com"), recipients, mw);
165: t.service(m);
166: ArrayList a = checkerMailet.receivedMails;
167: assertEquals(2, a.size());
168: MimeMessage m1 = ((Mail) a.get(0)).getMessage();
169: MimeMessage m2 = ((Mail) a.get(1)).getMessage();
170: assertNotSame(m1, m2);
171: assertEquals(m1.getSubject(), "new text 1");
172: assertEquals(m2.getSubject(), "new text 2");
173: m.dispose();
174: } catch (MessagingException e) {
175: // TODO Auto-generated catch block
176: e.printStackTrace();
177: }
178: }
179:
180: public void testStateChange() throws IOException,
181: MessagingException {
182: t.setSpool(new MockSpoolRepository() {
183: public void store(Mail mc) throws MessagingException {
184: assertEquals("MYSTATE", mc.getState());
185: super .store(mc);
186: }
187: });
188:
189: Matcher recipientIs = new RecipientIs();
190: recipientIs.init(new DummyMatcherConfig("rec1@domain.com"));
191:
192: Matcher all = new All();
193: all.init(new DummyMatcherConfig(""));
194:
195: Mailet dumpSystemErr = new DumpSystemErr();
196:
197: checkerMailet = new CheckerMailet();
198: t.add(recipientIs, dumpSystemErr);
199: t.add(all, dumpSystemErr);
200: t.add(all, checkerMailet);
201: t.closeProcessorLists();
202:
203: Collection recipients = new ArrayList();
204: recipients.add(new MailAddress("rec1", "domain.com"));
205: recipients.add(new MailAddress("rec2", "domain.com"));
206: try {
207: MailImpl m = new MailImpl("mail1", new MailAddress(
208: "sender", "domain.com"), recipients, mw);
209: m.setState("MYSTATE");
210: t.service(m);
211: ArrayList a = checkerMailet.receivedMails;
212: assertEquals(2, a.size());
213: MimeMessage m1 = ((Mail) a.get(0)).getMessage();
214: MimeMessage m2 = ((Mail) a.get(1)).getMessage();
215: assertNotSame(m1, m2);
216: assertEquals("MYSTATE", ((Mail) a.get(0)).getState());
217: assertEquals("MYSTATE", ((Mail) a.get(1)).getState());
218: m.dispose();
219: } catch (MessagingException e) {
220: // TODO Auto-generated catch block
221: e.printStackTrace();
222: }
223: }
224:
225: public void setUp() throws Exception {
226: super .setUp();
227: t = new LinearProcessor();
228: Logger l = new ConsoleLogger();
229: t.enableLogging(l);
230: t.initialize();
231:
232: }
233:
234: public void tearDown() throws Exception {
235: t.dispose();
236: super.tearDown();
237: }
238:
239: }
|