2929import java .io .InputStream ;
3030import java .io .OutputStream ;
3131import java .io .UncheckedIOException ;
32+ import java .lang .ref .ReferenceQueue ;
33+ import java .lang .ref .WeakReference ;
3234import java .net .InetAddress ;
3335import java .net .InetSocketAddress ;
3436import java .net .URI ;
7779 * @run testng/othervm AsFileDownloadTest
7880 * @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
7981 */
80-
8182public class AsFileDownloadTest {
8283
8384 SSLContext sslContext ;
@@ -89,6 +90,7 @@ public class AsFileDownloadTest {
8990 String httpsURI ;
9091 String http2URI ;
9192 String https2URI ;
93+ final ReferenceTracker TRACKER = ReferenceTracker .INSTANCE ;
9294
9395 Path tempDir ;
9496
@@ -164,37 +166,49 @@ void test(String uriString, String contentDispositionValue, String expectedFilen
164166 {
165167 out .printf ("test(%s, %s, %s): starting" , uriString , contentDispositionValue , expectedFilename );
166168 HttpClient client = HttpClient .newBuilder ().sslContext (sslContext ).build ();
169+ TRACKER .track (client );
170+ ReferenceQueue <HttpClient > queue = new ReferenceQueue <>();
171+ WeakReference <HttpClient > ref = new WeakReference <>(client , queue );
172+ try {
173+ URI uri = URI .create (uriString );
174+ HttpRequest request = HttpRequest .newBuilder (uri )
175+ .POST (BodyPublishers .ofString ("May the luck of the Irish be with you!" ))
176+ .build ();
167177
168- URI uri = URI .create (uriString );
169- HttpRequest request = HttpRequest .newBuilder (uri )
170- .POST (BodyPublishers .ofString ("May the luck of the Irish be with you!" ))
171- .build ();
172-
173- BodyHandler bh = ofFileDownload (tempDir .resolve (uri .getPath ().substring (1 )),
174- CREATE , TRUNCATE_EXISTING , WRITE );
175- HttpResponse <Path > response = client .send (request , bh );
176-
177- Path body = response .body ();
178- out .println ("Got response: " + response );
179- out .println ("Got body Path: " + body );
180- String fileContents = new String (Files .readAllBytes (response .body ()), UTF_8 );
181- out .println ("Got body: " + fileContents );
182-
183- assertEquals (response .statusCode (),200 );
184- assertEquals (body .getFileName ().toString (), expectedFilename );
185- assertTrue (response .headers ().firstValue ("Content-Disposition" ).isPresent ());
186- assertEquals (response .headers ().firstValue ("Content-Disposition" ).get (),
187- contentDispositionValue );
188- assertEquals (fileContents , "May the luck of the Irish be with you!" );
189-
190- if (!body .toAbsolutePath ().startsWith (tempDir .toAbsolutePath ())) {
191- System .out .println ("Tempdir = " + tempDir .toAbsolutePath ());
192- System .out .println ("body = " + body .toAbsolutePath ());
193- throw new AssertionError ("body in wrong location" );
178+ BodyHandler bh = ofFileDownload (tempDir .resolve (uri .getPath ().substring (1 )),
179+ CREATE , TRUNCATE_EXISTING , WRITE );
180+ HttpResponse <Path > response = client .send (request , bh );
181+ Path body = response .body ();
182+ out .println ("Got response: " + response );
183+ out .println ("Got body Path: " + body );
184+ String fileContents = new String (Files .readAllBytes (response .body ()), UTF_8 );
185+ out .println ("Got body: " + fileContents );
186+
187+ assertEquals (response .statusCode (), 200 );
188+ assertEquals (body .getFileName ().toString (), expectedFilename );
189+ assertTrue (response .headers ().firstValue ("Content-Disposition" ).isPresent ());
190+ assertEquals (response .headers ().firstValue ("Content-Disposition" ).get (),
191+ contentDispositionValue );
192+ assertEquals (fileContents , "May the luck of the Irish be with you!" );
193+
194+ if (!body .toAbsolutePath ().startsWith (tempDir .toAbsolutePath ())) {
195+ System .out .println ("Tempdir = " + tempDir .toAbsolutePath ());
196+ System .out .println ("body = " + body .toAbsolutePath ());
197+ throw new AssertionError ("body in wrong location" );
198+ }
199+ // additional checks unrelated to file download
200+ caseInsensitivityOfHeaders (request .headers ());
201+ caseInsensitivityOfHeaders (response .headers ());
202+ } finally {
203+ client = null ;
204+ System .gc ();
205+ while (!ref .refersTo (null )) {
206+ System .gc ();
207+ if (queue .remove (100 ) == ref ) break ;
208+ }
209+ AssertionError failed = TRACKER .checkShutdown (1000 );
210+ if (failed != null ) throw failed ;
194211 }
195- // additional checks unrelated to file download
196- caseInsensitivityOfHeaders (request .headers ());
197- caseInsensitivityOfHeaders (response .headers ());
198212 }
199213
200214 // --- Negative
@@ -246,18 +260,32 @@ void negativeTest(String uriString, String contentDispositionValue)
246260 {
247261 out .printf ("negativeTest(%s, %s): starting" , uriString , contentDispositionValue );
248262 HttpClient client = HttpClient .newBuilder ().sslContext (sslContext ).build ();
263+ TRACKER .track (client );
264+ ReferenceQueue <HttpClient > queue = new ReferenceQueue <>();
265+ WeakReference <HttpClient > ref = new WeakReference <>(client , queue );
249266
250- URI uri = URI .create (uriString );
251- HttpRequest request = HttpRequest .newBuilder (uri )
252- .POST (BodyPublishers .ofString ("Does not matter" ))
253- .build ();
254-
255- BodyHandler bh = ofFileDownload (tempDir , CREATE , TRUNCATE_EXISTING , WRITE );
256267 try {
257- HttpResponse <Path > response = client .send (request , bh );
258- fail ("UNEXPECTED response: " + response + ", path:" + response .body ());
259- } catch (UncheckedIOException | IOException ioe ) {
260- System .out .println ("Caught expected: " + ioe );
268+ URI uri = URI .create (uriString );
269+ HttpRequest request = HttpRequest .newBuilder (uri )
270+ .POST (BodyPublishers .ofString ("Does not matter" ))
271+ .build ();
272+
273+ BodyHandler bh = ofFileDownload (tempDir , CREATE , TRUNCATE_EXISTING , WRITE );
274+ try {
275+ HttpResponse <Path > response = client .send (request , bh );
276+ fail ("UNEXPECTED response: " + response + ", path:" + response .body ());
277+ } catch (UncheckedIOException | IOException ioe ) {
278+ System .out .println ("Caught expected: " + ioe );
279+ }
280+ } finally {
281+ client = null ;
282+ System .gc ();
283+ while (!ref .refersTo (null )) {
284+ System .gc ();
285+ if (queue .remove (100 ) == ref ) break ;
286+ }
287+ AssertionError failed = TRACKER .checkShutdown (1000 );
288+ if (failed != null ) throw failed ;
261289 }
262290 }
263291
0 commit comments