Skip to content

Commit 64aac01

Browse files
committed
Merge pull request spring-projects#26449 from weixsun
* spring-projectsgh-26449: Polish "Use try-with-resources statement" Use try-with-resources statements Closes spring-projectsgh-26449
2 parents 32d378a + 2dd94b5 commit 64aac01

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,13 +172,9 @@ void whenJarHasAPlusInItsPathConnectionJarFileMatchesOriginalJarFile(@TempDir Fi
172172
TestJarCreator.createTestJar(testJar);
173173
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
174174
JarURLConnection connection = (JarURLConnection) url.openConnection();
175-
JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile());
176-
try {
175+
try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) {
177176
assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar);
178177
}
179-
finally {
180-
jarFile.close();
181-
}
182178
}
183179

184180
@Test
@@ -187,13 +183,9 @@ void whenJarHasASpaceInItsPathConnectionJarFileMatchesOriginalJarFile(@TempDir F
187183
TestJarCreator.createTestJar(testJar);
188184
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
189185
JarURLConnection connection = (JarURLConnection) url.openConnection();
190-
JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile());
191-
try {
186+
try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) {
192187
assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar);
193188
}
194-
finally {
195-
jarFile.close();
196-
}
197189
}
198190

199191
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException {

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,20 +49,12 @@ class SampleIntegrationParentApplicationTests {
4949
void testVanillaExchange(@TempDir Path temp) throws Exception {
5050
File inputDir = new File(temp.toFile(), "input");
5151
File outputDir = new File(temp.toFile(), "output");
52-
ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class,
53-
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir);
54-
try {
55-
ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class,
56-
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World");
57-
try {
52+
try (ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class,
53+
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir)) {
54+
try (ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class,
55+
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World")) {
5856
awaitOutputContaining(outputDir, "Hello World");
5957
}
60-
finally {
61-
producer.close();
62-
}
63-
}
64-
finally {
65-
app.close();
6658
}
6759
}
6860

0 commit comments

Comments
 (0)