Browse Source

Use try-with-resources statements

See gh-26449
pull/27078/head
weixsun 5 years ago committed by Andy Wilkinson
parent
commit
5ba9db391f
  1. 12
      spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java
  2. 16
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java

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

@ -172,13 +172,9 @@ class HandlerTests {
TestJarCreator.createTestJar(testJar); TestJarCreator.createTestJar(testJar);
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler); URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
JarURLConnection connection = (JarURLConnection) url.openConnection(); JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile()); try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) {
try {
assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar); assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar);
} }
finally {
jarFile.close();
}
} }
@Test @Test
@ -187,13 +183,9 @@ class HandlerTests {
TestJarCreator.createTestJar(testJar); TestJarCreator.createTestJar(testJar);
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler); URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
JarURLConnection connection = (JarURLConnection) url.openConnection(); JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile()); try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) {
try {
assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar); assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar);
} }
finally {
jarFile.close();
}
} }
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException { private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException {

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

@ -49,20 +49,12 @@ class SampleIntegrationParentApplicationTests {
void testVanillaExchange(@TempDir Path temp) throws Exception { void testVanillaExchange(@TempDir Path temp) throws Exception {
File inputDir = new File(temp.toFile(), "input"); File inputDir = new File(temp.toFile(), "input");
File outputDir = new File(temp.toFile(), "output"); File outputDir = new File(temp.toFile(), "output");
ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class, try (ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class,
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir); "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir)) {
try { try (ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class,
ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class, "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World")) {
"--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World");
try {
awaitOutputContaining(outputDir, "Hello World"); awaitOutputContaining(outputDir, "Hello World");
} }
finally {
producer.close();
}
}
finally {
app.close();
} }
} }

Loading…
Cancel
Save