Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c581ae

Browse files
committed
Refactor MultipartBodyTest (make sure it works with different buffer sizes)
1 parent88d582e commit4c581ae

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
packagecom.ning.http.client.multipart;
1515

1616
importstaticjava.nio.charset.StandardCharsets.UTF_8;
17-
18-
importorg.testng.Assert;
19-
importorg.testng.annotations.Test;
20-
21-
importcom.ning.http.client.FluentCaseInsensitiveStringsMap;
17+
importstaticorg.testng.Assert.*;
2218

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

29+
importorg.testng.annotations.Test;
30+
31+
importcom.ning.http.client.FluentCaseInsensitiveStringsMap;
32+
3333
publicclassMultipartBodyTest {
3434

35-
@Test
36-
publicvoidtransferWithCopy()throwsIOException {
37-
try (MultipartBodymultipartBody =buildMultipart()) {
38-
longtranferred =transferWithCopy(multipartBody);
39-
Assert.assertEquals(tranferred,multipartBody.getContentLength());
35+
privatestaticfinalList<Part>PARTS =newArrayList<>();
36+
37+
static {
38+
try {
39+
PARTS.add(newFilePart("filePart",getTestfile()));
40+
}catch (URISyntaxExceptione) {
41+
thrownewExceptionInInitializerError(e);
4042
}
43+
PARTS.add(newByteArrayPart("baPart","testMultiPart".getBytes(UTF_8),"application/test",UTF_8,"fileName"));
44+
PARTS.add(newStringPart("stringPart","testString"));
4145
}
4246

43-
@Test
44-
publicvoidtransferWithCopy2()throwsIOException {
45-
try (finalMultipartBodymultipartBody =buildMultipart()) {
46-
finallongcontentLength =multipartBody.getContentLength();
47-
finalintbufferSize = (int)contentLength -1;
48-
finallongtranferred =transferWithCopy(multipartBody,bufferSize);
49-
Assert.assertEquals(tranferred,contentLength);
50-
}
47+
privatestaticFilegetTestfile()throwsURISyntaxException {
48+
finalClassLoadercl =MultipartBodyTest.class.getClassLoader();
49+
finalURLurl =cl.getResource("textfile.txt");
50+
assertNotNull(url);
51+
returnnewFile(url.toURI());
5152
}
5253

53-
@Test
54-
publicvoidtransferZeroCopy()throwsIOException {
55-
try (MultipartBodymultipartBody =buildMultipart()) {
56-
longtranferred =transferZeroCopy(multipartBody);
57-
Assert.assertEquals(tranferred,multipartBody.getContentLength());
54+
privatestaticlongMAX_MULTIPART_CONTENT_LENGTH_ESTIMATE;
55+
56+
static {
57+
try (MultipartBodydummyBody =buildMultipart()) {
58+
// separator is random
59+
MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE =dummyBody.getContentLength() +100;
60+
}catch (IOExceptione) {
61+
thrownewExceptionInInitializerError(e);
5862
}
5963
}
6064

6165
privatestaticMultipartBodybuildMultipart() {
62-
List<Part>parts =newArrayList<>();
63-
parts.add(newFilePart("filePart",getTestfile()));
64-
parts.add(newByteArrayPart("baPart","testMultiPart".getBytes(UTF_8),"application/test",UTF_8,"fileName"));
65-
parts.add(newStringPart("stringPart","testString"));
66-
returnMultipartUtils.newMultipartBody(parts,newFluentCaseInsensitiveStringsMap());
66+
returnMultipartUtils.newMultipartBody(PARTS,newFluentCaseInsensitiveStringsMap());
6767
}
6868

69-
privatestaticFilegetTestfile() {
70-
finalClassLoadercl =MultipartBodyTest.class.getClassLoader();
71-
finalURLurl =cl.getResource("textfile.txt");
72-
Assert.assertNotNull(url);
73-
Filefile =null;
74-
try {
75-
file =newFile(url.toURI());
76-
}catch (URISyntaxExceptionuse) {
77-
Assert.fail("uri syntax error");
69+
@Test
70+
publicvoidtransferWithCopy()throwsException {
71+
for (intbufferLength =1;bufferLength <MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE +1;bufferLength++) {
72+
try (MultipartBodymultipartBody =buildMultipart()) {
73+
longtranferred =transferWithCopy(multipartBody,bufferLength);
74+
assertEquals(tranferred,multipartBody.getContentLength());
75+
}
7876
}
79-
returnfile;
8077
}
8178

82-
privatestaticlongtransferWithCopy(MultipartBodymultipartBody)throwsIOException {
83-
returntransferWithCopy(multipartBody,8192);
79+
@Test
80+
publicvoidtransferZeroCopy()throwsException {
81+
for (intbufferLength =1;bufferLength <MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE +1;bufferLength++) {
82+
try (MultipartBodymultipartBody =buildMultipart()) {
83+
longtranferred =transferZeroCopy(multipartBody,bufferLength);
84+
assertEquals(tranferred,multipartBody.getContentLength());
85+
}
86+
}
8487
}
8588

8689
privatestaticlongtransferWithCopy(MultipartBodymultipartBody,intbufferSize)throwsIOException {
@@ -97,9 +100,9 @@ private static long transferWithCopy(MultipartBody multipartBody, int bufferSize
97100
returntotalBytes;
98101
}
99102

100-
privatestaticlongtransferZeroCopy(MultipartBodymultipartBody)throwsIOException {
103+
privatestaticlongtransferZeroCopy(MultipartBodymultipartBody,intbufferSize)throwsIOException {
101104

102-
finalByteBufferbuffer =ByteBuffer.allocate(8192);
105+
finalByteBufferbuffer =ByteBuffer.allocate(bufferSize);
103106
finalAtomicLongtransferred =newAtomicLong();
104107

105108
WritableByteChannelmockChannel =newWritableByteChannel() {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp