/
StreamTest.java
273 lines (233 loc) · 8.08 KB
/
StreamTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.jvnet.mimepull;
import junit.framework.AssertionFailedError;
import org.junit.Assert;
import org.junit.Test;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
/**
* @author Jitendra Kotamraju
*/
public class StreamTest {
public StreamTest() {}
@Test
public void testOrderRead() throws Exception {
testOrderRead(123456789);
}
@Test
public void testPartSize9195() throws Exception {
testOrderRead(9195);
}
@Test
public void testPartSize9196() throws Exception {
testOrderRead(9196);
}
@Test
public void testAllPartSizes() throws Exception {
for (int size = 0; size < 50000; size++) {
if (size %1000 == 0) {
System.out.println("Trying for the size="+size);
}
try {
testOrderRead(size);
} catch (AssertionFailedError e) {
System.out.println("Failed for part length " + size + " bytes");
throw e;
}
}
}
@Test
public void testAllPartSizesForBufferedStream() throws Exception {
for (int size = 0; size < 50000; size++) {
if (size %1000 == 0) {
System.out.println("Trying for the size="+size);
}
try {
testOrderRead(size, getBufferedInputStream(size));
} catch (AssertionFailedError e) {
System.out.println("Failed for part length " + size + " bytes");
throw e;
}
}
}
private void testOrderRead(int size, InputStream is) throws Exception {
String boundary = "boundary";
MIMEConfig config = new MIMEConfig();
MIMEMessage mm = new MIMEMessage(is, boundary , config);
MIMEPart partA = mm.getPart("partA");
verifyPart(partA.read(), 0, size);
partA.close();
MIMEPart partB = mm.getPart("partB");
verifyPart(partB.read(), 1, size);
partB.close();
MIMEPart partC = mm.getPart("partC");
verifyPart(partC.read(), 2, size);
partC.close();
}
private void testOrderRead(int size) throws Exception {
testOrderRead(size, getInputStream(size));
}
// Parts are accessed in order. The data is accessed using readOnce()
// and there shouldn't be any data stored in temp files.
@Test
public void testOrderReadOnce() throws Exception {
String boundary = "boundary";
int size = 123456789;
MIMEConfig config = new MIMEConfig();
MIMEMessage mm = new MIMEMessage(getInputStream(size), boundary , config);
MIMEPart partA = mm.getPart("partA");
verifyPart(partA.readOnce(), 0, size);
partA.close();
MIMEPart partB = mm.getPart("partB");
verifyPart(partB.readOnce(), 1, size);
partB.close();
MIMEPart partC = mm.getPart("partC");
verifyPart(partC.readOnce(), 2, size);
partC.close();
}
// partB, partA, partC are accessed in that order. Then partA should
// go to disk. partB, and partC are accessed from in-memory
@Test
public void testOutofOrderRead() throws Exception {
String boundary = "boundary";
int size = 12345678;
MIMEConfig config = new MIMEConfig();
MIMEMessage mm = new MIMEMessage(getInputStream(size), boundary , config);
MIMEPart partA = mm.getPart("partA");
MIMEPart partB = mm.getPart("partB");
MIMEPart partC = mm.getPart("partC");
verifyPart(partB.read(), 1, size);
verifyPart(partA.read(), 0, size);
verifyPart(partC.read(), 2, size);
partA.close();
partB.close();
partC.close();
}
// partB, partA, partC are accessed in that order. Then partA should
// go to disk. partB, and partC are accessed from in-memory
@Test
public void testOutofOrderReadOnce() throws Exception {
String boundary = "boundary";
int size = 12345678;
MIMEConfig config = new MIMEConfig();
MIMEMessage mm = new MIMEMessage(getInputStream(size), boundary , config);
MIMEPart partA = mm.getPart("partA");
MIMEPart partB = mm.getPart("partB");
MIMEPart partC = mm.getPart("partC");
verifyPart(partB.readOnce(), 1, size);
partB.close();
verifyPart(partA.readOnce(), 0, size);
partA.close();
verifyPart(partC.readOnce(), 2, size);
partC.close();
}
// MIMEPart.read() is called twice
@Test
public void testOutofOrderMultipleRead() throws Exception {
String boundary = "boundary";
final int size = 12345678;
MIMEConfig config = new MIMEConfig();
MIMEMessage mm = new MIMEMessage(getInputStream(size), boundary , config);
final MIMEPart partA = mm.getPart("partA");
MIMEPart partB = mm.getPart("partB");
MIMEPart partC = mm.getPart("partC");
verifyPart(partB.readOnce(), 1, size);
Thread[] threads = new Thread[2];
for(int i=0; i < threads.length; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
try {
verifyPart(partA.read(), 0, size);
} catch(Exception e) {
Assert.fail();
}
}
});
}
for (Thread thread : threads) {
thread.start();
}
verifyPart(partC.readOnce(), 2, size);
for (Thread thread : threads) {
thread.join();
}
partA.close();
partB.close();
partC.close();
}
/*
private void verifyPart(InputStream in, int partNo, int size) throws Exception {
int i = 0;
int ch;
while((ch=in.read()) != -1) {
Assert.assertEquals((byte)('A'+(partNo+i++)%26), (byte)ch);
}
Assert.assertEquals(size, i);
in.close();
}
*/
private void verifyPart(InputStream in, int partNo, int size) throws Exception {
byte[] buf = new byte[8192];
int total = 0;
int len;
while((len=in.read(buf, 0, buf.length)) != -1) {
for(int i=0; i < len; i++) {
Assert.assertEquals((byte)('A'+(partNo+total+i)%26), buf[i]);
}
total += len;
}
Assert.assertEquals(size, total);
in.close();
}
/*
* partA's content ABC...ZAB...
* partB's content BCD...ZAB...
* partC's content CDE...ZAB...
*/
private InputStream getInputStream(final int size) {
final byte[] data = (
"--boundary\r\n"+
"Content-Type: text/plain\r\n"+
"Content-Id: partA\r\n\r\n"+
"1\r\n"+
"--boundary\r\n"+
"Content-Type: text/plain\r\n"+
"Content-ID: partB\r\n\r\n"+
"2\r\n"+
"--boundary\r\n"+
"Content-Type: text/plain\r\n"+
"Content-ID: partC\r\n\r\n"+
"3\r\n"+
"--boundary--").getBytes();
return new InputStream() {
int i, j;
@Override
public int read() throws IOException {
if (i >= data.length) {
return -1;
} else if (data[i] == '1' || data[i] == '2' || data[i] == '3') {
if (j < size) {
int partNo = data[i]-'1';
return (byte)('A'+(partNo+j++)%26);
} else {
j = 0; i++;
}
}
return data[i++];
}
};
}
private InputStream getBufferedInputStream(int size) {
return new BufferedInputStream(getInputStream(size));
}
}