From f5cd0ec302a8c0fca515f3e4b6d7d014129ec3de Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Tue, 6 Aug 2019 00:50:47 +0200 Subject: [PATCH] Use try-with-resources instead of try-finally --- .../ldap/LdapServerBeanDefinitionParserTests.java | 11 +---------- .../security/core/JavaVersionTests.java | 12 ++---------- .../ldap/ApacheDSServerIntegrationTests.java | 13 +------------ 3 files changed, 4 insertions(+), 32 deletions(-) diff --git a/config/src/integration-test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTests.java b/config/src/integration-test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTests.java index 52a9400f89..9b2c07cb17 100644 --- a/config/src/integration-test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTests.java +++ b/config/src/integration-test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTests.java @@ -99,17 +99,8 @@ public class LdapServerBeanDefinitionParserTests { } private int getDefaultPort() throws IOException { - ServerSocket server = null; - try { - server = new ServerSocket(0); + try (ServerSocket server = new ServerSocket(0)) { return server.getLocalPort(); } - finally { - try { - server.close(); - } - catch (IOException e) { - } - } } } diff --git a/core/src/test/java/org/springframework/security/core/JavaVersionTests.java b/core/src/test/java/org/springframework/security/core/JavaVersionTests.java index b0faae75d0..f7a6ab7057 100644 --- a/core/src/test/java/org/springframework/security/core/JavaVersionTests.java +++ b/core/src/test/java/org/springframework/security/core/JavaVersionTests.java @@ -38,21 +38,13 @@ public class JavaVersionTests { private void assertClassVersion(Class clazz) throws Exception { String classResourceName = clazz.getName().replaceAll("\\.", "/") + ".class"; - InputStream input = Thread.currentThread().getContextClassLoader() - .getResourceAsStream(classResourceName); - try { + try (InputStream input = Thread.currentThread().getContextClassLoader() + .getResourceAsStream(classResourceName)) { DataInputStream data = new DataInputStream(input); data.readInt(); data.readShort(); // minor int major = data.readShort(); assertThat(major).isEqualTo(JDK8_CLASS_VERSION); } - finally { - try { - input.close(); - } - catch (Exception e) { - } - } } } diff --git a/ldap/src/integration-test/java/org/springframework/security/ldap/ApacheDSServerIntegrationTests.java b/ldap/src/integration-test/java/org/springframework/security/ldap/ApacheDSServerIntegrationTests.java index 7a8bfdc6d8..501b7439d6 100644 --- a/ldap/src/integration-test/java/org/springframework/security/ldap/ApacheDSServerIntegrationTests.java +++ b/ldap/src/integration-test/java/org/springframework/security/ldap/ApacheDSServerIntegrationTests.java @@ -114,19 +114,8 @@ public final class ApacheDSServerIntegrationTests { */ private static int getAvailablePort() throws IOException { - ServerSocket serverSocket = null; - try { - serverSocket = new ServerSocket(0); + try (ServerSocket serverSocket = new ServerSocket(0)) { return serverSocket.getLocalPort(); } - finally { - if (serverSocket != null) { - try { - serverSocket.close(); - } - catch (IOException e) { - } - } - } } }