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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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.io;
22
import org.apache.james.mime4j.io.BufferedLineReaderInputStream;
23
import org.apache.james.mime4j.io.LineReaderInputStream;
24
import org.apache.james.mime4j.io.MaxLineLimitException;
25
import org.apache.james.mime4j.util.ByteArrayBuffer;
27
import java.io.ByteArrayInputStream;
28
import java.io.ByteArrayOutputStream;
30
import junit.framework.TestCase;
32
public class BufferedLineReaderInputStreamTest extends TestCase {
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);
39
assertEquals((byte)'a', instream.read());
40
assertEquals((byte)'h', instream.read());
41
assertEquals((byte)' ', instream.read());
43
byte[] tmp1 = new byte[4];
44
assertEquals(4, instream.read(tmp1));
45
assertEquals(4, instream.read(tmp1));
47
assertEquals(-1, instream.read(tmp1));
48
assertEquals(-1, instream.read(tmp1));
49
assertEquals(-1, instream.read());
50
assertEquals(-1, instream.read());
53
public void testBasicReadLine() throws Exception {
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 ");
63
sb.append("and stuff like that\r\n");
64
teststrs[2] = sb.toString();
66
teststrs[4] = "And goodbye\r\n";
68
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
70
for (String teststr : teststrs) {
71
outstream.write(teststr.getBytes("US-ASCII"));
73
byte[] raw = outstream.toByteArray();
75
BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 16);
77
ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
78
for (String teststr : teststrs) {
80
instream.readLine(linebuf);
81
String s = new String(linebuf.toByteArray(), "US-ASCII");
82
assertEquals(teststr, s);
84
assertEquals(-1, instream.readLine(linebuf));
85
assertEquals(-1, instream.readLine(linebuf));
88
public void testReadEmptyLine() throws Exception {
90
String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n";
91
byte[] raw = teststr.getBytes("US-ASCII");
93
LineReaderInputStream instream = new BufferedLineReaderInputStream(new ByteArrayInputStream(raw), 4);
95
ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
97
instream.readLine(linebuf);
98
String s = new String(linebuf.toByteArray(), "US-ASCII");
99
assertEquals("\n", s);
102
instream.readLine(linebuf);
103
s = new String(linebuf.toByteArray(), "US-ASCII");
104
assertEquals("\n", s);
107
instream.readLine(linebuf);
108
s = new String(linebuf.toByteArray(), "US-ASCII");
109
assertEquals("\r\n", s);
112
instream.readLine(linebuf);
113
s = new String(linebuf.toByteArray(), "US-ASCII");
114
assertEquals("\r\r\n", s);
117
instream.readLine(linebuf);
118
s = new String(linebuf.toByteArray(), "US-ASCII");
119
assertEquals("\n", s);
122
instream.readLine(linebuf);
123
s = new String(linebuf.toByteArray(), "US-ASCII");
124
assertEquals("\n", s);
127
instream.readLine(linebuf);
128
s = new String(linebuf.toByteArray(), "US-ASCII");
129
assertEquals("\n", s);
132
instream.readLine(linebuf);
133
s = new String(linebuf.toByteArray(), "US-ASCII");
134
assertEquals("\n", s);
137
instream.readLine(linebuf);
138
s = new String(linebuf.toByteArray(), "US-ASCII");
139
assertEquals("\n", s);
141
assertEquals(-1, instream.readLine(linebuf));
142
assertEquals(-1, instream.readLine(linebuf));
145
public void testReadEmptyLineMaxLimit() throws Exception {
147
String teststr = "1234567890\r\n";
148
byte[] raw = teststr.getBytes("US-ASCII");
150
LineReaderInputStream instream1 = new BufferedLineReaderInputStream(
151
new ByteArrayInputStream(raw), 1024, 13);
152
ByteArrayBuffer linebuf = new ByteArrayBuffer(8);
154
instream1.readLine(linebuf);
156
LineReaderInputStream instream2 = new BufferedLineReaderInputStream(
157
new ByteArrayInputStream(raw), 1024, 12);
160
instream2.readLine(linebuf);
161
fail("MaxLineLimitException should have been thrown");
162
} catch (MaxLineLimitException ex) {