Polish "Use try-with-resources to close resources automatically"
- Apply code formatting - Use try-with-resources in many other places that were missed in the pull request Closes gh-8045
This commit is contained in:
@@ -51,7 +51,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
*
|
||||
* @author Lari Hotari
|
||||
* @author Phillip Webb
|
||||
* @author rajakolli
|
||||
* @author Raja Kolli
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "endpoints.heapdump")
|
||||
@@ -146,9 +146,9 @@ public class HeapdumpMvcEndpoint extends AbstractNamedMvcEndpoint {
|
||||
response.setHeader("Content-Disposition",
|
||||
"attachment; filename=\"" + (heapDumpFile.getName() + ".gz") + "\"");
|
||||
try (InputStream in = new FileInputStream(heapDumpFile);
|
||||
GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
|
||||
StreamUtils.copy(in, out);
|
||||
out.finish();
|
||||
GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
|
||||
StreamUtils.copy(in, out);
|
||||
out.finish();
|
||||
}
|
||||
catch (NullPointerException | FileNotFoundException ex) {
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -35,15 +35,11 @@ public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Connection connection = this.connectionFactory.createConnection();
|
||||
try {
|
||||
try (Connection connection = this.connectionFactory.createConnection()) {
|
||||
connection.start();
|
||||
builder.up().withDetail("provider",
|
||||
connection.getMetaData().getJMSProviderName());
|
||||
}
|
||||
finally {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ import static org.assertj.core.api.Assertions.offset;
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @author Raja Kolli
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public class CacheStatisticsAutoConfigurationTests {
|
||||
|
||||
@@ -338,9 +338,8 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||
@Test
|
||||
public void specificPortsViaPropertiesWithClash() throws Exception {
|
||||
int managementPort = ports.get().management;
|
||||
ServerSocket serverSocket = new ServerSocket();
|
||||
serverSocket.bind(new InetSocketAddress(managementPort));
|
||||
try {
|
||||
try (ServerSocket serverSocket = new ServerSocket()) {
|
||||
serverSocket.bind(new InetSocketAddress(managementPort));
|
||||
EnvironmentTestUtils.addEnvironment(this.applicationContext,
|
||||
"server.port:" + ports.get().server,
|
||||
"management.port:" + ports.get().management);
|
||||
@@ -350,9 +349,6 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||
this.thrown.expect(WebServerException.class);
|
||||
this.applicationContext.refresh();
|
||||
}
|
||||
finally {
|
||||
serverSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -683,23 +679,17 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||
httpClient);
|
||||
ClientHttpRequest request = requestFactory.createRequest(
|
||||
new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
|
||||
try {
|
||||
ClientHttpResponse response = request.execute();
|
||||
try (ClientHttpResponse response = request.execute()) {
|
||||
if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
try {
|
||||
String actual = StreamUtils.copyToString(response.getBody(),
|
||||
Charset.forName("UTF-8"));
|
||||
if (expected instanceof Matcher) {
|
||||
assertThat(actual).is(Matched.by((Matcher<?>) expected));
|
||||
}
|
||||
else {
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
String actual = StreamUtils.copyToString(response.getBody(),
|
||||
Charset.forName("UTF-8"));
|
||||
if (expected instanceof Matcher) {
|
||||
assertThat(actual).is(Matched.by((Matcher<?>) expected));
|
||||
}
|
||||
finally {
|
||||
response.close();
|
||||
else {
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
||||
@@ -39,9 +39,8 @@ public class TomcatPublicMetricsTests {
|
||||
|
||||
@Test
|
||||
public void tomcatMetrics() throws Exception {
|
||||
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
|
||||
Config.class);
|
||||
try {
|
||||
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
|
||||
Config.class)) {
|
||||
TomcatPublicMetrics tomcatMetrics = context
|
||||
.getBean(TomcatPublicMetrics.class);
|
||||
Iterator<Metric<?>> metrics = tomcatMetrics.metrics().iterator();
|
||||
@@ -49,9 +48,6 @@ public class TomcatPublicMetricsTests {
|
||||
assertThat(metrics.next().getName()).isEqualTo("httpsessions.active");
|
||||
assertThat(metrics.hasNext()).isFalse();
|
||||
}
|
||||
finally {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -62,18 +62,14 @@ public class MetricCopyExporterTests {
|
||||
@Test
|
||||
public void counterWithGaugeWriter() throws Exception {
|
||||
SimpleGaugeWriter writer = new SimpleGaugeWriter();
|
||||
MetricCopyExporter exporter = new MetricCopyExporter(this.reader, writer);
|
||||
try {
|
||||
try (MetricCopyExporter customExporter = new MetricCopyExporter(this.reader, writer)) {
|
||||
this.reader.increment(new Delta<Number>("counter.foo", 2));
|
||||
exporter.export();
|
||||
customExporter.export();
|
||||
this.reader.increment(new Delta<Number>("counter.foo", 3));
|
||||
exporter.export();
|
||||
exporter.flush();
|
||||
customExporter.export();
|
||||
customExporter.flush();
|
||||
assertThat(writer.getValue().getValue()).isEqualTo(5L);
|
||||
}
|
||||
finally {
|
||||
exporter.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user