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

Revision 6785, 8.7 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        // an empty preamble should not raise a T_PREAMBLE event
112        // nextIs(EntityStates.T_PREAMBLE);
113        nextShouldBeStandardPart(true);
114        nextShouldBeStandardPart(true);
115        nextIs(EntityState.T_EPILOGUE);
116        nextIs(EntityState.T_END_MULTIPART);
117        nextIs(EntityState.T_END_MESSAGE);
118        nextIs(EntityState.T_END_BODYPART);
119        nextShouldBeStandardPart(true);
120        nextIs(EntityState.T_EPILOGUE);
121        nextIs(EntityState.T_END_MULTIPART);
122    }
123   
124
125    public void testWhenRecurseShouldTreatInnerMailAsAnyOtherPart() throws Exception {
126        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
127        nextIs(EntityState.T_START_HEADER);
128        nextIs(EntityState.T_FIELD);
129        nextIs(EntityState.T_FIELD);
130        nextIs(EntityState.T_FIELD);
131        nextIs(EntityState.T_FIELD);
132        nextIs(EntityState.T_FIELD);
133        nextIs(EntityState.T_FIELD);
134        nextIs(EntityState.T_END_HEADER);
135       
136        nextIs(EntityState.T_START_MULTIPART);
137        nextIs(EntityState.T_PREAMBLE);
138        nextShouldBeStandardPart(false);
139       
140        nextShouldBeStandardPart(true);
141        nextShouldBeStandardPart(true);
142        nextShouldBeStandardPart(true);
143        nextIs(EntityState.T_EPILOGUE);
144        nextIs(EntityState.T_END_MULTIPART);
145    }
146   
147    public void testWhenNoRecurseInputStreamShouldContainInnerMail() throws Exception {
148        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
149        nextIs(EntityState.T_START_HEADER);
150        nextIs(EntityState.T_FIELD);
151        nextIs(EntityState.T_FIELD);
152        nextIs(EntityState.T_FIELD);
153        nextIs(EntityState.T_FIELD);
154        nextIs(EntityState.T_FIELD);
155        nextIs(EntityState.T_FIELD);
156        nextIs(EntityState.T_END_HEADER);
157       
158        nextIs(EntityState.T_START_MULTIPART);
159        nextIs(EntityState.T_PREAMBLE);
160        nextShouldBeStandardPart(false);
161       
162        nextShouldBeStandardPart(true);
163        nextIs(EntityState.T_START_BODYPART);
164        nextIs(EntityState.T_START_HEADER);
165        nextIs(EntityState.T_FIELD);
166        nextIs(EntityState.T_END_HEADER);
167        nextIs(EntityState.T_BODY);
168        InputStream inputStream = stream.getInputStream();
169        int next = inputStream.read();
170        int i=0;
171        while (next != -1) {
172            assertEquals("@" + i, INNER_MAIL.charAt(i++), (char) next);
173            next = inputStream.read();
174        }
175        assertEquals(INNER_MAIL.length()-2, i);
176    }
177   
178    public void testSetNoRecurseSoInputStreamShouldContainInnerMail() throws Exception {
179        nextIs(EntityState.T_START_HEADER);
180        nextIs(EntityState.T_FIELD);
181        nextIs(EntityState.T_FIELD);
182        nextIs(EntityState.T_FIELD);
183        nextIs(EntityState.T_FIELD);
184        nextIs(EntityState.T_FIELD);
185        nextIs(EntityState.T_FIELD);
186        nextIs(EntityState.T_END_HEADER);
187       
188        nextIs(EntityState.T_START_MULTIPART);
189        nextIs(EntityState.T_PREAMBLE);
190        nextShouldBeStandardPart(false);
191       
192        nextShouldBeStandardPart(true);
193        stream.setRecursionMode(RecursionMode.M_NO_RECURSE);
194        nextIs(EntityState.T_START_BODYPART);
195        nextIs(EntityState.T_START_HEADER);
196        nextIs(EntityState.T_FIELD);
197        nextIs(EntityState.T_END_HEADER);
198        nextIs(EntityState.T_BODY);
199        InputStream inputStream = stream.getInputStream();
200        int next = inputStream.read();
201        int i=0;
202        while (next != -1) {
203            assertEquals("@" + i, INNER_MAIL.charAt(i++), (char) next);
204            next = inputStream.read();
205        }
206        assertEquals(INNER_MAIL.length()-2, i);
207    }
208
209    private void nextShouldBeStandardPart(boolean withHeader) throws Exception {
210        nextIs(EntityState.T_START_BODYPART);
211        nextIs(EntityState.T_START_HEADER);
212        if (withHeader) {
213            nextIs(EntityState.T_FIELD);
214        }
215        nextIs(EntityState.T_END_HEADER);
216        nextIs(EntityState.T_BODY);
217        nextIs(EntityState.T_END_BODYPART);
218    }
219   
220    private void nextIs(EntityState state) throws Exception {
221        assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(stream.next()));
222    }
223}
Note: See TracBrowser for help on using the repository browser.