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

Revision 6785, 6.2 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 org.apache.james.mime4j.io.BufferedLineReaderInputStream;
23import org.apache.james.mime4j.io.LineReaderInputStream;
24import org.apache.james.mime4j.io.MaxLineLimitException;
25import org.apache.james.mime4j.util.ByteArrayBuffer;
26
27import java.io.ByteArrayInputStream;
28import java.io.ByteArrayOutputStream;
29
30import junit.framework.TestCase;
31
32public class BufferedLineReaderInputStreamTest extends TestCase {
33
34    public void testBasicOperations() throws Exception {
35        String text = "ah blahblah";
36        byte[] b1 = text.getBytes("US-ASCII");
37        BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
38       
39        assertEquals((byte)'a', instream.read());
40        assertEquals((byte)'h', instream.read());
41        assertEquals((byte)' ', instream.read());
42
43        byte[] tmp1 = new byte[4];
44        assertEquals(4, instream.read(tmp1));
45        assertEquals(4, instream.read(tmp1));
46
47        assertEquals(-1, instream.read(tmp1));
48        assertEquals(-1, instream.read(tmp1));
49        assertEquals(-1, instream.read());
50        assertEquals(-1, instream.read());
51    }
52
53    public void testBasicReadLine() throws Exception {
54       
55        String[] teststrs = new String[5];
56        teststrs[0] = "Hello\r\n";
57        teststrs[1] = "This string should be much longer than the size of the input buffer " +
58                "which is only 16 bytes for this test\r\n";
59        StringBuilder sb = new StringBuilder();
60        for (int i = 0; i < 15; i++) {
61            sb.append("123456789 ");
62        }
63        sb.append("and stuff like that\r\n");
64        teststrs[2] = sb.toString();
65        teststrs[3] = "\r\n";
66        teststrs[4] = "And goodbye\r\n";
67
68        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
69       
70        for (String teststr : teststrs) {
71            outstream.write(teststr.getBytes("US-ASCII"));
72        }
73        byte[] raw = outstream.toByteArray();
74       
75        BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 16);
76       
77        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
78        for (String teststr : teststrs) {
79            linebuf.clear();
80            instream.readLine(linebuf);
81            String s = new String(linebuf.toByteArray(), "US-ASCII");
82            assertEquals(teststr, s);
83        }
84        assertEquals(-1, instream.readLine(linebuf));
85        assertEquals(-1, instream.readLine(linebuf));
86    }
87   
88    public void testReadEmptyLine() throws Exception {
89       
90        String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n";
91        byte[] raw = teststr.getBytes("US-ASCII");
92       
93        LineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 4);
94       
95        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
96        linebuf.clear();
97        instream.readLine(linebuf);
98        String s = new String(linebuf.toByteArray(), "US-ASCII");
99        assertEquals("\n", s);
100       
101        linebuf.clear();
102        instream.readLine(linebuf);
103        s = new String(linebuf.toByteArray(), "US-ASCII");
104        assertEquals("\n", s);
105       
106        linebuf.clear();
107        instream.readLine(linebuf);
108        s = new String(linebuf.toByteArray(), "US-ASCII");
109        assertEquals("\r\n", s);
110
111        linebuf.clear();
112        instream.readLine(linebuf);
113        s = new String(linebuf.toByteArray(), "US-ASCII");
114        assertEquals("\r\r\n", s);
115
116        linebuf.clear();
117        instream.readLine(linebuf);
118        s = new String(linebuf.toByteArray(), "US-ASCII");
119        assertEquals("\n", s);
120
121        linebuf.clear();
122        instream.readLine(linebuf);
123        s = new String(linebuf.toByteArray(), "US-ASCII");
124        assertEquals("\n", s);
125
126        linebuf.clear();
127        instream.readLine(linebuf);
128        s = new String(linebuf.toByteArray(), "US-ASCII");
129        assertEquals("\n", s);
130
131        linebuf.clear();
132        instream.readLine(linebuf);
133        s = new String(linebuf.toByteArray(), "US-ASCII");
134        assertEquals("\n", s);
135
136        linebuf.clear();
137        instream.readLine(linebuf);
138        s = new String(linebuf.toByteArray(), "US-ASCII");
139        assertEquals("\n", s);
140
141        assertEquals(-1, instream.readLine(linebuf));
142        assertEquals(-1, instream.readLine(linebuf));
143    }
144
145    public void testReadEmptyLineMaxLimit() throws Exception {
146       
147        String teststr = "1234567890\r\n";
148        byte[] raw = teststr.getBytes("US-ASCII");
149       
150        LineReaderInputStream instream1 = new BufferedLineReaderInputStream(
151                new ByteArrayInputStream(raw), 1024, 13);
152        ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
153        linebuf.clear();
154        instream1.readLine(linebuf);
155
156        LineReaderInputStream instream2 = new BufferedLineReaderInputStream(
157                new ByteArrayInputStream(raw), 1024, 12);
158        linebuf.clear();
159        try {
160            instream2.readLine(linebuf);
161            fail("MaxLineLimitException should have been thrown");
162        } catch (MaxLineLimitException ex) {
163        }
164    }
165
166}
Note: See TracBrowser for help on using the repository browser.