14
14
package com .ning .http .client .multipart ;
15
15
16
16
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 .*;
22
18
23
19
import java .io .File ;
24
20
import java .io .IOException ;
30
26
import java .util .List ;
31
27
import java .util .concurrent .atomic .AtomicLong ;
32
28
29
+ import org .testng .annotations .Test ;
30
+
31
+ import com .ning .http .client .FluentCaseInsensitiveStringsMap ;
32
+
33
33
public class MultipartBodyTest {
34
34
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 );
40
42
}
43
+ PARTS .add (new ByteArrayPart ("baPart" , "testMultiPart" .getBytes (UTF_8 ), "application/test" , UTF_8 , "fileName" ));
44
+ PARTS .add (new StringPart ("stringPart" , "testString" ));
41
45
}
42
46
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 ());
51
52
}
52
53
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 );
58
62
}
59
63
}
60
64
61
65
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 ());
67
67
}
68
68
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
+ }
78
76
}
79
- return file ;
80
77
}
81
78
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
+ }
84
87
}
85
88
86
89
private static long transferWithCopy (MultipartBody multipartBody , int bufferSize ) throws IOException {
@@ -97,9 +100,9 @@ private static long transferWithCopy(MultipartBody multipartBody, int bufferSize
97
100
return totalBytes ;
98
101
}
99
102
100
- private static long transferZeroCopy (MultipartBody multipartBody ) throws IOException {
103
+ private static long transferZeroCopy (MultipartBody multipartBody , int bufferSize ) throws IOException {
101
104
102
- final ByteBuffer buffer = ByteBuffer .allocate (8192 );
105
+ final ByteBuffer buffer = ByteBuffer .allocate (bufferSize );
103
106
final AtomicLong transferred = new AtomicLong ();
104
107
105
108
WritableByteChannel mockChannel = new WritableByteChannel () {
0 commit comments