From 5ba9db391f0cb9cc1212d53bca52cbae8b2c9399 Mon Sep 17 00:00:00 2001 From: weixsun Date: Thu, 13 May 2021 17:06:49 +0800 Subject: [PATCH 1/2] Use try-with-resources statements See gh-26449 --- .../boot/loader/jar/HandlerTests.java | 12 ++---------- .../SampleIntegrationParentApplicationTests.java | 16 ++++------------ 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java index 0c67f89827..0ab36d6e15 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java +++ b/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); URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler); JarURLConnection connection = (JarURLConnection) url.openConnection(); - JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile()); - try { + try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) { assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar); } - finally { - jarFile.close(); - } } @Test @@ -187,13 +183,9 @@ class HandlerTests { TestJarCreator.createTestJar(testJar); URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler); JarURLConnection connection = (JarURLConnection) url.openConnection(); - JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile()); - try { + try (JarFile jarFile = JarFileWrapper.unwrap(connection.getJarFile())) { assertThat(jarFile.getRootJarFile().getFile()).isEqualTo(testJar); } - finally { - jarFile.close(); - } } private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException { diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java index 00557f959d..d40ba6e91f 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java +++ b/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 { File inputDir = new File(temp.toFile(), "input"); File outputDir = new File(temp.toFile(), "output"); - ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class, - "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir); - try { - ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class, - "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World"); - try { + try (ConfigurableApplicationContext app = SpringApplication.run(SampleParentContextApplication.class, + "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir)) { + try (ConfigurableApplicationContext producer = SpringApplication.run(ProducerApplication.class, + "--service.input-dir=" + inputDir, "--service.output-dir=" + outputDir, "World")) { awaitOutputContaining(outputDir, "Hello World"); } - finally { - producer.close(); - } - } - finally { - app.close(); } } From 2dd94b55546aeabfe1fe3fd1212388f29469c6c9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 13 May 2021 11:36:01 +0100 Subject: [PATCH 2/2] Polish "Use try-with-resources statement" See gh-26449 --- .../java/org/springframework/boot/loader/jar/HandlerTests.java | 2 +- .../consumer/SampleIntegrationParentApplicationTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java index 0ab36d6e15..13950475bc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java index d40ba6e91f..b86a674290 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-parent-context/src/test/java/smoketest/parent/consumer/SampleIntegrationParentApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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.