Use AutoClosables with try-with-resources
Closes gh-33538
This commit is contained in:
@@ -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())) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user