Use try-with-resources language construct where feasible
Closes gh-2063 Co-authored-by: igor-suhorukov <igor.suhorukov@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -291,19 +291,12 @@ class PathResourceTests {
|
||||
@Test
|
||||
void getReadableByteChannel() throws IOException {
|
||||
PathResource resource = new PathResource(TEST_FILE);
|
||||
ReadableByteChannel channel = null;
|
||||
try {
|
||||
channel = resource.readableChannel();
|
||||
try (ReadableByteChannel channel = resource.readableChannel()) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength());
|
||||
channel.read(buffer);
|
||||
buffer.rewind();
|
||||
assertThat(buffer.limit()).isGreaterThan(0);
|
||||
}
|
||||
finally {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -330,16 +323,9 @@ class PathResourceTests {
|
||||
Files.createFile(testPath);
|
||||
PathResource resource = new PathResource(testPath);
|
||||
ByteBuffer buffer = ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8));
|
||||
WritableByteChannel channel = null;
|
||||
try {
|
||||
channel = resource.writableChannel();
|
||||
try (WritableByteChannel channel = resource.writableChannel()) {
|
||||
channel.write(buffer);
|
||||
}
|
||||
finally {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
assertThat(resource.contentLength()).isEqualTo(4L);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -298,19 +298,12 @@ class ResourceTests {
|
||||
@Test
|
||||
void readableChannel() throws IOException {
|
||||
Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile());
|
||||
ReadableByteChannel channel = null;
|
||||
try {
|
||||
channel = resource.readableChannel();
|
||||
try (ReadableByteChannel channel = resource.readableChannel()) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength());
|
||||
channel.read(buffer);
|
||||
buffer.rewind();
|
||||
assertThat(buffer.limit() > 0).isTrue();
|
||||
}
|
||||
finally {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -346,17 +346,12 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
|
||||
byte[] bytes = {'a', 'b', 'c'};
|
||||
buffer.write(bytes);
|
||||
|
||||
InputStream inputStream = buffer.asInputStream(true);
|
||||
|
||||
try {
|
||||
try (InputStream inputStream = buffer.asInputStream(true)) {
|
||||
byte[] result = new byte[3];
|
||||
int len = inputStream.read(result);
|
||||
assertThat(len).isEqualTo(3);
|
||||
assertThat(result).isEqualTo(bytes);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
|
||||
// AbstractDataBufferAllocatingTests.leakDetector will verify the buffer's release
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -75,19 +75,16 @@ class SocketUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("try")
|
||||
void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
||||
int port = SocketUtils.findAvailableTcpPort();
|
||||
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"));
|
||||
try {
|
||||
try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"))) {
|
||||
// will only look for the exact port
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
SocketUtils.findAvailableTcpPort(port, port))
|
||||
.withMessageStartingWith("Could not find an available TCP port")
|
||||
.withMessageEndingWith("after 1 attempts");
|
||||
}
|
||||
finally {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,19 +149,16 @@ class SocketUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("try")
|
||||
void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
||||
int port = SocketUtils.findAvailableUdpPort();
|
||||
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
|
||||
try {
|
||||
try (DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"))) {
|
||||
// will only look for the exact port
|
||||
assertThatIllegalStateException().isThrownBy(() ->
|
||||
SocketUtils.findAvailableUdpPort(port, port))
|
||||
.withMessageStartingWith("Could not find an available UDP port")
|
||||
.withMessageEndingWith("after 1 attempts");
|
||||
}
|
||||
finally {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user