KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MimeMessageTest


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.core;
21
22 import org.apache.avalon.framework.container.ContainerUtil;
23 import org.apache.mailet.RFC2822Headers;
24
25 import javax.mail.BodyPart JavaDoc;
26 import javax.mail.Session JavaDoc;
27 import javax.mail.internet.InternetHeaders JavaDoc;
28 import javax.mail.internet.MimeBodyPart JavaDoc;
29 import javax.mail.internet.MimeMessage JavaDoc;
30 import javax.mail.internet.MimeMultipart JavaDoc;
31
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 import junit.framework.TestCase;
39
40 /**
41  * Test the subject folding issue.
42  */

43 public class MimeMessageTest extends TestCase {
44
45     protected MimeMessage JavaDoc getSimpleMessage() throws Exception JavaDoc {
46         MimeMessage JavaDoc mmCreated = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()));
47         mmCreated.setSubject("test");
48         mmCreated.setText("test body");
49         mmCreated.saveChanges();
50         return mmCreated;
51     }
52     
53     protected String JavaDoc getSimpleMessageCleanedSource() throws Exception JavaDoc {
54         return "Subject: test\r\n"
55             +"MIME-Version: 1.0\r\n"
56             +"Content-Type: text/plain; charset=us-ascii\r\n"
57             +"Content-Transfer-Encoding: 7bit\r\n"
58             +"\r\n"
59             +"test body";
60     }
61     
62
63     protected MimeMessage JavaDoc getMessageWithBadReturnPath() throws Exception JavaDoc {
64         MimeMessage JavaDoc mmCreated = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()));
65         mmCreated.setSubject("test");
66         mmCreated.setHeader(RFC2822Headers.RETURN_PATH, "<mybadreturn@example.com>");
67         mmCreated.setText("test body");
68         mmCreated.saveChanges();
69         return mmCreated;
70     }
71     
72     protected String JavaDoc getMessageWithBadReturnPathSource() throws Exception JavaDoc {
73         return "Subject: test\r\n"
74             +"Return-Path: <mybadreturn@example.com>\r\n"
75             +"MIME-Version: 1.0\r\n"
76             +"Content-Type: text/plain; charset=us-ascii\r\n"
77             +"Content-Transfer-Encoding: 7bit\r\n"
78             +"\r\n"
79             +"test body";
80     }
81     
82     protected String JavaDoc getSimpleMessageCleanedSourceHeaderExpected() throws Exception JavaDoc {
83         return "X-Test: foo\r\n"+getSimpleMessageCleanedSource();
84     }
85     
86     /*
87      * Class under test for String getSubject()
88      */

