Use try-with-resources instead of try-finally
This commit is contained in:
parent
a17d66463d
commit
f5cd0ec302
|
@ -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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue