source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/core/src/test/java/org/apache/james/mime4j/stream/MultipartTokensTest.java @ 6785

Revision 6785, 13.6 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.stream;
21
22import java.io.ByteArrayInputStream;
23import java.io.IOException;
24import java.nio.charset.Charset;
25
26import junit.framework.TestCase;
27
28import org.apache.commons.io.IOUtils;
29import org.apache.james.mime4j.MimeException;
30import org.apache.james.mime4j.util.CharsetUtil;
31
32public class MultipartTokensTest extends TestCase {
33
34    private static final Charset US_ASCII = CharsetUtil.US_ASCII;
35   
36    private static final String BODY = "A Preamble\r\n" +
37                "--1729\r\n\r\n" +
38                "Simple plain text\r\n" +
39                "--1729\r\n" +
40                "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" +
41                "Some more text\r\n" +
42                "--1729--\r\n";
43    public static final String MESSAGE = "To: Road Runner <runner@example.org>\r\n" +
44            "From: Wile E. Cayote <wile@example.org>\r\n" +
45            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
46            "Subject: Mail\r\n" +
47            "Content-Type: multipart/mixed;boundary=1729\r\n\r\n" +
48            BODY;
49   
50    public static final String COMPLEX_MESSAGE = "To: Wile E. Cayote <wile@example.org>\r\n" +
51    "From: Road Runner <runner@example.org>\r\n" +
52    "Date: Tue, 19 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
53    "Subject: Mail\r\n" +
54    "Content-Type: multipart/mixed;boundary=42\r\n\r\n" +
55    "A little preamble\r\n" +
56    "--42\r\n" +
57    "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" +
58    "Rhubard!\r\n" +
59    "--42\r\n" +
60    "Content-Type: message/rfc822\r\n\r\n" +
61    MESSAGE +
62    "\r\n" +
63    "--42\r\n" +
64    "\r\n" +
65    "Custard!" +
66    "\r\n" +
67    "--42--\r\n";
68   
69    public static final String COMPLEX_QP_MESSAGE =
70        "Content-Transfer-Encoding: quoted-printable\r\n" +
71        "Content-Type: message/rfc822; charset=us-ascii\r\n" +
72        "\r\n" +
73        "Subject: The subject\r\n" +
74        "Content-Type: multipart/alternative;\r\n" +
75        "        boundary=3D=22----=3DNextPart=22\r\n" +
76        "\r\n" +
77        "This is a multi-part message in MIME format.\r\n" +
78        "\r\n" +
79        "------=3DNextPart\r\n" +
80        "Content-Type: text/plain;\r\n" +
81        "        charset=3D=22iso-8859-1=22\r\n" +
82        "\r\n" +
83        "Some text\r\n" +
84        "\r\n" +
85        "------=3DNextPart\r\n" +
86        "Content-Type: text/html;\r\n" +
87        "        charset=3D=22iso-8859-1=22\r\n" +
88        "\r\n" +
89        "<HTML><BODY>=3D Some HTML =3D</BODY></HTML>\r\n" +
90        "------=3DNextPart--\r\n" +
91        "\r\n" +
92        "\r\n";
93   
94    MimeTokenStream parser;
95   
96    @Override
97    protected void setUp() throws Exception {
98        super.setUp();
99        parser = new MimeTokenStream();
100    }
101
102    @Override
103    protected void tearDown() throws Exception {
104        super.tearDown();
105    }
106   
107    public void testShouldParseSimpleMessage() throws Exception {
108        parser.parse(new ByteArrayInputStream(US_ASCII.encode(MESSAGE).array()));
109        checkState(EntityState.T_START_HEADER);
110        checkState(EntityState.T_FIELD);
111        checkState(EntityState.T_FIELD);
112        checkState(EntityState.T_FIELD);
113        checkState(EntityState.T_FIELD);
114        checkState(EntityState.T_FIELD);
115        checkState(EntityState.T_END_HEADER);
116        checkState(EntityState.T_START_MULTIPART);
117        checkState(EntityState.T_PREAMBLE);
118        checkState(EntityState.T_START_BODYPART);
119        checkState(EntityState.T_START_HEADER);
120        checkState(EntityState.T_END_HEADER);
121        checkState(EntityState.T_BODY);
122        checkState(EntityState.T_END_BODYPART);
123        checkState(EntityState.T_START_BODYPART);
124        checkState(EntityState.T_START_HEADER);
125        checkState(EntityState.T_FIELD);
126        checkState(EntityState.T_END_HEADER);
127        checkState(EntityState.T_BODY);
128        checkState(EntityState.T_END_BODYPART);
129        checkState(EntityState.T_EPILOGUE);
130        checkState(EntityState.T_END_MULTIPART);
131        checkState(EntityState.T_END_MESSAGE);
132        checkState(EntityState.T_END_OF_STREAM);
133    }
134   
135    public void testShouldParseMoreComplexMessage() throws Exception {
136        String message =
137            "Content-Type: multipart/alternative; boundary=\"outer-boundary\"\r\n" +
138            "\r\n" +
139            "--outer-boundary\r\n" +
140            "Content-Type: multipart/alternative; boundary=\"inner-boundary\"\r\n" +
141            "\r\n" +
142            "--inner-boundary\r\n" +
143            "Content-Type: text/plain\r\n" +
144            "\r\n" +
145            "Some text\r\n" +
146            "--inner-boundary--\r\n" +
147            "\r\n" +
148            "foo\r\n" +
149            "--outer-boundary--\r\n";
150       
151        parser.parse(new ByteArrayInputStream(US_ASCII.encode(message).array()));
152        checkState(EntityState.T_START_HEADER);
153        checkState(EntityState.T_FIELD);
154        checkState(EntityState.T_END_HEADER);
155        checkState(EntityState.T_START_MULTIPART);
156        // an empty preamble should not rais a T_PREAMBLE event
157        // checkState(EntityStates.T_PREAMBLE);
158        checkState(EntityState.T_START_BODYPART);
159        checkState(EntityState.T_START_HEADER);
160        checkState(EntityState.T_FIELD);
161        checkState(EntityState.T_END_HEADER);
162        checkState(EntityState.T_START_MULTIPART);
163        // an empty preamble should not rais a T_PREAMBLE event
164        // checkState(EntityStates.T_PREAMBLE);
165        checkState(EntityState.T_START_BODYPART);
166        checkState(EntityState.T_START_HEADER);
167        checkState(EntityState.T_FIELD);
168        checkState(EntityState.T_END_HEADER);
169        checkState(EntityState.T_BODY);
170        checkState(EntityState.T_END_BODYPART);
171        checkState(EntityState.T_EPILOGUE);
172        checkState(EntityState.T_END_MULTIPART);
173        checkState(EntityState.T_END_BODYPART);
174        checkState(EntityState.T_EPILOGUE);
175        checkState(EntityState.T_END_MULTIPART);
176        checkState(EntityState.T_END_MESSAGE);
177        checkState(EntityState.T_END_OF_STREAM);
178    }
179   
180    public void testShouldParseMessageWithEmbeddedMessage() throws Exception {
181        parser.parse(new ByteArrayInputStream(US_ASCII.encode(COMPLEX_MESSAGE).array()));
182        checkState(EntityState.T_START_HEADER);
183            checkState(EntityState.T_FIELD);
184            checkState(EntityState.T_FIELD);
185            checkState(EntityState.T_FIELD);
186            checkState(EntityState.T_FIELD);
187            checkState(EntityState.T_FIELD);
188        checkState(EntityState.T_END_HEADER);
189        checkState(EntityState.T_START_MULTIPART);
190            checkState(EntityState.T_PREAMBLE);
191            checkState(EntityState.T_START_BODYPART);
192                checkState(EntityState.T_START_HEADER);
193                    checkState(EntityState.T_FIELD);
194                checkState(EntityState.T_END_HEADER);
195                checkState(EntityState.T_BODY);
196            checkState(EntityState.T_END_BODYPART);
197            checkState(EntityState.T_START_BODYPART);
198                checkState(EntityState.T_START_HEADER);
199                    checkState(EntityState.T_FIELD);
200                checkState(EntityState.T_END_HEADER);
201                checkState(EntityState.T_START_MESSAGE);
202                    checkState(EntityState.T_START_HEADER);
203                        checkState(EntityState.T_FIELD);
204                        checkState(EntityState.T_FIELD);
205                        checkState(EntityState.T_FIELD);
206                        checkState(EntityState.T_FIELD);
207                        checkState(EntityState.T_FIELD);
208                    checkState(EntityState.T_END_HEADER);
209                    checkState(EntityState.T_START_MULTIPART);
210                        checkState(EntityState.T_PREAMBLE);
211                        checkState(EntityState.T_START_BODYPART);
212                            checkState(EntityState.T_START_HEADER);
213                            checkState(EntityState.T_END_HEADER);   
214                            checkState(EntityState.T_BODY);
215                        checkState(EntityState.T_END_BODYPART);
216                        checkState(EntityState.T_START_BODYPART);
217                            checkState(EntityState.T_START_HEADER);
218                                checkState(EntityState.T_FIELD);
219                            checkState(EntityState.T_END_HEADER);
220                            checkState(EntityState.T_BODY);
221                        checkState(EntityState.T_END_BODYPART);
222                        checkState(EntityState.T_EPILOGUE);
223                    checkState(EntityState.T_END_MULTIPART);
224                checkState(EntityState.T_END_MESSAGE);
225            checkState(EntityState.T_END_BODYPART);
226            checkState(EntityState.T_START_BODYPART);
227                checkState(EntityState.T_START_HEADER);
228                checkState(EntityState.T_END_HEADER);
229                checkState(EntityState.T_BODY);
230            checkState(EntityState.T_END_BODYPART);
231            checkState(EntityState.T_EPILOGUE);
232        checkState(EntityState.T_END_MULTIPART);
233        checkState(EntityState.T_END_MESSAGE);
234        checkState(EntityState.T_END_OF_STREAM);
235    }
236
237    public void testShouldParseMessagesWithEmbeddedQuotedPrintableEncodedMessage() throws Exception {
238        parser.parse(new ByteArrayInputStream(US_ASCII.encode(COMPLEX_QP_MESSAGE).array()));
239        checkState(EntityState.T_START_HEADER);
240        checkState(EntityState.T_FIELD);
241        checkState(EntityState.T_FIELD);
242        checkState(EntityState.T_END_HEADER);
243        checkState(EntityState.T_START_MESSAGE);
244        checkState(EntityState.T_START_HEADER);
245        checkState(EntityState.T_FIELD);
246        checkState(EntityState.T_FIELD);
247        checkState(EntityState.T_END_HEADER);
248        checkState(EntityState.T_START_MULTIPART);
249        checkState(EntityState.T_PREAMBLE);
250        checkState(EntityState.T_START_BODYPART);
251        checkState(EntityState.T_START_HEADER);
252        checkState(EntityState.T_FIELD);
253        checkState(EntityState.T_END_HEADER);
254        checkState(EntityState.T_BODY);
255        assertEquals("text/plain", parser.getBodyDescriptor().getMimeType());
256        assertEquals("iso-8859-1", parser.getBodyDescriptor().getCharset());
257        assertEquals("Some text\r\n",
258                IOUtils.toString(parser.getInputStream()));
259        checkState(EntityState.T_END_BODYPART);
260        checkState(EntityState.T_START_BODYPART);
261        checkState(EntityState.T_START_HEADER);
262        checkState(EntityState.T_FIELD);
263        checkState(EntityState.T_END_HEADER);
264        checkState(EntityState.T_BODY);
265        assertEquals("text/html", parser.getBodyDescriptor().getMimeType());
266        assertEquals("iso-8859-1", parser.getBodyDescriptor().getCharset());
267        assertEquals("<HTML><BODY>= Some HTML =</BODY></HTML>",
268                IOUtils.toString(parser.getInputStream()));
269        checkState(EntityState.T_END_BODYPART);
270        checkState(EntityState.T_EPILOGUE);
271        checkState(EntityState.T_END_MULTIPART);
272        checkState(EntityState.T_END_MESSAGE);
273        checkState(EntityState.T_END_MESSAGE);
274        checkState(EntityState.T_END_OF_STREAM);
275    }
276   
277    public void testMultipartMessageWithoutHeader() throws Exception {
278        parser.parseHeadless(new ByteArrayInputStream(US_ASCII.encode(BODY).array()),
279                "multipart/mixed;boundary=1729");
280        // see https://issues.apache.org/jira/browse/MIME4J-153
281        // checkState(EntityStates.T_END_HEADER);
282       
283        // see https://issues.apache.org/jira/browse/MIME4J-153
284        // checkState(EntityStates.T_START_MULTIPART);
285       
286        // actually T_START_MULTIPART is the first state, but the
287        // checkState method calls next() before checking.
288        checkState(EntityState.T_PREAMBLE);
289        checkState(EntityState.T_START_BODYPART);
290        checkState(EntityState.T_START_HEADER);
291        checkState(EntityState.T_END_HEADER);
292        checkState(EntityState.T_BODY);
293        checkState(EntityState.T_END_BODYPART);
294        checkState(EntityState.T_START_BODYPART);
295        checkState(EntityState.T_START_HEADER);
296        checkState(EntityState.T_FIELD);
297        checkState(EntityState.T_END_HEADER);
298        checkState(EntityState.T_BODY);
299        checkState(EntityState.T_END_BODYPART);
300        checkState(EntityState.T_EPILOGUE);
301        checkState(EntityState.T_END_MULTIPART);
302        checkState(EntityState.T_END_MESSAGE);
303        checkState(EntityState.T_END_OF_STREAM);
304    }
305   
306    private void checkState(final EntityState state) throws IOException, MimeException {
307        assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(parser.next()));
308    }
309}
Note: See TracBrowser for help on using the repository browser.