From 290ec22022dca1ece5a05268926c23074fb603aa Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 14 Nov 2022 11:42:46 -0500 Subject: [PATCH] Fix compiler warnings in tests Use try-with-resources, also avoids possible leaks on test failures --- .../cache/ManagedHttpCacheStorageTest.java | 11 +++--- .../cache/TestProtocolRecommendations.java | 7 ++-- .../http/entity/TestDecompressingEntity.java | 15 ++++---- .../hc/client5/http/entity/TestGZip.java | 37 ++++++++++--------- .../http/impl/classic/TestConnectExec.java | 9 +++-- 5 files changed, 43 insertions(+), 36 deletions(-) diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/cache/ManagedHttpCacheStorageTest.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/cache/ManagedHttpCacheStorageTest.java index efcab7a37f..34e3c51d74 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/cache/ManagedHttpCacheStorageTest.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/cache/ManagedHttpCacheStorageTest.java @@ -44,11 +44,12 @@ class ManagedHttpCacheStorageTest { void putEntry() throws ResourceIOException { final CacheConfig cacheConfig = getCacheConfig(); - final ManagedHttpCacheStorage cacheStorage = new ManagedHttpCacheStorage(cacheConfig); - final String key = "foo"; - final HttpCacheEntry value = HttpTestUtils.makeCacheEntry(); - cacheStorage.putEntry(key, value); - assertEquals(HttpStatus.SC_OK, cacheStorage.getEntry(key).getStatus()); + try (final ManagedHttpCacheStorage cacheStorage = new ManagedHttpCacheStorage(cacheConfig)) { + final String key = "foo"; + final HttpCacheEntry value = HttpTestUtils.makeCacheEntry(); + cacheStorage.putEntry(key, value); + assertEquals(HttpStatus.SC_OK, cacheStorage.getEntry(key).getStatus()); + } } @Test diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRecommendations.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRecommendations.java index c409b14043..617f28c8d0 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRecommendations.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRecommendations.java @@ -279,9 +279,10 @@ private void cacheGenerated304ForStrongValidatorShouldNotContainContentRange( req2.setHeader("Range","bytes=0-127"); req2.setHeader(conditionalHeader, validator); - final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified"); - resp2.setHeader("Date", DateUtils.formatStandardDate(now)); - resp2.setHeader(validatorHeader, validator); + try (final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified")) { + resp2.setHeader("Date", DateUtils.formatStandardDate(now)); + resp2.setHeader(validatorHeader, validator); + } // cache module does not currently deal with byte ranges, but we want // this test to work even if it does some day diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestDecompressingEntity.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestDecompressingEntity.java index 5662aca0f5..cc439405ed 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestDecompressingEntity.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestDecompressingEntity.java @@ -80,15 +80,16 @@ public void testStreaming() throws Exception { public void testWriteToStream() throws Exception { final CRC32 crc32 = new CRC32(); final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII); - final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32); - Assertions.assertFalse(entity.isStreaming()); + try (final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32)) { + Assertions.assertFalse(entity.isStreaming()); - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - entity.writeTo(out); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + entity.writeTo(out); - final String s = new String(out.toByteArray(), StandardCharsets.US_ASCII); - Assertions.assertEquals("1234567890", s); - Assertions.assertEquals(639479525L, crc32.getValue()); + final String s = new String(out.toByteArray(), StandardCharsets.US_ASCII); + Assertions.assertEquals("1234567890", s); + Assertions.assertEquals(639479525L, crc32.getValue()); + } } static class ChecksumEntity extends DecompressingEntity { diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestGZip.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestGZip.java index 3fa7adcc4d..786c8a0e02 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestGZip.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/TestGZip.java @@ -50,34 +50,37 @@ public class TestGZip { public void testBasic() throws Exception { final String s = "some kind of text"; final StringEntity e = new StringEntity(s, ContentType.TEXT_PLAIN, false); - final GzipCompressingEntity gzipe = new GzipCompressingEntity(e); - Assertions.assertTrue(gzipe.isChunked()); - Assertions.assertEquals(-1, gzipe.getContentLength()); - Assertions.assertNotNull(gzipe.getContentEncoding()); - Assertions.assertEquals("gzip", gzipe.getContentEncoding()); + try (final GzipCompressingEntity gzipe = new GzipCompressingEntity(e)) { + Assertions.assertTrue(gzipe.isChunked()); + Assertions.assertEquals(-1, gzipe.getContentLength()); + Assertions.assertNotNull(gzipe.getContentEncoding()); + Assertions.assertEquals("gzip", gzipe.getContentEncoding()); + } } @Test public void testCompressionDecompression() throws Exception { final StringEntity in = new StringEntity("some kind of text", ContentType.TEXT_PLAIN); - final GzipCompressingEntity gzipe = new GzipCompressingEntity(in); - final ByteArrayOutputStream buf = new ByteArrayOutputStream(); - gzipe.writeTo(buf); - final ByteArrayEntity out = new ByteArrayEntity(buf.toByteArray(), ContentType.APPLICATION_OCTET_STREAM); - final GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(out); - Assertions.assertEquals("some kind of text", EntityUtils.toString(gunzipe, StandardCharsets.US_ASCII)); + try (final GzipCompressingEntity gzipe = new GzipCompressingEntity(in)) { + final ByteArrayOutputStream buf = new ByteArrayOutputStream(); + gzipe.writeTo(buf); + final ByteArrayEntity out = new ByteArrayEntity(buf.toByteArray(), ContentType.APPLICATION_OCTET_STREAM); + final GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(out); + Assertions.assertEquals("some kind of text", EntityUtils.toString(gunzipe, StandardCharsets.US_ASCII)); + } } @Test public void testCompressionIOExceptionLeavesOutputStreamOpen() throws Exception { final HttpEntity in = Mockito.mock(HttpEntity.class); Mockito.doThrow(new IOException("Ooopsie")).when(in).writeTo(ArgumentMatchers.any()); - final GzipCompressingEntity gzipe = new GzipCompressingEntity(in); - final OutputStream out = Mockito.mock(OutputStream.class); - try { - gzipe.writeTo(out); - } catch (final IOException ex) { - Mockito.verify(out, Mockito.never()).close(); + try (final GzipCompressingEntity gzipe = new GzipCompressingEntity(in)) { + final OutputStream out = Mockito.mock(OutputStream.class); + try { + gzipe.writeTo(out); + } catch (final IOException ex) { + Mockito.verify(out, Mockito.never()).close(); + } } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestConnectExec.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestConnectExec.java index 2ebb280bc9..e9b9db15d3 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestConnectExec.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestConnectExec.java @@ -96,10 +96,11 @@ public void testExecAcquireConnection() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpClientContext context = new HttpClientContext(); final ClassicHttpRequest request = new HttpGet("http://bar/test"); - final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK"); - response.setEntity(EntityBuilder.create() - .setStream(new ByteArrayInputStream(new byte[]{})) - .build()); + try (final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK")) { + response.setEntity(EntityBuilder.create() + .setStream(new ByteArrayInputStream(new byte[]{})) + .build()); + } context.setUserToken("Blah"); Mockito.when(execRuntime.isEndpointAcquired()).thenReturn(false);