MIME4J-5 Performance patch 3, https://issues.apache.org/jira/browse/MIME4J-5. Contrib...
[mime4j.git] / src / test / java / org / apache / james / mime4j / BufferingInputStreamAdaptorTest.java
blobed62a6aefa8993becccd9695c9ca84b0316c9bdc
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;
25 import junit.framework.TestCase;
27 public class BufferingInputStreamAdaptorTest extends TestCase {
29 public void testBasicOperations() throws Exception {
30 String text = "ah blahblah";
31 byte[] b1 = text.getBytes("US-ASCII");
33 BufferingInputStreamAdaptor instream = new BufferingInputStreamAdaptor(
34 new ByteArrayInputStream(b1));
36 assertEquals((byte)'a', instream.read());
37 assertEquals((byte)'h', instream.read());
38 assertEquals((byte)' ', instream.read());
40 byte[] tmp1 = new byte[4];
41 assertEquals(4, instream.read(tmp1));
42 assertEquals(4, instream.read(tmp1));
44 assertEquals(-1, instream.read(tmp1));
45 assertEquals(-1, instream.read(tmp1));
46 assertEquals(-1, instream.read());
47 assertEquals(-1, instream.read());
50 public void testBasicReadLine() throws Exception {
52 String[] teststrs = new String[5];
53 teststrs[0] = "Hello\r\n";
54 teststrs[1] = "This string should be much longer than the size of the input buffer " +
55 "which is only 16 bytes for this test\r\n";
56 StringBuffer sb = new StringBuffer();
57 for (int i = 0; i < 15; i++) {
58 sb.append("123456789 ");
60 sb.append("and stuff like that\r\n");
61 teststrs[2] = sb.toString();
62 teststrs[3] = "\r\n";
63 teststrs[4] = "And goodbye\r\n";
65 ByteArrayOutputStream outstream = new ByteArrayOutputStream();
67 for (int i = 0; i < teststrs.length; i++) {
68 outstream.write(teststrs[i].getBytes("US-ASCII"));
70 byte[] raw = outstream.toByteArray();
72 BufferingInputStreamAdaptor instream = new BufferingInputStreamAdaptor(
73 new ByteArrayInputStream(raw));
75 ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
76 for (int i = 0; i < teststrs.length; i++) {
77 linebuf.clear();
78 instream.readLine(linebuf);
79 String s = new String(linebuf.toByteArray(), "US-ASCII");
80 assertEquals(teststrs[i], s);
82 assertEquals(-1, instream.readLine(linebuf));
83 assertEquals(-1, instream.readLine(linebuf));
86 public void testReadEmptyLine() throws Exception {
88 String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n";
89 byte[] raw = teststr.getBytes("US-ASCII");
91 BufferingInputStreamAdaptor instream = new BufferingInputStreamAdaptor(
92 new ByteArrayInputStream(raw));
94 ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
95 linebuf.clear();
96 instream.readLine(linebuf);
97 String s = new String(linebuf.toByteArray(), "US-ASCII");
98 assertEquals("\n", s);
100 linebuf.clear();
101 instream.readLine(linebuf);
102 s = new String(linebuf.toByteArray(), "US-ASCII");
103 assertEquals("\n", s);
105 linebuf.clear();
106 instream.readLine(linebuf);
107 s = new String(linebuf.toByteArray(), "US-ASCII");
108 assertEquals("\r\n", s);
110 linebuf.clear();
111 instream.readLine(linebuf);
112 s = new String(linebuf.toByteArray(), "US-ASCII");
113 assertEquals("\r\r\n", s);
115 linebuf.clear();
116 instream.readLine(linebuf);
117 s = new String(linebuf.toByteArray(), "US-ASCII");
118 assertEquals("\n", s);
120 linebuf.clear();
121 instream.readLine(linebuf);
122 s = new String(linebuf.toByteArray(), "US-ASCII");
123 assertEquals("\n", s);
125 linebuf.clear();
126 instream.readLine(linebuf);
127 s = new String(linebuf.toByteArray(), "US-ASCII");
128 assertEquals("\n", s);
130 linebuf.clear();
131 instream.readLine(linebuf);
132 s = new String(linebuf.toByteArray(), "US-ASCII");
133 assertEquals("\n", s);
135 linebuf.clear();
136 instream.readLine(linebuf);
137 s = new String(linebuf.toByteArray(), "US-ASCII");
138 assertEquals("\n", s);
140 assertEquals(-1, instream.readLine(linebuf));
141 assertEquals(-1, instream.readLine(linebuf));