Use try-with-resources language construct where feasible

Closes gh-2063

Co-authored-by: igor-suhorukov <igor.suhorukov@gmail.com>
This commit is contained in:
Sam Brannen
2020-06-16 22:57:45 +02:00
parent 456d2c46e3
commit 8099fc8178
23 changed files with 179 additions and 394 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,13 +71,9 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
assertThat(request.getMethod()).as("Invalid HTTP method").isEqualTo(HttpMethod.GET);
assertThat(request.getURI()).as("Invalid HTTP URI").isEqualTo(uri);
Future<ClientHttpResponse> futureResponse = request.executeAsync();
ClientHttpResponse response = futureResponse.get();
try {
try (ClientHttpResponse response = futureResponse.get()) {
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.NOT_FOUND);
}
finally {
response.close();
}
}
@Test
@@ -102,13 +98,9 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
throw new AssertionError(ex.getMessage(), ex);
}
});
ClientHttpResponse response = listenableFuture.get();
try {
try (ClientHttpResponse response = listenableFuture.get()) {
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.NOT_FOUND);
}
finally {
response.close();
}
}
@Test
@@ -132,20 +124,17 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
ClientHttpResponse response = futureResponse.get();
try {
try ( ClientHttpResponse response = futureResponse.get()) {
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().containsKey(headerName)).as("Header not found").isTrue();
assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2));
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
assertThat(Arrays.equals(body, result)).as("Invalid body").isTrue();
}
finally {
response.close();
}
}
@Test
@SuppressWarnings("try")
public void multipleWrites() throws Exception {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
final byte[] body = "Hello World".getBytes("UTF-8");
@@ -159,17 +148,13 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
ClientHttpResponse response = futureResponse.get();
try {
assertThatIllegalStateException().isThrownBy(() ->
FileCopyUtils.copy(body, request.getBody()));
}
finally {
response.close();
try (ClientHttpResponse response = futureResponse.get()) {
assertThatIllegalStateException().isThrownBy(() -> FileCopyUtils.copy(body, request.getBody()));
}
}
@Test
@SuppressWarnings("try")
public void headersAfterExecute() throws Exception {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value");
@@ -177,14 +162,10 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
FileCopyUtils.copy(body, request.getBody());
Future<ClientHttpResponse> futureResponse = request.executeAsync();
ClientHttpResponse response = futureResponse.get();
try {
try (ClientHttpResponse response = futureResponse.get()) {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
request.getHeaders().add("MyHeader", "value"));
}
finally {
response.close();
}
}
@Test
@@ -198,23 +179,16 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
request.getBody().write(32);
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
response = futureResponse.get();
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
request.getBody().write(32);
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
try (ClientHttpResponse response = futureResponse.get()) {
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
assertThat(request.getMethod().name()).as("Invalid method").isEqualTo(path.toUpperCase(Locale.ENGLISH));
}
finally {
if (response != null) {
response.close();
}
}
}
@Test
@@ -226,5 +200,4 @@ public abstract class AbstractAsyncHttpRequestFactoryTests extends AbstractMockW
assertThat(futureResponse.isCancelled()).isTrue();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,13 +40,13 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* @author Arjen Poutsma
*/
public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebServerTests {
abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebServerTests {
protected ClientHttpRequestFactory factory;
@BeforeEach
public final void createFactory() throws Exception {
final void createFactory() throws Exception {
factory = createRequestFactory();
if (factory instanceof InitializingBean) {
((InitializingBean) factory).afterPropertiesSet();
@@ -54,7 +54,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
}
@AfterEach
public final void destroyFactory() throws Exception {
final void destroyFactory() throws Exception {
if (factory instanceof DisposableBean) {
((DisposableBean) factory).destroy();
}
@@ -65,7 +65,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
@Test
public void status() throws Exception {
void status() throws Exception {
URI uri = new URI(baseUrl + "/status/notfound");
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);
assertThat(request.getMethod()).as("Invalid HTTP method").isEqualTo(HttpMethod.GET);
@@ -77,7 +77,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
}
@Test
public void echo() throws Exception {
void echo() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
assertThat(request.getMethod()).as("Invalid HTTP method").isEqualTo(HttpMethod.PUT);
@@ -107,7 +107,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
}
@Test
public void multipleWrites() throws Exception {
void multipleWrites() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
final byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8);
@@ -130,7 +130,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
@Test
@SuppressWarnings("try")
public void headersAfterExecute() throws Exception {
void headersAfterExecute() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value");
@@ -144,7 +144,7 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
}
@Test
public void httpMethods() throws Exception {
void httpMethods() throws Exception {
assertHttpMethod("get", HttpMethod.GET);
assertHttpMethod("head", HttpMethod.HEAD);
assertHttpMethod("post", HttpMethod.POST);
@@ -154,31 +154,25 @@ public abstract class AbstractHttpRequestFactoryTests extends AbstractMockWebSer
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
try {
request.getBody().write(32);
}
catch (UnsupportedOperationException ex) {
// probably a streaming request - let's simply ignore it
}
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
try {
request.getBody().write(32);
}
response = request.execute();
catch (UnsupportedOperationException ex) {
// probably a streaming request - let's simply ignore it
}
}
try (ClientHttpResponse response = request.execute()) {
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
assertThat(request.getMethod().name()).as("Invalid method").isEqualTo(path.toUpperCase(Locale.ENGLISH));
}
finally {
if (response != null) {
response.close();
}
}
}
@Test
public void queryParameters() throws Exception {
void queryParameters() throws Exception {
URI uri = new URI(baseUrl + "/params?param1=value&param2=value1&param2=value2");
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
public class BufferingClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
class BufferingClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
@Override
protected ClientHttpRequestFactory createRequestFactory() {
@@ -35,7 +35,7 @@ public class BufferingClientHttpRequestFactoryTests extends AbstractHttpRequestF
}
@Test
public void repeatableRead() throws Exception {
void repeatableRead() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
assertThat(request.getMethod()).as("Invalid HTTP method").isEqualTo(HttpMethod.PUT);
String headerName = "MyHeader";
@@ -46,8 +46,7 @@ public class BufferingClientHttpRequestFactoryTests extends AbstractHttpRequestF
byte[] body = "Hello World".getBytes("UTF-8");
request.getHeaders().setContentLength(body.length);
FileCopyUtils.copy(body, request.getBody());
ClientHttpResponse response = request.execute();
try {
try (ClientHttpResponse response = request.execute()) {
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
@@ -62,9 +61,6 @@ public class BufferingClientHttpRequestFactoryTests extends AbstractHttpRequestF
FileCopyUtils.copyToByteArray(response.getBody());
assertThat(Arrays.equals(body, result)).as("Invalid body").isTrue();
}
finally {
response.close();
}
}
}