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

Revision 6785, 8.0 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.ExampleMail;
28
29public class MimeTokenEmbeddedMessageTest extends TestCase {
30   
31    MimeTokenStream stream;
32   
33    @Override
34    protected void setUp() throws Exception {
35        super.setUp();
36        stream = new MimeTokenStream();
37        InputStream in = new ByteArrayInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_BYTES);
38        stream.parse(in);
39    }
40
41    @Override
42    protected void tearDown() throws Exception {
43        super.tearDown();
44    }
45
46    public void testWhenRecurseShouldVisitInnerMailsAndInnerMultiparts() throws Exception {
47        stream.setRecursionMode(RecursionMode.M_RECURSE);
48
49        nextIs(EntityState.T_START_HEADER);
50        nextIs(EntityState.T_FIELD);
51        nextIs(EntityState.T_FIELD);
52        nextIs(EntityState.T_FIELD);
53        nextIs(EntityState.T_FIELD);
54        nextIs(EntityState.T_FIELD);
55        nextIs(EntityState.T_END_HEADER);
56       
57        nextIs(EntityState.T_START_MULTIPART);
58       
59        nextIs(EntityState.T_PREAMBLE);
60
61        // PART ONE
62        nextIs(EntityState.T_START_BODYPART);
63        nextIs(EntityState.T_START_HEADER);
64        nextIs(EntityState.T_FIELD);
65        nextIs(EntityState.T_END_HEADER);
66        nextIs(EntityState.T_BODY);
67        checkInputStream("Rhubarb!\r\n");
68        nextIs(EntityState.T_END_BODYPART);
69       
70        // PART TWO
71        nextIs(EntityState.T_START_BODYPART);
72        nextIs(EntityState.T_START_HEADER);
73        nextIs(EntityState.T_FIELD);
74        nextIs(EntityState.T_FIELD);
75        nextIs(EntityState.T_END_HEADER);
76        nextIs(EntityState.T_BODY);
77        checkInputStream("987654321AHPLA\r\n");
78        nextIs(EntityState.T_END_BODYPART);
79       
80        // PART THREE
81        nextIs(EntityState.T_START_BODYPART);
82        nextIs(EntityState.T_START_HEADER);
83        nextIs(EntityState.T_FIELD);
84        nextIs(EntityState.T_END_HEADER);
85        nextIs(EntityState.T_START_MESSAGE);
86        nextIs(EntityState.T_START_HEADER);
87        nextIs(EntityState.T_FIELD);
88        nextIs(EntityState.T_FIELD);
89        nextIs(EntityState.T_FIELD);
90        nextIs(EntityState.T_FIELD);
91        nextIs(EntityState.T_FIELD);
92        nextIs(EntityState.T_END_HEADER);
93        nextIs(EntityState.T_START_MULTIPART);
94        nextIs(EntityState.T_PREAMBLE);
95        nextIs(EntityState.T_START_BODYPART);
96        nextIs(EntityState.T_START_HEADER);
97        nextIs(EntityState.T_FIELD);
98        nextIs(EntityState.T_END_HEADER);
99        nextIs(EntityState.T_BODY);
100        checkInputStream("Custard!\r\n");
101        nextIs(EntityState.T_END_BODYPART);
102        nextIs(EntityState.T_START_BODYPART);
103        nextIs(EntityState.T_START_HEADER);
104        nextIs(EntityState.T_FIELD);
105        nextIs(EntityState.T_END_HEADER);
106        nextIs(EntityState.T_BODY);
107        checkInputStream("CUSTARDCUSTARDCUSTARD\r\n");
108        nextIs(EntityState.T_END_BODYPART);
109        nextIs(EntityState.T_EPILOGUE);
110        nextIs(EntityState.T_END_MULTIPART);   
111        nextIs(EntityState.T_END_MESSAGE);
112        nextIs(EntityState.T_END_BODYPART);
113       
114        // PART FOUR
115        nextIs(EntityState.T_START_BODYPART);
116        nextIs(EntityState.T_START_HEADER);
117        nextIs(EntityState.T_FIELD);
118        nextIs(EntityState.T_END_HEADER);
119        nextIs(EntityState.T_START_MULTIPART);
120        checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MULTIPART_MIXED);
121        nextIs(EntityState.T_END_MULTIPART);
122        nextIs(EntityState.T_END_BODYPART);
123        nextIs(EntityState.T_EPILOGUE);
124        nextIs(EntityState.T_END_MULTIPART);
125        nextIs(EntityState.T_END_MESSAGE);
126        nextIs(EntityState.T_END_OF_STREAM);
127    }
128   
129   
130    public void testWhenFlatAtStartShouldIgnoreMultipartStructure() throws Exception {
131        stream.setRecursionMode(RecursionMode.M_FLAT);
132        nextIs(EntityState.T_START_HEADER);
133        nextIs(EntityState.T_FIELD);
134        nextIs(EntityState.T_FIELD);
135        nextIs(EntityState.T_FIELD);
136        nextIs(EntityState.T_FIELD);
137        nextIs(EntityState.T_FIELD);
138        nextIs(EntityState.T_END_HEADER);
139       
140        nextIs(EntityState.T_BODY);
141       
142        checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_BODY);
143       
144        nextIs(EntityState.T_END_MESSAGE);
145    }
146   
147    public void testWhenFlatShouldIgnoreInnerMailsAndInnerMultiparts() throws Exception {
148        nextIs(EntityState.T_START_HEADER);
149        nextIs(EntityState.T_FIELD);
150        nextIs(EntityState.T_FIELD);
151        nextIs(EntityState.T_FIELD);
152        nextIs(EntityState.T_FIELD);
153        nextIs(EntityState.T_FIELD);
154        nextIs(EntityState.T_END_HEADER);
155       
156        nextIs(EntityState.T_START_MULTIPART);
157       
158        stream.setRecursionMode(RecursionMode.M_FLAT);
159        nextIs(EntityState.T_PREAMBLE);
160
161        // PART ONE
162        nextIs(EntityState.T_START_BODYPART);
163        nextIs(EntityState.T_START_HEADER);
164        nextIs(EntityState.T_FIELD);
165        nextIs(EntityState.T_END_HEADER);
166        nextIs(EntityState.T_BODY);
167        checkInputStream("Rhubarb!\r\n");
168        nextIs(EntityState.T_END_BODYPART);
169       
170        // PART TWO
171        nextIs(EntityState.T_START_BODYPART);
172        nextIs(EntityState.T_START_HEADER);
173        nextIs(EntityState.T_FIELD);
174        nextIs(EntityState.T_FIELD);
175        nextIs(EntityState.T_END_HEADER);
176        nextIs(EntityState.T_BODY);
177        checkInputStream("987654321AHPLA\r\n");
178        nextIs(EntityState.T_END_BODYPART);
179       
180        // PART THREE
181        nextIs(EntityState.T_START_BODYPART);
182        nextIs(EntityState.T_START_HEADER);
183        nextIs(EntityState.T_FIELD);
184        nextIs(EntityState.T_END_HEADER);
185        nextIs(EntityState.T_BODY);
186        checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MAIL);
187        nextIs(EntityState.T_END_BODYPART);
188       
189        // PART FOUR
190        nextIs(EntityState.T_START_BODYPART);
191        nextIs(EntityState.T_START_HEADER);
192        nextIs(EntityState.T_FIELD);
193        nextIs(EntityState.T_END_HEADER);
194        nextIs(EntityState.T_BODY);
195        checkInputStream(ExampleMail.MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MULTIPART_MIXED);
196        nextIs(EntityState.T_END_BODYPART);
197        nextIs(EntityState.T_EPILOGUE);
198        nextIs(EntityState.T_END_MULTIPART);
199    }
200   
201    private void checkInputStream(String expected) throws Exception {
202        InputStream inputStream = stream.getInputStream();
203        int next = inputStream.read();
204        int i=0;
205        while (next != -1) {
206            assertEquals("@" + i, expected.charAt(i++), (char) next);
207            next = inputStream.read();
208        }
209        assertEquals(expected.length(), i);
210    }
211   
212    private void nextIs(EntityState state) throws Exception {
213        assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(stream.next()));
214    }
215}
Note: See TracBrowser for help on using the repository browser.