MIME4J-5 Performance patch 3, https://issues.apache.org/jira/browse/MIME4J-5. Contrib...
[mime4j.git] / src / test / java / org / apache / james / mime4j / MimeBoundaryInputStreamTest.java
blob4f8c7ff522159e13aab6c3e386bd84a17273c850
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 ****************************************************************/
20 package org.apache.james.mime4j;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import org.apache.james.mime4j.MimeBoundaryInputStream;
30 import junit.framework.TestCase;
32 /**
36 * @version $Id: MimeBoundaryInputStreamTest.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
38 public class MimeBoundaryInputStreamTest extends TestCase {
40 public void testBasicReading() throws IOException {
41 String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
42 "Line 3\r\nLine 4\r\n--boundary--";
44 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
46 InputBuffer buffer = new InputBuffer(bis, 4096);
48 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
49 assertEquals("Line 1\r\nLine 2", read(mime1, 5));
51 assertFalse(mime1.isLastPart());
53 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
54 assertEquals("Line 3\r\nLine 4", read(mime2, 5));
56 assertTrue(mime2.isLastPart());
59 public void testLenientLineDelimiterReading() throws IOException {
60 String text = "Line 1\r\nLine 2\n--boundary\n" +
61 "Line 3\r\nLine 4\n--boundary--\n";
63 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
65 InputBuffer buffer = new InputBuffer(bis, 4096);
67 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
68 assertEquals("Line 1\r\nLine 2", read(mime1, 5));
70 assertFalse(mime1.isLastPart());
72 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
73 assertEquals("Line 3\r\nLine 4", read(mime2, 5));
75 assertTrue(mime2.isLastPart());
78 public void testBasicReadingSmallBuffer1() throws IOException {
79 String text = "yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada\r\n--boundary\r\n" +
80 "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah\r\n--boundary--";
82 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
84 InputBuffer buffer = new InputBuffer(bis, 20);
86 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
87 assertEquals("yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada",
88 read(mime1, 10));
90 assertFalse(mime1.isLastPart());
92 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
93 assertEquals("blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah",
94 read(mime2, 10));
96 assertTrue(mime2.isLastPart());
99 public void testBasicReadingSmallBuffer2() throws IOException {
100 String text = "yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada\r\n--boundary\r\n" +
101 "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah\r\n--boundary--";
103 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
105 InputBuffer buffer = new InputBuffer(bis, 20);
107 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
109 assertEquals("yadayadayadayadayadayadayadayadayadayadayadayadayadayadayadayada",
110 read(mime1, 25));
112 assertFalse(mime1.isLastPart());
114 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
115 assertEquals("blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah",
116 read(mime2, 25));
118 assertTrue(mime2.isLastPart());
121 public void testBasicReadingByOneByte() throws IOException {
122 String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
123 "Line 3\r\nLine 4\r\n--boundary--";
125 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
127 InputBuffer buffer = new InputBuffer(bis, 4096);
129 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
130 assertEquals("Line 1\r\nLine 2", readByOneByte(mime1));
132 assertFalse(mime1.isLastPart());
134 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
135 assertEquals("Line 3\r\nLine 4", readByOneByte(mime2));
137 assertTrue(mime2.isLastPart());
141 * Tests that a CRLF immediately preceding a boundary isn't included in
142 * the stream.
144 public void testCRLFPrecedingBoundary() throws IOException {
145 String text = "Line 1\r\nLine 2\r\n--boundary\r\n" +
146 "Line 3\r\nLine 4\r\n\r\n--boundary\r\n";
148 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes("US-ASCII"));
150 InputBuffer buffer = new InputBuffer(bis, 4096);
152 MimeBoundaryInputStream mime1 = new MimeBoundaryInputStream(buffer, "boundary");
153 assertEquals("Line 1\r\nLine 2", read(mime1, 5));
155 assertFalse(mime1.isLastPart());
157 MimeBoundaryInputStream mime2 = new MimeBoundaryInputStream(buffer, "boundary");
158 assertEquals("Line 3\r\nLine 4\r\n", read(mime2, 5));
160 assertFalse(mime2.isLastPart());
163 private String readByOneByte(InputStream is) throws IOException {
164 StringBuffer sb = new StringBuffer();
165 int b = 0;
166 while ((b = is.read()) != -1) {
167 sb.append((char) b);
169 return sb.toString();
172 private String read(InputStream is, int bufsize) throws IOException {
173 StringBuffer sb = new StringBuffer();
174 int l;
175 byte[] tmp = new byte[bufsize];
176 while ((l = is.read(tmp)) != -1) {
177 for (int i = 0; i < l; i++) {
178 sb.append((char) tmp[i]);
181 return sb.toString();
185 * Tests that a stream containing only a boundary is empty.
187 public void testImmediateBoundary() throws IOException {
188 String text = "--boundary\r\n";
190 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
191 InputBuffer buffer = new InputBuffer(bis, 4096);
193 MimeBoundaryInputStream stream =
194 new MimeBoundaryInputStream(buffer, "boundary");
195 assertEquals(-1, stream.read());
197 text = "\r\n--boundary\r\n";
199 bis = new ByteArrayInputStream(text.getBytes());
200 buffer = new InputBuffer(bis, 4096);
201 stream =
202 new MimeBoundaryInputStream(buffer, "boundary");
203 assertEquals(-1, stream.read());
207 * Tests that hasMoreParts behave as expected.
209 public void testHasMoreParts() throws IOException {
210 String text = "--boundary--\r\n";
212 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
213 InputBuffer buffer = new InputBuffer(bis, 4096);
214 MimeBoundaryInputStream stream =
215 new MimeBoundaryInputStream(buffer, "boundary");
216 assertEquals(-1, stream.read());
217 assertTrue(stream.isLastPart());
221 * Tests that a stream containing only a boundary is empty.
223 public void testPrefixIsBoundary() throws IOException {
224 String text = "Line 1\r\n\r\n--boundaryyada\r\n";
226 ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
227 InputBuffer buffer = new InputBuffer(bis, 4096);
228 MimeBoundaryInputStream stream =
229 new MimeBoundaryInputStream(buffer, "boundary");
230 assertEquals("Line 1\r\n", read(stream, 100));
232 text = "--boundaryyada\r\n";
234 bis = new ByteArrayInputStream(text.getBytes());
235 buffer = new InputBuffer(bis, 4096);
236 stream = new MimeBoundaryInputStream(buffer, "boundary");
237 assertEquals(-1, stream.read());
241 public void testBasicReadLine() throws Exception {
243 String[] teststrs = new String[5];
244 teststrs[0] = "Hello\r\n";
245 teststrs[1] = "This string should be much longer than the size of the input buffer " +
246 "which is only 20 bytes for this test\r\n";
247 StringBuffer sb = new StringBuffer();
248 for (int i = 0; i < 15; i++) {
249 sb.append("123456789 ");
251 sb.append("and stuff like that\r\n");
252 teststrs[2] = sb.toString();
253 teststrs[3] = "\r\n";
254 teststrs[4] = "And goodbye\r\n";
256 String term = "\r\n--1234\r\n";
258 ByteArrayOutputStream outstream = new ByteArrayOutputStream();
260 for (int i = 0; i < teststrs.length; i++) {
261 outstream.write(teststrs[i].getBytes("US-ASCII"));
263 outstream.write(term.getBytes("US-ASCII"));
264 byte[] raw = outstream.toByteArray();
266 InputBuffer inbuffer = new InputBuffer(new ByteArrayInputStream(raw), 20);
267 BufferingInputStream instream = new MimeBoundaryInputStream(inbuffer, "1234");
269 ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
270 for (int i = 0; i < teststrs.length; i++) {
271 linebuf.clear();
272 instream.readLine(linebuf);
273 String s = new String(linebuf.toByteArray(), "US-ASCII");
274 assertEquals(teststrs[i], s);
276 assertEquals(-1, instream.readLine(linebuf));
277 assertEquals(-1, instream.readLine(linebuf));
280 public void testReadEmptyLine() throws Exception {
282 String teststr = "01234567890123456789\n\n\r\n\r\r\n\n\n\n\n\n--1234\r\n";
283 byte[] raw = teststr.getBytes("US-ASCII");
285 InputBuffer inbuffer = new InputBuffer(new ByteArrayInputStream(raw), 20);
286 BufferingInputStream instream = new MimeBoundaryInputStream(inbuffer, "1234");
288 ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
289 linebuf.clear();
290 instream.readLine(linebuf);
291 String s = new String(linebuf.toByteArray(), "US-ASCII");
292 assertEquals("01234567890123456789\n", s);
294 linebuf.clear();
295 instream.readLine(linebuf);
296 s = new String(linebuf.toByteArray(), "US-ASCII");
297 assertEquals("\n", s);
299 linebuf.clear();
300 instream.readLine(linebuf);
301 s = new String(linebuf.toByteArray(), "US-ASCII");
302 assertEquals("\r\n", s);
304 linebuf.clear();
305 instream.readLine(linebuf);
306 s = new String(linebuf.toByteArray(), "US-ASCII");
307 assertEquals("\r\r\n", s);
309 linebuf.clear();
310 instream.readLine(linebuf);
311 s = new String(linebuf.toByteArray(), "US-ASCII");
312 assertEquals("\n", s);
314 linebuf.clear();
315 instream.readLine(linebuf);
316 s = new String(linebuf.toByteArray(), "US-ASCII");
317 assertEquals("\n", s);
319 linebuf.clear();
320 instream.readLine(linebuf);
321 s = new String(linebuf.toByteArray(), "US-ASCII");
322 assertEquals("\n", s);
324 linebuf.clear();
325 instream.readLine(linebuf);
326 s = new String(linebuf.toByteArray(), "US-ASCII");
327 assertEquals("\n", s);
329 assertEquals(-1, instream.readLine(linebuf));
330 assertEquals(-1, instream.readLine(linebuf));