Skip to content

Commit

Permalink
Fix compiler warnings in tests
Browse files Browse the repository at this point in the history
Use try-with-resources, also avoids possible leaks on test failures
  • Loading branch information
garydgregory authored and ok2c committed Nov 14, 2022
1 parent 230a2ca commit 290ec22
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 290ec22

Please sign in to comment.