11package org .baeldung .httpclient ;
22
3- import static org .hamcrest .Matchers .equalTo ;
4- import static org .junit .Assert .assertThat ;
5- import static org .junit .Assert .assertTrue ;
6-
7- import java .io .BufferedReader ;
8- import java .io .File ;
9- import java .io .FileInputStream ;
10- import java .io .IOException ;
11- import java .io .InputStream ;
12- import java .io .InputStreamReader ;
13- import java .net .URL ;
14- import java .util .logging .Level ;
15- import java .util .logging .Logger ;
16-
173import org .apache .http .HttpEntity ;
184import org .apache .http .HttpStatus ;
19- import org .apache .http .client .ClientProtocolException ;
205import org .apache .http .client .methods .CloseableHttpResponse ;
216import org .apache .http .client .methods .HttpPost ;
227import org .apache .http .entity .ContentType ;
3015import org .junit .Before ;
3116import org .junit .Test ;
3217
18+ import java .io .BufferedReader ;
19+ import java .io .File ;
20+ import java .io .FileInputStream ;
21+ import java .io .IOException ;
22+ import java .io .InputStream ;
23+ import java .io .InputStreamReader ;
24+ import java .net .URL ;
25+ import java .util .logging .Level ;
26+ import java .util .logging .Logger ;
27+
28+ import static org .hamcrest .Matchers .equalTo ;
29+ import static org .junit .Assert .assertThat ;
30+ import static org .junit .Assert .assertTrue ;
31+
3332public class HttpClientMultipartLiveTest {
3433
3534 // No longer available
@@ -48,7 +47,7 @@ public class HttpClientMultipartLiveTest {
4847 @ Before
4948 public final void before () {
5049 client = HttpClientBuilder .create ()
51- .build ();
50+ .build ();
5251 post = new HttpPost (SERVER );
5352 }
5453
@@ -67,24 +66,16 @@ public final void after() throws IllegalStateException, IOException {
6766 LOGGER .log (Level .SEVERE , e .getMessage (), e );
6867 throw e ;
6968 }
70- try {
71- final HttpEntity entity = response .getEntity ();
72- if (entity != null ) {
73- final InputStream instream = entity .getContent ();
74- instream .close ();
75- }
76- } finally {
77- response .close ();
78- }
69+ ResponseUtil .closeResponse (response );
7970 }
8071
8172 // tests
8273
8374 @ Test
8475 public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions () throws IOException {
8576 final URL url = Thread .currentThread ()
86- .getContextClassLoader ()
87- .getResource ("uploads/" + TEXTFILENAME );
77+ .getContextClassLoader ()
78+ .getResource ("uploads/" + TEXTFILENAME );
8879
8980 final File file = new File (url .getPath ());
9081 final FileBody fileBody = new FileBody (file , ContentType .DEFAULT_BINARY );
@@ -102,7 +93,7 @@ public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExce
10293 response = client .execute (post );
10394
10495 final int statusCode = response .getStatusLine ()
105- .getStatusCode ();
96+ .getStatusCode ();
10697 final String responseString = getContent ();
10798 final String contentTypeInHeader = getContentTypeHeader ();
10899 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -113,10 +104,10 @@ public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExce
113104 }
114105
115106 @ Test
116- public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption () throws ClientProtocolException , IOException {
107+ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption () throws IOException {
117108 final URL url = Thread .currentThread ()
118- .getContextClassLoader ()
119- .getResource ("uploads/" + TEXTFILENAME );
109+ .getContextClassLoader ()
110+ .getResource ("uploads/" + TEXTFILENAME );
120111 final File file = new File (url .getPath ());
121112 final String message = "This is a multipart post" ;
122113 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -127,7 +118,7 @@ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody
127118 post .setEntity (entity );
128119 response = client .execute (post );
129120 final int statusCode = response .getStatusLine ()
130- .getStatusCode ();
121+ .getStatusCode ();
131122 final String responseString = getContent ();
132123 final String contentTypeInHeader = getContentTypeHeader ();
133124 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -138,13 +129,13 @@ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody
138129 }
139130
140131 @ Test
141- public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws ClientProtocolException , IOException {
132+ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws IOException {
142133 final URL url = Thread .currentThread ()
143- .getContextClassLoader ()
144- .getResource ("uploads/" + ZIPFILENAME );
134+ .getContextClassLoader ()
135+ .getResource ("uploads/" + ZIPFILENAME );
145136 final URL url2 = Thread .currentThread ()
146- .getContextClassLoader ()
147- .getResource ("uploads/" + IMAGEFILENAME );
137+ .getContextClassLoader ()
138+ .getResource ("uploads/" + IMAGEFILENAME );
148139 final InputStream inputStream = new FileInputStream (url .getPath ());
149140 final File file = new File (url2 .getPath ());
150141 final String message = "This is a multipart post" ;
@@ -157,7 +148,7 @@ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandA
157148 post .setEntity (entity );
158149 response = client .execute (post );
159150 final int statusCode = response .getStatusLine ()
160- .getStatusCode ();
151+ .getStatusCode ();
161152 final String responseString = getContent ();
162153 final String contentTypeInHeader = getContentTypeHeader ();
163154 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -169,7 +160,7 @@ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandA
169160 }
170161
171162 @ Test
172- public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws ClientProtocolException , IOException {
163+ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws IOException {
173164 final String message = "This is a multipart post" ;
174165 final byte [] bytes = "binary code" .getBytes ();
175166 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -180,7 +171,7 @@ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBod
180171 post .setEntity (entity );
181172 response = client .execute (post );
182173 final int statusCode = response .getStatusLine ()
183- .getStatusCode ();
174+ .getStatusCode ();
184175 final String responseString = getContent ();
185176 final String contentTypeInHeader = getContentTypeHeader ();
186177 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -192,21 +183,21 @@ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBod
192183
193184 // UTIL
194185
195- final String getContent () throws IOException {
186+ private String getContent () throws IOException {
196187 rd = new BufferedReader (new InputStreamReader (response .getEntity ()
197- .getContent ()));
188+ .getContent ()));
198189 String body = "" ;
199- String content = "" ;
190+ StringBuilder content = new StringBuilder () ;
200191 while ((body = rd .readLine ()) != null ) {
201- content += body + "\n " ;
192+ content . append ( body ). append ( "\n " ) ;
202193 }
203- return content .trim ();
194+ return content .toString (). trim ();
204195 }
205196
206- final String getContentTypeHeader () throws IOException {
197+ private String getContentTypeHeader () throws IOException {
207198 return post .getEntity ()
208- .getContentType ()
209- .toString ();
199+ .getContentType ()
200+ .toString ();
210201 }
211202
212203}
0 commit comments