Fix compiler warnings in tests
Use try-with-resources, also avoids possible leaks on test failures
This commit is contained in:
parent
230a2caf67
commit
290ec22022
|
@ -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
|
||||
|
|
|
@ -279,9 +279,10 @@ public class TestProtocolRecommendations {
|
|||
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
|
||||
|
|
|
@ -80,15 +80,16 @@ public class TestDecompressingEntity {
|
|||
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 {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -96,10 +96,11 @@ public class TestConnectExec {
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue