KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > composer > MimeTreeRendererTest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.composer;
37
38 import java.io.InputStream JavaDoc;
39 import java.nio.charset.Charset JavaDoc;
40 import java.util.Date JavaDoc;
41
42 import junit.framework.TestCase;
43
44 import org.columba.ristretto.io.CharSequenceSource;
45 import org.columba.ristretto.message.Address;
46 import org.columba.ristretto.message.BasicHeader;
47 import org.columba.ristretto.message.Header;
48 import org.columba.ristretto.message.LocalMimePart;
49 import org.columba.ristretto.message.MimeHeader;
50 import org.columba.ristretto.message.MimeType;
51 import org.columba.ristretto.message.StreamableMimePart;
52
53 public class MimeTreeRendererTest extends TestCase {
54     
55     public void testComposeSimple() {
56         Header header = new Header();
57         String JavaDoc body = "Body";
58         BasicHeader basicHeader = new BasicHeader( header );
59         basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net"));
60         basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )});
61         basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII"));
62         basicHeader.setDate(new Date JavaDoc(0));
63         
64         MimeHeader mimeHeader = new MimeHeader(header);
65         mimeHeader.setMimeType(new MimeType("text", "plain"));
66         mimeHeader.setContentTransferEncoding("7bit");
67         mimeHeader.putContentParameter("charset", "us-ascii");
68         
69         StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body));
70         
71         
72         // Read from stream
73
try {
74             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
75             InputStream JavaDoc messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart);
76             int next = messageStream.read();
77             while( next != -1 ) {
78                 result.append((char) next);
79                 next = messageStream.read();
80             }
81             String JavaDoc message = result.toString();
82             System.out.println( message );
83             
84             assertEquals( "Date: Thu, 1 Jan 1970 01:00:00 +0100\r\nSubject: This is a test mail\r\nTo: \"Timo Stich\" <tstich@users.sourceforge.net>\r\nContent-Transfer-Encoding: 7bit\r\nFrom: \"Timo Stich\" <tstich@users.sourceforge.net>\r\nContent-Type: text/plain; charset=\"us-ascii\"\r\n\r\nBody" , message);
85         } catch (Exception JavaDoc e) {
86             e.printStackTrace();
87         }
88     }
89     
90     public void testComposeMultipartMixed() {
91         Header header = new Header();
92         String JavaDoc body = "Body";
93         BasicHeader basicHeader = new BasicHeader( header );
94         basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net"));
95         basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )});
96         basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII"));
97         basicHeader.setDate(new Date JavaDoc(0));
98         
99         MimeHeader mimeHeader = new MimeHeader(header);
100         mimeHeader.setMimeType(new MimeType("multipart", "mixed"));
101         
102         StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body));
103
104         mimeHeader = new MimeHeader();
105         mimeHeader.setMimeType(new MimeType("text", "plain"));
106         mimeHeader.setContentTransferEncoding("7bit");
107         mimeHeader.putContentParameter("charset", "us-ascii");
108         
109         StreamableMimePart subpart = new LocalMimePart( mimeHeader, new CharSequenceSource(body));
110         mimepart.addChild( subpart );
111         mimepart.addChild( subpart );
112         
113         
114         // Read from stream
115
try {
116             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
117             InputStream JavaDoc messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart);
118             int next = messageStream.read();
119             while( next != -1 ) {
120                 result.append((char) next);
121                 next = messageStream.read();
122             }
123             String JavaDoc message = result.toString();
124             System.out.println( message );
125             
126         } catch (Exception JavaDoc e) {
127             e.printStackTrace();
128         }
129     }
130
131     public void testComposeQPEncoded() {
132         Header header = new Header();
133         String JavaDoc body = "This is a ?est";
134         BasicHeader basicHeader = new BasicHeader( header );
135         basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net"));
136         basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )});
137         basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII"));
138         basicHeader.setDate(new Date JavaDoc(0));
139         
140         MimeHeader mimeHeader = new MimeHeader(header);
141         mimeHeader.setMimeType(new MimeType("text", "plain"));
142         mimeHeader.setContentTransferEncoding("7bit");
143         mimeHeader.putContentParameter("charset", "iso-8859-1");
144         mimeHeader.setContentTransferEncoding("quoted-printable");
145         
146         StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body));
147         
148         
149         // Read from stream
150
try {
151             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
152             InputStream JavaDoc messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart);
153             int next = messageStream.read();
154             while( next != -1 ) {
155                 result.append((char) next);
156                 next = messageStream.read();
157             }
158             String JavaDoc message = result.toString();
159             System.out.println( message );
160             
161         } catch (Exception JavaDoc e) {
162             e.printStackTrace();
163         }
164     }
165
166     
167 }
168
Popular Tags