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 {
ServerSocket server = null;
try {
server = new ServerSocket(0);
try (ServerSocket server = new ServerSocket(0)) {
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 {
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) {
}
}
}
}

View File

@ -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) {
}
}
}
}
}