source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/test/java/org/apache/james/mime4j/stream/MultipartTokensTest.java @ 6785

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