Use AutoClosables with try-with-resources

Closes gh-33538
This commit is contained in:
Moritz Halbritter
2022-12-16 15:43:15 +01:00
parent 725337f976
commit f36e2ecb7b
16 changed files with 121 additions and 77 deletions

View File

@@ -111,7 +111,7 @@ public class DevToolsDataSourceAutoConfiguration {
DERBY(null, new HashSet<>(Arrays.asList("org.apache.derby.jdbc.EmbeddedDriver")), (dataSource) -> {
String url = dataSource.getConnection().getMetaData().getURL();
try {
new EmbeddedDriver().connect(url + ";drop=true", new Properties());
new EmbeddedDriver().connect(url + ";drop=true", new Properties()).close();
}
catch (SQLException ex) {
if (!"08006".equals(ex.getSQLState())) {

View File

@@ -110,10 +110,11 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
headers.setContentLength(bytes.length);
FileCopyUtils.copy(bytes, request.getBody());
logUpload(event);
ClientHttpResponse response = request.execute();
HttpStatusCode statusCode = response.getStatusCode();
Assert.state(statusCode == HttpStatus.OK,
() -> "Unexpected " + statusCode + " response uploading class files");
try (ClientHttpResponse response = request.execute()) {
HttpStatusCode statusCode = response.getStatusCode();
Assert.state(statusCode == HttpStatus.OK,
() -> "Unexpected " + statusCode + " response uploading class files");
}
return;
}
catch (SocketException ex) {

View File

@@ -103,8 +103,9 @@ class DelayedLiveReloadTrigger implements Runnable {
private boolean isUp() {
try {
ClientHttpRequest request = createRequest();
ClientHttpResponse response = request.execute();
return response.getStatusCode() == HttpStatus.OK;
try (ClientHttpResponse response = request.execute()) {
return response.getStatusCode() == HttpStatus.OK;
}
}
catch (Exception ex) {
return false;

View File

@@ -139,9 +139,10 @@ class HttpTunnelConnectionTests {
@Test
void connectFailureLogsWarning(CapturedOutput output) throws Exception {
this.requestFactory.willRespond(new ConnectException());
TunnelChannel tunnel = openTunnel(true);
assertThat(tunnel.isOpen()).isFalse();
assertThat(output).contains("Failed to connect to remote application at http://localhost:12345");
try (TunnelChannel tunnel = openTunnel(true)) {
assertThat(tunnel.isOpen()).isFalse();
assertThat(output).contains("Failed to connect to remote application at http://localhost:12345");
}
}
private void write(TunnelChannel channel, String string) throws IOException {

View File

@@ -53,34 +53,37 @@ class SocketTargetServerConnectionTests {
void readData() throws Exception {
this.server.willSend("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.allocate(5);
channel.read(buffer);
assertThat(buffer.array()).isEqualTo("hello".getBytes());
try (ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT)) {
ByteBuffer buffer = ByteBuffer.allocate(5);
channel.read(buffer);
assertThat(buffer.array()).isEqualTo("hello".getBytes());
}
}
@Test
void writeData() throws Exception {
this.server.expect("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
try (ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT)) {
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
}
}
@Test
void timeout() throws Exception {
this.server.delay(1000);
this.server.start();
ByteChannel channel = this.connection.open(10);
long startTime = System.currentTimeMillis();
assertThatExceptionOfType(SocketTimeoutException.class).isThrownBy(() -> channel.read(ByteBuffer.allocate(5)))
.satisfies((ex) -> {
long runTime = System.currentTimeMillis() - startTime;
assertThat(runTime).isGreaterThanOrEqualTo(10L);
assertThat(runTime).isLessThan(10000L);
});
try (ByteChannel channel = this.connection.open(10)) {
long startTime = System.currentTimeMillis();
assertThatExceptionOfType(SocketTimeoutException.class)
.isThrownBy(() -> channel.read(ByteBuffer.allocate(5))).satisfies((ex) -> {
long runTime = System.currentTimeMillis() - startTime;
assertThat(runTime).isGreaterThanOrEqualTo(10L);
assertThat(runTime).isLessThan(10000L);
});
}
}
static class MockServer {