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

Revision 6785, 8.5 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.InputStream;
24
25import junit.framework.TestCase;
26
27import org.apache.james.mime4j.util.CharsetUtil;
28
29public class MimeTokenNoRecurseTest extends TestCase {
30
31    private static final String INNER_MAIL = "From: Timothy Tayler <tim@example.org>\r\n" +
32                "To: Joshua Tetley <joshua@example.org>\r\n" +
33                "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
34                "Subject: Multipart Without RFC822 Part\r\n" +
35                "Content-Type: multipart/mixed;boundary=42\r\n\r\n" +
36                "--42\r\n" +
37                "Content-Type:text/plain; charset=US-ASCII\r\n\r\n" +
38                "First part of this mail\r\n" +
39                "--42\r\n" +
40                "Content-Type:text/plain; charset=US-ASCII\r\n\r\n" +
41                "Second part of this mail\r\n" +
42                "--42--\r\n";
43
44    private static final String MAIL_WITH_RFC822_PART = "MIME-Version: 1.0\r\n" +
45            "From: Timothy Tayler <tim@example.org>\r\n" +
46            "To: Joshua Tetley <joshua@example.org>\r\n" +
47            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
48            "Subject: Multipart With RFC822 Part\r\n" +
49            "Content-Type: multipart/mixed;boundary=1729\r\n\r\n" +
50            "A short premable\r\n" +
51            "--1729\r\n\r\n" +
52            "First part has no headers\r\n" +
53            "--1729\r\n" +
54            "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" +
55            "Second part is plain text\r\n" +
56            "--1729\r\n" +
57            "Content-Type: message/rfc822\r\n\r\n" +
58            INNER_MAIL +
59            "--1729\r\n" +
60            "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" +
61            "Last part is plain text\r\n" +
62            "--1729--\r\n" +
63            "The End";
64   
65    MimeTokenStream stream;
66   
67    @Override
68    protected void setUp() throws Exception {
69        super.setUp();
70        stream = new MimeTokenStream();
71        byte[] bytes = CharsetUtil.US_ASCII.encode(MAIL_WITH_RFC822_PART).array();
72        InputStream in = new ByteArrayInputStream(bytes);
73        stream.parse(in);
74    }
75
76    @Override
77    protected void tearDown() throws Exception {
78        super.tearDown();
79    }
80
81    public void testWhenRecurseShouldRecurseInnerMail() throws Exception {
82        stream.setRecursionMode(RecursionMode.M_RECURSE);
83        nextIs(EntityState.T_START_HEADER);
84        nextIs(EntityState.T_FIELD);
85        nextIs(EntityState.T_FIELD);
86        nextIs(EntityState.T_FIELD);
87        nextIs(EntityState.T_FIELD);
88        nextIs(EntityState.T_FIELD);
89        nextIs(EntityState.T_FIELD);
90        nextIs(EntityState.T_END_HEADER);
91       
92        nextIs(EntityState.T_START_MULTIPART);
93        nextIs(EntityState.T_PREAMBLE);
94        nextShouldBeStandardPart(false);
95       
96        nextShouldBeStandardPart(true);
97       
98        nextIs(EntityState.T_START_BODYPART);
99        nextIs(EntityState.T_START_HEADER);
100        nextIs(EntityState.T_FIELD);
101        nextIs(EntityState.T_END_HEADER);
102        nextIs(EntityState.T_START_MESSAGE);
103        nextIs(EntityState.T_START_HEADER);
104        nextIs(EntityState.T_FIELD);
105        nextIs(EntityState.T_FIELD);
106        nextIs(EntityState.T_FIELD);
107        nextIs(EntityState.T_FIELD);
108        nextIs(EntityState.T_FIELD);
109        nextIs(EntityState.T_END_HEADER);
110        nextIs(EntityState.T_START_MULTIPART);
111        nextShouldBeStandardPart(true);
112        nextShouldBeStandardPart(true);
113        nextIs(EntityState.T_END_MULTIPART);
114        nextIs(EntityState.T_END_MESSAGE);
115        nextIs(EntityState.T_END_BODYPART);
116        nextShouldBeStandardPart(true);
117        nextIs(EntityState.T_EPILOGUE);
118        nextIs(EntityState.T_END_MULTIPART);
119    }
120   
121
122    public void testWhenRecurseShouldTreatInnerMailAsAnyOtherPart() throws Exception {
123        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
124        nextIs(EntityState.T_START_HEADER);
125        nextIs(EntityState.T_FIELD);
126        nextIs(EntityState.T_FIELD);
127        nextIs(EntityState.T_FIELD);
128        nextIs(EntityState.T_FIELD);
129        nextIs(EntityState.T_FIELD);
130        nextIs(EntityState.T_FIELD);
131        nextIs(EntityState.T_END_HEADER);
132       
133        nextIs(EntityState.T_START_MULTIPART);
134        nextIs(EntityState.T_PREAMBLE);
135        nextShouldBeStandardPart(false);
136       
137        nextShouldBeStandardPart(true);
138        nextShouldBeStandardPart(true);
139        nextShouldBeStandardPart(true);
140        nextIs(EntityState.T_EPILOGUE);
141        nextIs(EntityState.T_END_MULTIPART);
142    }
143   
144    public void testWhenNoRecurseInputStreamShouldContainInnerMail() throws Exception {
145        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
146        nextIs(EntityState.T_START_HEADER);
147        nextIs(EntityState.T_FIELD);
148        nextIs(EntityState.T_FIELD);
149        nextIs(EntityState.T_FIELD);
150        nextIs(EntityState.T_FIELD);
151        nextIs(EntityState.T_FIELD);
152        nextIs(EntityState.T_FIELD);
153        nextIs(EntityState.T_END_HEADER);
154       
155        nextIs(EntityState.T_START_MULTIPART);
156        nextIs(EntityState.T_PREAMBLE);
157        nextShouldBeStandardPart(false);
158       
159        nextShouldBeStandardPart(true);
160        nextIs(EntityState.T_START_BODYPART);
161        nextIs(EntityState.T_START_HEADER);
162        nextIs(EntityState.T_FIELD);
163        nextIs(EntityState.T_END_HEADER);
164        nextIs(EntityState.T_BODY);
165        InputStream inputStream = stream.getInputStream();
166        int next = inputStream.read();
167        int i=0;
168        while (next != -1) {
169            assertEquals("@" + i, INNER_MAIL.charAt(i++), (char) next);
170            next = inputStream.read();
171        }
172        assertEquals(INNER_MAIL.length()-2, i);
173    }
174   
175    public void testSetNoRecurseSoInputStreamShouldContainInnerMail() throws Exception {
176        nextIs(EntityState.T_START_HEADER);
177        nextIs(EntityState.T_FIELD);
178        nextIs(EntityState.T_FIELD);
179        nextIs(EntityState.T_FIELD);
180        nextIs(EntityState.T_FIELD);
181        nextIs(EntityState.T_FIELD);
182        nextIs(EntityState.T_FIELD);
183        nextIs(EntityState.T_END_HEADER);
184       
185        nextIs(EntityState.T_START_MULTIPART);
186        nextIs(EntityState.T_PREAMBLE);
187        nextShouldBeStandardPart(false);
188       
189        nextShouldBeStandardPart(true);
190        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
191        nextIs(EntityState.T_START_BODYPART);
192        nextIs(EntityState.T_START_HEADER);
193        nextIs(EntityState.T_FIELD);
194        nextIs(EntityState.T_END_HEADER);
195        nextIs(EntityState.T_BODY);
196        InputStream inputStream = stream.getInputStream();
197        int next = inputStream.read();
198        int i=0;
199        while (next != -1) {
200            assertEquals("@" + i, INNER_MAIL.charAt(i++), (char) next);
201            next = inputStream.read();
202        }
203        assertEquals(INNER_MAIL.length()-2, i);
204    }
205
206    private void nextShouldBeStandardPart(boolean withHeader) throws Exception {
207        nextIs(EntityState.T_START_BODYPART);
208        nextIs(EntityState.T_START_HEADER);
209        if (withHeader) {
210            nextIs(EntityState.T_FIELD);
211        }
212        nextIs(EntityState.T_END_HEADER);
213        nextIs(EntityState.T_BODY);
214        nextIs(EntityState.T_END_BODYPART);
215    }
216   
217    private void nextIs(EntityState state) throws Exception {
218        assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(stream.next()));
219    }
220}
Note: See TracBrowser for help on using the repository browser.