source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.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.io;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26
27import org.apache.james.mime4j.io.BufferedLineReaderInputStream;
28import org.apache.james.mime4j.io.LineReaderInputStream;
29import org.apache.james.mime4j.io.MimeBoundaryInputStream;
30import org.apache.james.mime4j.util.ByteArrayBuffer;
31
32
33import junit.framework.TestCase;
34
35public class MimeBoundaryInputStreamTest extends TestCase {
36
37    public void testBasicReading() throws IOException {
38        String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
39                "Line 3\r\nLine 4\r\n--boundary--";
40       
41        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
42       
43        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
44       
45        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
46        assertEquals("Line 1\r\nLine 2", read(mime1, 5));
47       
48        assertFalse(mime1.isLastPart());
49       
50        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
51        assertEquals("Line 3\r\nLine 4", read(mime2, 5));
52
53        assertTrue(mime2.isLastPart());
54    }
55   
56    public void testLenientLineDelimiterReading() throws IOException {
57        String text = "Line 1\r\nLine 2\n--boundary\n" +
58                "Line 3\r\nLine 4\n--boundary--\n";
59       
60        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
61       
62        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
63       
64        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
65        assertEquals("Line 1\r\nLine 2", read(mime1, 5));
66       
67        assertFalse(mime1.isLastPart());
68       
69        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
70        assertEquals("Line 3\r\nLine 4", read(mime2, 5));
71
72        assertTrue(mime2.isLastPart());
73    }
74   
75    public void testBasicReadingSmallBuffer1() throws IOException {
76        String text = "yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada\r\n--boundary\r\n" +
77                "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah\r\n--boundary--";
78       
79        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
80       
81        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 20);
82       
83        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
84        assertEquals("yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada",
85                read(mime1, 10));
86       
87        assertFalse(mime1.isLastPart());
88       
89        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
90        assertEquals("blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah",
91                read(mime2, 10));
92
93        assertTrue(mime2.isLastPart());
94    }
95   
96    public void testBasicReadingSmallBuffer2() throws IOException {
97        String text = "yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada\r\n--boundary\r\n" +
98                "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah\r\n--boundary--";
99       
100        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
101       
102        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 20);
103       
104        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
105       
106        assertEquals("yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada",
107                read(mime1, 25));
108       
109        assertFalse(mime1.isLastPart());
110       
111        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
112        assertEquals("blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah",
113                read(mime2, 25));
114
115        assertTrue(mime2.isLastPart());
116    }
117   
118    public void testBasicReadingByOneByte() throws IOException {
119        String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
120                "Line 3\r\nLine 4\r\n--boundary--";
121       
122        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
123       
124        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
125       
126        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
127        assertEquals("Line 1\r\nLine 2", readByOneByte(mime1));
128       
129        assertFalse(mime1.isLastPart());
130       
131        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
132        assertEquals("Line 3\r\nLine 4", readByOneByte(mime2));
133
134        assertTrue(mime2.isLastPart());
135    }
136   
137    /**
138     * Tests that a CRLF immediately preceding a boundary isn't included in
139     * the stream.
140     */
141    public void testCRLFPrecedingBoundary() throws IOException {
142        String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
143                "Line 3\r\nLine 4\r\n\r\n--boundary\r\n";
144       
145        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
146       
147        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
148       
149        MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
150        assertEquals("Line 1\r\nLine 2", read(mime1, 5));
151       
152        assertFalse(mime1.isLastPart());
153       
154        MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
155        assertEquals("Line 3\r\nLine 4\r\n", read(mime2, 5));
156
157        assertFalse(mime2.isLastPart());
158    }
159   
160    private String readByOneByte(InputStream is) throws IOException {
161        StringBuilder sb = new StringBuilder();
162        int b = 0;
163        while ((b = is.read()) != -1) {
164            sb.append((char) b);
165        }
166        return sb.toString();
167    }
168
169    private String read(InputStream is, int bufsize) throws IOException {
170        StringBuilder sb = new StringBuilder();
171        int l;
172        byte[] tmp = new byte[bufsize];
173        while ((l = is.read(tmp)) != -1) {
174            for (int i = 0; i < l; i++) {
175                sb.append((char) tmp[i]);
176            }
177        }
178        return sb.toString();
179    }
180   
181    /**
182     * Tests that a stream containing only a boundary is empty.
183     */
184    public void testImmediateBoundary() throws IOException {
185        String text = "--boundary\r\n";
186       
187        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
188        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
189       
190        MimeBoundaryInputStream stream =
191            new MimeBoundaryInputStream(buffer, "boundary");
192        assertEquals(-1, stream.read());
193       
194        text = "\r\n--boundary\r\n";
195       
196        bis = new ByteArrayInputStream(text.getBytes());
197        buffer = new BufferedLineReaderInputStream(bis, 4096);
198        stream =
199            new MimeBoundaryInputStream(buffer, "boundary");
200        assertEquals(-1, stream.read());
201    }
202   
203    /**
204     * Tests that hasMoreParts behave as expected.
205     */
206    public void testHasMoreParts() throws IOException {
207        String text = "--boundary--\r\n";
208       
209        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
210        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
211        MimeBoundaryInputStream stream =
212            new MimeBoundaryInputStream(buffer, "boundary");
213        assertEquals(-1, stream.read());
214        assertTrue(stream.isLastPart());
215    }
216   
217    /**
218     * Tests that a stream containing only a boundary is empty.
219     */
220    public void testPrefixIsBoundary() throws IOException {
221        String text = "Line 1\r\n\r\n--boundaryyada\r\n";
222       
223        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
224        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 4096);
225        MimeBoundaryInputStream stream =
226            new MimeBoundaryInputStream(buffer, "boundary");
227        assertEquals("Line 1\r\n", read(stream, 100));
228       
229        text = "--boundaryyada\r\n";
230       
231        bis = new ByteArrayInputStream(text.getBytes());
232        buffer = new BufferedLineReaderInputStream(bis, 4096);
233        stream = new MimeBoundaryInputStream(buffer, "boundary");
234        assertEquals(-1, stream.read());
235    }
236   
237   
238    public void testBasicReadLine() throws Exception {
239       
240        String[] teststrs = new String[5];
241        teststrs[0] = "Hello\r\n";
242        teststrs[1] = "This string should be much longer than the size of the input buffer " +
243                "which is only 20 bytes for this test\r\n";
244        StringBuilder sb = new StringBuilder();
245        for (int i = 0; i < 15; i++) {
246            sb.append("123456789 ");
247        }
248        sb.append("and stuff like that\r\n");
249        teststrs[2] = sb.toString();
250        teststrs[3] = "\r\n";
251        teststrs[4] = "And goodbye\r\n";
252
253        String term = "\r\n--1234\r\n";
254       
255        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
256       
257        for (String teststr : teststrs) {
258            outstream.write(teststr.getBytes("US-ASCII"));
259        }
260        outstream.write(term.getBytes("US-ASCII"));
261        byte[] raw = outstream.toByteArray();
262       
263        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 20);
264        LineReaderInputStream instream = new MimeBoundaryInputStream(inbuffer, "1234");
265       
266        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
267        for (String teststr : teststrs) {
268            linebuf.clear();
269            instream.readLine(linebuf);
270            String s = new String(linebuf.toByteArray(), "US-ASCII");
271            assertEquals(teststr, s);
272        }
273        assertEquals(-1, instream.readLine(linebuf));
274        assertEquals(-1, instream.readLine(linebuf));
275    }
276   
277    public void testReadEmptyLine() throws Exception {
278       
279        String teststr = "01234567890123456789\n\n\r\n\r\r\n\n\n\n\n\n--1234\r\n";
280        byte[] raw = teststr.getBytes("US-ASCII");
281       
282        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 20);
283        LineReaderInputStream instream = new MimeBoundaryInputStream(inbuffer, "1234");
284       
285        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
286        linebuf.clear();
287        instream.readLine(linebuf);
288        String s = new String(linebuf.toByteArray(), "US-ASCII");
289        assertEquals("01234567890123456789\n", s);
290       
291        linebuf.clear();
292        instream.readLine(linebuf);
293        s = new String(linebuf.toByteArray(), "US-ASCII");
294        assertEquals("\n", s);
295       
296        linebuf.clear();
297        instream.readLine(linebuf);
298        s = new String(linebuf.toByteArray(), "US-ASCII");
299        assertEquals("\r\n", s);
300
301        linebuf.clear();
302        instream.readLine(linebuf);
303        s = new String(linebuf.toByteArray(), "US-ASCII");
304        assertEquals("\r\r\n", s);
305
306        linebuf.clear();
307        instream.readLine(linebuf);
308        s = new String(linebuf.toByteArray(), "US-ASCII");
309        assertEquals("\n", s);
310
311        linebuf.clear();
312        instream.readLine(linebuf);
313        s = new String(linebuf.toByteArray(), "US-ASCII");
314        assertEquals("\n", s);
315
316        linebuf.clear();
317        instream.readLine(linebuf);
318        s = new String(linebuf.toByteArray(), "US-ASCII");
319        assertEquals("\n", s);
320
321        linebuf.clear();
322        instream.readLine(linebuf);
323        s = new String(linebuf.toByteArray(), "US-ASCII");
324        assertEquals("\n", s);
325
326        assertEquals(-1, instream.readLine(linebuf));
327        assertEquals(-1, instream.readLine(linebuf));
328    }
329   
330    public void testboundaryLongerThanBuffer() throws IOException {
331        String text = "--looooooooooooooooooooooooooong-boundary\r\n";
332       
333        ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
334        BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 10);
335       
336        try {
337            new MimeBoundaryInputStream(buffer, "looooooooooooooooooooooooooong-boundary");
338            fail("IllegalArgumentException should have been thrown");
339        } catch (IllegalArgumentException expected) {
340        }
341    }
342
343}
Note: See TracBrowser for help on using the repository browser.