KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > parser > BodyParserTest


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.parser;
37
38 import java.io.IOException JavaDoc;
39
40 import junit.framework.TestCase;
41
42 import org.columba.ristretto.io.CharSequenceSource;
43 import org.columba.ristretto.io.Source;
44 import org.columba.ristretto.message.LocalMimePart;
45 import org.columba.ristretto.message.MimeHeader;
46 import org.columba.ristretto.message.MimePart;
47
48 public class BodyParserTest extends TestCase {
49
50     /**
51      * Constructor for BodyParserTest.
52      * @param arg0
53      */

54     public BodyParserTest(String JavaDoc arg0) {
55         super(arg0);
56     }
57     
58     public void testSimpleBody() {
59         MimeHeader mimeHeader = new MimeHeader();
60         String JavaDoc testmail = "This is a test\r\n";
61         try {
62             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
63             Source source = message.getBody();
64             assertTrue( source.length() == testmail.length() );
65         } catch (IOException JavaDoc e) {
66             e.printStackTrace();
67         } catch (ParserException e) {
68             e.printStackTrace();
69         }
70     }
71
72     public void testMultipartBody() {
73         MimeHeader mimeHeader = new MimeHeader("multipart","mixed");
74         mimeHeader.putContentParameter("boundary","+?*");
75         String JavaDoc testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2\r\n--+?*--\r\n";
76         try {
77             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
78             Source source = message.getBody();
79             assertTrue( source.length() == testmail.length() );
80             assertTrue( message.countChilds() == 2);
81             Source body1 = ((LocalMimePart)message.getChild(0)).getBody();
82             assertEquals( "1", body1.toString());
83             Source body2 = ((LocalMimePart)message.getChild(1)).getBody();
84             assertTrue( body2.toString().equals("2"));
85                         
86         } catch (IOException JavaDoc e) {
87             e.printStackTrace();
88         } catch (ParserException e) {
89             e.printStackTrace();
90         }
91     }
92
93     public void testMultipartBody2() {
94         MimeHeader mimeHeader = new MimeHeader("multipart","mixed");
95         mimeHeader.putContentParameter("boundary","=_alternative 0047EBBC85256D9F_=");
96         String JavaDoc testmail = "This is a test\r\n--=_alternative 0047EBBC85256D9F_=\r\nContent-Type: text/plain\r\n\r\n1\r\n--=_alternative 0047EBBC85256D9F_=\r\nContent-Type: text/plain\r\n\r\n2\r\n--=_alternative 0047EBBC85256D9F_=--\r\n";
97         try {
98             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
99             Source source = message.getBody();
100             assertTrue( source.length() == testmail.length() );
101             assertTrue( message.countChilds() == 2);
102             Source body1 = ((LocalMimePart)message.getChild(0)).getBody();
103             assertTrue( body1.toString().equals("1"));
104             Source body2 = ((LocalMimePart)message.getChild(1)).getBody();
105             assertTrue( body2.toString().equals("2"));
106                         
107         } catch (IOException JavaDoc e) {
108             e.printStackTrace();
109         } catch (ParserException e) {
110             e.printStackTrace();
111         }
112     }
113
114     public void testMultipartMultipartBody() {
115         MimeHeader mimeHeader = new MimeHeader("multipart","mixed");
116         mimeHeader.putContentParameter("boundary","boundary");
117         String JavaDoc testmail = "This is a test\r\n--boundary\r\nContent-Type: text/plain\r\n\r\n1\r\n--boundary\r\nContent-Type: multipart/mixed; boundary=\"bound2\"\r\n\r\nblablbala\r\n--bound2\r\nContent-Type: text/plain\r\n\r\n21\r\n--bound2\r\nContent-Type: text/plain\r\n\r\n22\r\n--bound2--\r\n\r\n--boundary--\r\n";
118         try {
119             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
120             Source source = message.getBody();
121             assertTrue( source.length() == testmail.length() );
122             assertTrue( message.countChilds() == 2);
123             Source body1 = ((LocalMimePart)message.getChild(0)).getBody();
124             assertTrue( body1.toString().equals("1"));
125             MimePart nestedMultipart = (MimePart)message.getChild(1);
126             Source body21 = ((LocalMimePart)nestedMultipart.getChild(0)).getBody();
127             assertTrue( body21.toString().equals("21"));
128             Source body22 = ((LocalMimePart)nestedMultipart.getChild(1)).getBody();
129             assertTrue( body22.toString().equals("22"));
130                         
131         } catch (IOException JavaDoc e) {
132             e.printStackTrace();
133         } catch (ParserException e) {
134             e.printStackTrace();
135         }
136     }
137     
138     public void testMultipartBodyNoStartBoundary() {
139         MimeHeader mimeHeader = new MimeHeader("multipart","mixed");
140         mimeHeader.putContentParameter("boundary","willNotFind");
141         String JavaDoc testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2\r\n--+?*--\r\n";
142         try {
143             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
144         } catch (IOException JavaDoc e) {
145             e.printStackTrace();
146         } catch (ParserException e) {
147             assertTrue(true);
148             System.err.println( e.getSource() );
149             return;
150         }
151         
152         assertTrue( false );
153     }
154     
155     public void testMultipartBodyMissingEndBoundary() {
156         MimeHeader mimeHeader = new MimeHeader("multipart","mixed");
157         mimeHeader.putContentParameter("boundary","+?*");
158         String JavaDoc testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2";
159         try {
160             LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail));
161             Source source = message.getBody();
162             assertTrue( source.length() == testmail.length() );
163             assertTrue( message.countChilds() == 2);
164             Source body1 = ((LocalMimePart)message.getChild(0)).getBody();
165             assertEquals( "1", body1.toString());
166             Source body2 = ((LocalMimePart)message.getChild(1)).getBody();
167             assertEquals( "2", body2.toString());
168                         
169         } catch (IOException JavaDoc e) {
170             e.printStackTrace();
171         } catch (ParserException e) {
172             e.printStackTrace();
173         }
174     }
175     
176     
177 }
178
Popular Tags