Skip to content

Commit 4c581ae

Browse files
committed
Refactor MultipartBodyTest (make sure it works with different buffer sizes)
1 parent 88d582e commit 4c581ae

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

src/test/java/com/ning/http/client/multipart/MultipartBodyTest.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414
package com.ning.http.client.multipart;
1515

1616
import static java.nio.charset.StandardCharsets.UTF_8;
17-
18-
import org.testng.Assert;
19-
import org.testng.annotations.Test;
20-
21-
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
17+
import static org.testng.Assert.*;
2218

2319
import java.io.File;
2420
import java.io.IOException;
@@ -30,57 +26,64 @@
3026
import java.util.List;
3127
import java.util.concurrent.atomic.AtomicLong;
3228

29+
import org.testng.annotations.Test;
30+
31+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
32+
3333
public class MultipartBodyTest {
3434

35-
@Test
36-
public void transferWithCopy() throws IOException {
37-
try (MultipartBody multipartBody = buildMultipart()) {
38-
long tranferred = transferWithCopy(multipartBody);
39-
Assert.assertEquals(tranferred, multipartBody.getContentLength());
35+
private static final List<Part> PARTS = new ArrayList<>();
36+
37+
static {
38+
try {
39+
PARTS.add(new FilePart("filePart", getTestfile()));
40+
} catch (URISyntaxException e) {
41+
throw new ExceptionInInitializerError(e);
4042
}
43+
PARTS.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
44+
PARTS.add(new StringPart("stringPart", "testString"));
4145
}
4246

43-
@Test
44-
public void transferWithCopy2() throws IOException {
45-
try (final MultipartBody multipartBody = buildMultipart()) {
46-
final long contentLength = multipartBody.getContentLength();
47-
final int bufferSize = (int)contentLength - 1;
48-
final long tranferred = transferWithCopy(multipartBody, bufferSize);
49-
Assert.assertEquals(tranferred, contentLength);
50-
}
47+
private static File getTestfile() throws URISyntaxException {
48+
final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
49+
final URL url = cl.getResource("textfile.txt");
50+
assertNotNull(url);
51+
return new File(url.toURI());
5152
}
5253

53-
@Test
54-
public void transferZeroCopy() throws IOException {
55-
try (MultipartBody multipartBody = buildMultipart()) {
56-
long tranferred = transferZeroCopy(multipartBody);
57-
Assert.assertEquals(tranferred, multipartBody.getContentLength());
54+
private static long MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE;
55+
56+
static {
57+
try (MultipartBody dummyBody = buildMultipart()) {
58+
// separator is random
59+
MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE = dummyBody.getContentLength() + 100;
60+
} catch (IOException e) {
61+
throw new ExceptionInInitializerError(e);
5862
}
5963
}
6064

6165
private static MultipartBody buildMultipart() {
62-
List<Part> parts = new ArrayList<>();
63-
parts.add(new FilePart("filePart", getTestfile()));
64-
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
65-
parts.add(new StringPart("stringPart", "testString"));
66-
return MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap());
66+
return MultipartUtils.newMultipartBody(PARTS, new FluentCaseInsensitiveStringsMap());
6767
}
6868

69-
private static File getTestfile() {
70-
final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
71-
final URL url = cl.getResource("textfile.txt");
72-
Assert.assertNotNull(url);
73-
File file = null;
74-
try {
75-
file = new File(url.toURI());
76-
} catch (URISyntaxException use) {
77-
Assert.fail("uri syntax error");
69+
@Test
70+
public void transferWithCopy() throws Exception {
71+
for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) {
72+
try (MultipartBody multipartBody = buildMultipart()) {
73+
long tranferred = transferWithCopy(multipartBody, bufferLength);
74+
assertEquals(tranferred, multipartBody.getContentLength());
75+
}
7876
}
79-
return file;
8077
}
8178

82-
private static long transferWithCopy(MultipartBody multipartBody) throws IOException {
83-
return transferWithCopy(multipartBody, 8192);
79+
@Test
80+
public void transferZeroCopy() throws Exception {
81+
for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) {
82+
try (MultipartBody multipartBody = buildMultipart()) {
83+
long tranferred = transferZeroCopy(multipartBody, bufferLength);
84+
assertEquals(tranferred, multipartBody.getContentLength());
85+
}
86+
}
8487
}
8588

8689
private static long transferWithCopy(MultipartBody multipartBody, int bufferSize) throws IOException {
@@ -97,9 +100,9 @@ private static long transferWithCopy(MultipartBody multipartBody, int bufferSize
97100
return totalBytes;
98101
}
99102

100-
private static long transferZeroCopy(MultipartBody multipartBody) throws IOException {
103+
private static long transferZeroCopy(MultipartBody multipartBody, int bufferSize) throws IOException {
101104

102-
final ByteBuffer buffer = ByteBuffer.allocate(8192);
105+
final ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
103106
final AtomicLong transferred = new AtomicLong();
104107

105108
WritableByteChannel mockChannel = new WritableByteChannel() {

0 commit comments

Comments
 (0)