89     public void testSimpleMessage() throws Exception JavaDoc {
90         MimeMessage JavaDoc m = getSimpleMessage();
91         assertEquals(getSimpleMessageCleanedSource(), getCleanedMessageSource(m));
92         ContainerUtil.dispose(m);
93     }
94     
95     
96     protected MimeMessage JavaDoc getMultipartMessage() throws Exception JavaDoc {
97         MimeMessage JavaDoc mmCreated = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()));
98         mmCreated.setSubject("test");
99         MimeMultipart JavaDoc mm = new MimeMultipart JavaDoc("alternative");
100         mm.addBodyPart(new MimeBodyPart JavaDoc(new InternetHeaders JavaDoc(new ByteArrayInputStream JavaDoc("X-header: test1\r\nContent-Type: text/plain; charset=Cp1252\r\n".getBytes())),"first part \u00F2\u00E0\u00F9".getBytes()));
101         mm.addBodyPart(new MimeBodyPart JavaDoc(new InternetHeaders JavaDoc(new ByteArrayInputStream JavaDoc("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
102         mmCreated.setContent(mm);
103         mmCreated.saveChanges();
104         return mmCreated;
105     }
106     
107     protected String JavaDoc getMultipartMessageSource() {
108         return "Subject: test\r\n"
109             +"MIME-Version: 1.0\r\n"
110             +"Content-Type: multipart/alternative; \r\n"
111             +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
112             +"\r\n"
113             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
114             +"X-header: test1\r\n"
115             +"Content-Type: text/plain; charset=Cp1252\r\n"
116             +"Content-Transfer-Encoding: quoted-printable\r\n"
117             +"\r\n"
118             +"first part =E8\r\n"
119             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
120             +"X-header: test2\r\n"
121             +"Content-Type: text/plain; charset=Cp1252\r\n"
122             +"Content-Transfer-Encoding: quoted-printable\r\n"
123             +"\r\n"
124             +"second part =E8=E8\r\n"
125             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
126     }
127     
128     protected String JavaDoc getMultipartMessageExpected1() {
129         return "Subject: test\r\n"
130             +"MIME-Version: 1.0\r\n"
131             +"Content-Type: multipart/alternative; \r\n"
132             +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
133             +"\r\n"
134             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
135             +"X-header: test1\r\n"
136             +"Content-Type: text/plain; charset=Cp1252\r\n"
137             +"Content-Transfer-Encoding: quoted-printable\r\n"
138             +"\r\n"
139             +"test=80\r\n"
140             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
141             +"X-header: test2\r\n"
142             +"Content-Type: text/plain; charset=Cp1252\r\n"
143             +"Content-Transfer-Encoding: quoted-printable\r\n"
144             +"\r\n"
145             +"second part =E8=E8\r\n"
146             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
147     }
148     
149     protected String JavaDoc getMultipartMessageExpected2() {
150         return "Subject: test\r\n"
151             +"MIME-Version: 1.0\r\n"
152             +"Content-Type: multipart/alternative; \r\n"
153             +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
154             +"\r\n"
155             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
156             +"X-header: test1\r\n"
157             +"Content-Type: text/plain; charset=Cp1252\r\n"
158             +"Content-Transfer-Encoding: quoted-printable\r\n"
159             +"\r\n"
160             +"test=80\r\n"
161             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
162             +"X-header: test2\r\n"
163             +"Content-Type: text/plain; charset=Cp1252\r\n"
164             +"Content-Transfer-Encoding: quoted-printable\r\n"
165             +"\r\n"
166             +"second part =E8=E8\r\n"
167             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
168             +"Subject: test3\r\n"
169             +"Content-Transfer-Encoding: 7bit\r\n"
170             +"Content-Type: text/plain; charset=us-ascii\r\n"
171             +"\r\n"
172             +"second part\r\n"
173             +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
174     }
175     
176     protected String JavaDoc getMultipartMessageExpected3() {
177         return "Subject: test\r\n"
178             +"MIME-Version: 1.0\r\n"
179             +"Content-Type: binary/octet-stream\r\n"
180             +"Content-Transfer-Encoding: quoted-printable\r\n"
181             +"\r\n"
182             +"mynewco=F2=E0=F9ntent=80=E0!";
183     }
184     
185     /*
186      * Class under test for String getSubject()
187      */

188     public void testMultipartMessageChanges() throws Exception JavaDoc {
189
190         MimeMessage JavaDoc mm = getMultipartMessage();
191         
192 // ByteArrayOutputStream out = new ByteArrayOutputStream();
193
// mmCreated.writeTo(out,new String[] {"Message-ID"});
194
// String messageSource = out.toString();
195
// System.out.println(messageSource);
196

197         
198         MimeMultipart JavaDoc content1 = (MimeMultipart JavaDoc) mm.getContent();
199         BodyPart JavaDoc b1 = content1.getBodyPart(0);
200         b1.setContent("test\u20AC","text/plain; charset=Cp1252");
201         mm.setContent(content1,mm.getContentType());
202         //.setHeader(RFC2822Headers.CONTENT_TYPE,contentType);
203
mm.saveChanges();
204
205         assertEquals(getMultipartMessageExpected1(),getCleanedMessageSource(mm));
206
207         MimeMultipart JavaDoc content2 = (MimeMultipart JavaDoc) mm.getContent();
208         content2.addBodyPart(new MimeBodyPart JavaDoc(new InternetHeaders JavaDoc(new ByteArrayInputStream JavaDoc("Subject: test3\r\n".getBytes())),"second part".getBytes()));
209         mm.setContent(content2,mm.getContentType());
210         mm.saveChanges();
211
212         assertEquals(getMultipartMessageExpected2(),getCleanedMessageSource(mm));
213
214         mm.setContent("mynewco\u00F2\u00E0\u00F9ntent\u20AC\u00E0!","text/plain; charset=cp1252");
215         mm.setHeader(RFC2822Headers.CONTENT_TYPE,"binary/octet-stream");
216         //mm.setHeader("Content-Transfer-Encoding","8bit");
217
mm.saveChanges();
218         
219         assertEquals(getMultipartMessageExpected3(),getCleanedMessageSource(mm));
220         
221         ContainerUtil.dispose(mm);
222         
223     }
224
225     protected MimeMessage JavaDoc getMissingEncodingAddHeaderMessage() throws Exception JavaDoc {
226         MimeMessage JavaDoc m = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()));
227         m.setText("Test\u00E0\r\n");
228         m.setSubject("test");
229         m.saveChanges();
230         return m;
231     }
232     
233
234     protected String JavaDoc getMissingEncodingAddHeaderSource() {
235         return "Subject: test\r\n"+
236                 "\r\n"+
237                 "Test\u00E0\r\n";
238     }
239     
240     protected String JavaDoc getMissingEncodingAddHeaderExpected() {
241         return "Subject: test\r\n"
242             +"MIME-Version: 1.0\r\n"
243             +"Content-Type: text/plain; charset=Cp1252\r\n"
244             +"Content-Transfer-Encoding: quoted-printable\r\n"
245             +"\r\n"
246             +"Test=E0\r\n";
247     }
248     
249
250     /**
251      * This test is not usable in different locale environment.
252      */

253     /*
254     public void testMissingEncodingAddHeader() throws Exception {
255         
256         
257         MimeMessage mm = getMissingEncodingAddHeaderMessage();
258         mm.setHeader("Content-Transfer-Encoding", "quoted-printable");
259         mm.saveChanges();
260
261         assertEquals(getMissingEncodingAddHeaderExpected(),getCleanedMessageSource(mm));
262     }
263     */

264     
265
266     protected String JavaDoc getCleanedMessageSource(MimeMessage JavaDoc mm) throws Exception JavaDoc {
267         ByteArrayOutputStream JavaDoc out2;
268         out2 = new ByteArrayOutputStream JavaDoc();
269         mm.writeTo(out2,new String JavaDoc[] {"Message-ID"});
270
271         String JavaDoc res = out2.toString();
272
273         int p = res.indexOf("\r\n\r\n");
274         if (p > 0) {
275             String JavaDoc head = res.substring(0,p);
276             String JavaDoc[] str = head.split("\r\n");
277             Arrays.sort(str);
278             StringBuffer JavaDoc outputHead = new StringBuffer JavaDoc();
279             for (int i = str.length-1; i >= 0; i--) {
280                 outputHead.append(str[i]);
281                 outputHead.append("\r\n");
282             }
283             outputHead.append(res.substring(p+2));
284             res = outputHead.toString();
285         }
286         
287         res = res.replaceAll("----=_Part_\\d_\\d+\\.\\d+","----=_Part_\\0_XXXXXXXXXXX.XXXXXXXXXXX");
288         return res;
289     }
290     
291     protected void debugMessage(MimeMessage JavaDoc mm) throws Exception JavaDoc {
292         System.out.println("-------------------");
293         System.out.println(getCleanedMessageSource(mm));
294         System.out.println("-------------------");
295     }
296     
297
298     protected MimeMessage JavaDoc getMissingEncodingMessage() throws Exception JavaDoc {
299         MimeMessage JavaDoc mmCreated = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()));
300         mmCreated.setSubject("test");
301         MimeMultipart JavaDoc mm = new MimeMultipart JavaDoc("alternative");
302         mm.addBodyPart(new MimeBodyPart JavaDoc(new InternetHeaders JavaDoc(new ByteArrayInputStream JavaDoc("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
303         mmCreated.setContent(mm);
304         mmCreated.saveChanges();
305         return mmCreated;
306     }
307     
308
309     protected String JavaDoc getMissingEncodingMessageSource() {
310         return "Subject: test\r\n"
311         +"MIME-Version: 1.0\r\n"
312         +"Content-Type: multipart/alternative; \r\n"
313         +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
314         +"\r\n"
315         +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
316         +"X-header: test2\r\n"
317         +"Content-Type: text/plain; charset=Cp1252\r\n"
318         +"Content-Transfer-Encoding: quoted-printable\r\n"
319         +"\r\n"
320         +"second part =E8=E8\r\n"
321         +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
322     }
323     
324
325     public void testGetLineCount() throws Exception JavaDoc {
326         MimeMessage JavaDoc mm = getMissingEncodingMessage();
327         try {
328             int count = mm.getLineCount();
329             assertTrue(count == -1 || count == 7);
330         } catch (Exception JavaDoc e) {
331             fail("Unexpected exception in getLineCount");
332         }
333         ContainerUtil.dispose(mm);
334     }
335     
336     /**
337      * This test throw a NullPointerException when the original message was created by
338      * a MimeMessageInputStreamSource.
339      */

340     public void testMessageCloningViaCoW() throws Exception JavaDoc {
341         MimeMessage JavaDoc mmorig = getSimpleMessage();
342         
343         MimeMessage JavaDoc mm = new MimeMessageCopyOnWriteProxy(mmorig);
344
345         MimeMessage JavaDoc mm2 = new MimeMessageCopyOnWriteProxy(mm);
346
347         mm2.setHeader("Subject", "Modified");
348         
349         ContainerUtil.dispose(mm2);
350         System.gc();
351         Thread.sleep(200);
352         //((Disposable)mail_dup.getMessage()).dispose();
353

354         mm.setHeader("Subject", "Modified");
355         
356         ContainerUtil.dispose(mm);
357         ContainerUtil.dispose(mmorig);
358     }
359     
360     /**
361      * This test throw a NullPointerException when the original message was created by
362      * a MimeMessageInputStreamSource.
363      */

364     public void testMessageCloningViaCoW2() throws Exception JavaDoc {
365         MimeMessage JavaDoc mmorig = getSimpleMessage();
366         
367         MimeMessage JavaDoc mm = new MimeMessageCopyOnWriteProxy(mmorig);
368         
369         MimeMessage JavaDoc mm2 = new MimeMessageCopyOnWriteProxy(mm);
370         
371         ContainerUtil.dispose(mm);
372         mm = null;
373         System.gc();
374         Thread.sleep(200);
375
376         try {
377             mm2.writeTo(System.out);
378         } catch (Exception JavaDoc e) {
379             e.printStackTrace();
380             fail("Exception while writing the message to output");
381         }
382         
383         ContainerUtil.dispose(mm2);
384         ContainerUtil.dispose(mmorig);
385     }
386
387     
388     /**
389      * This test throw a NullPointerException when the original message was created by
390      * a MimeMessageInputStreamSource.
391      */

392     public void testMessageCloningViaCoWSubjectLost() throws Exception JavaDoc {
393         MimeMessage JavaDoc mmorig = getSimpleMessage();
394         
395         MimeMessage JavaDoc mm = new MimeMessageCopyOnWriteProxy(mmorig);
396
397         mm.setHeader("X-Test", "foo");
398         mm.saveChanges();
399         
400         assertEquals(getSimpleMessageCleanedSourceHeaderExpected(),getCleanedMessageSource(mm));
401
402         ContainerUtil.dispose(mm);
403         ContainerUtil.dispose(mmorig);
404     }
405     
406     public void testReturnPath() throws Exception JavaDoc {
407         MimeMessage JavaDoc message = getSimpleMessage();
408         assertNull(message.getHeader(RFC2822Headers.RETURN_PATH));
409         ContainerUtil.dispose(message);
410     }
411     
412     public void testHeaderOrder() throws Exception JavaDoc {
413         MimeMessage JavaDoc message = getSimpleMessage();
414         message.setHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
415         Enumeration JavaDoc h = message.getAllHeaderLines();
416         
417         assertEquals(h.nextElement(),"Return-Path: <test@test.de>");
418         ContainerUtil.dispose(message);
419     }
420
421 }
422
Popular Tags