Use try-with-resources instead of try-finally

This commit is contained in:
Lars Grefer 2019-08-06 00:50:47 +02:00 committed by Rob Winch
parent a17d66463d
commit f5cd0ec302
3 changed files with 4 additions and 32 deletions

View File

@ -99,17 +99,8 @@ public class LdapServerBeanDefinitionParserTests {
} }
private int getDefaultPort() throws IOException { private int getDefaultPort() throws IOException {
ServerSocket server = null; try (ServerSocket server = new ServerSocket(0)) {
try {
server = new ServerSocket(0);
return server.getLocalPort(); return server.getLocalPort();
} }
finally {
try {
server.close();
}
catch (IOException e) {
}
}
} }
} }

View File

@ -38,21 +38,13 @@ public class JavaVersionTests {
private void assertClassVersion(Class<?> clazz) throws Exception { private void assertClassVersion(Class<?> clazz) throws Exception {
String classResourceName = clazz.getName().replaceAll("\\.", "/") + ".class"; String classResourceName = clazz.getName().replaceAll("\\.", "/") + ".class";
InputStream input = Thread.currentThread().getContextClassLoader() try (InputStream input = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(classResourceName); .getResourceAsStream(classResourceName)) {
try {
DataInputStream data = new DataInputStream(input); DataInputStream data = new DataInputStream(input);
data.readInt(); data.readInt();
data.readShort(); // minor data.readShort(); // minor
int major = data.readShort(); int major = data.readShort();
assertThat(major).isEqualTo(JDK8_CLASS_VERSION); assertThat(major).isEqualTo(JDK8_CLASS_VERSION);
} }
finally {
try {
input.close();
}
catch (Exception e) {
}
}
} }
} }

View File

@ -114,19 +114,8 @@ public final class ApacheDSServerIntegrationTests {
*/ */
private static int getAvailablePort() throws IOException { private static int getAvailablePort() throws IOException {
ServerSocket serverSocket = null; try (ServerSocket serverSocket = new ServerSocket(0)) {
try {
serverSocket = new ServerSocket(0);
return serverSocket.getLocalPort(); return serverSocket.getLocalPort();
} }
finally {
if (serverSocket != null) {
try {
serverSocket.close();
}
catch (IOException e) {
}
}
}
} }
} }