Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed non final variable code smell #1849

Merged
merged 7 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public static int getFreeRandomPort() {
public static int getFreeRandomPort(int min, int max, int attempts) {
for (int i=0; i < attempts; i++) {
int port = min + RANDOM.nextInt(max - min + 1);
try (Socket socket = new Socket("localhost", port)) { // NOSONAR
// Port is open, so it's used up, try again
try (Socket ignored = new Socket("localhost", port)) { // NOSONAR
// Port is open for communication, meaning it's used up, try again
} catch (ConnectException e) {
return port;
} catch (IOException e) {
Expand All @@ -132,7 +132,7 @@ public static String sanitizeFileName(String name) {

// ========================================================================================

private static int PROGRESS_LENGTH = 50;
private static final int PROGRESS_LENGTH = 50;

private static String getProgressBar(long bytesRead, long length) {
StringBuilder ret = new StringBuilder("[");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class IoUtilTest {


@Test
void findOpenPort() throws IOException {
int port = IoUtil.getFreeRandomPort();
Expand All @@ -44,8 +47,46 @@ void findOpenPortWhenPortsAreBusy() throws IOException {
assertThat(port2).isGreaterThan(port);
}

@Test
void findOpenPortWithSmallAttemptsCount() throws IOException {
int port = IoUtil.getFreeRandomPort(30000, 60000, 30);
try (ServerSocket ss = new ServerSocket(port)) {
assertThat(ss).isNotNull();
}
}

@Test
void findOpenPortWithLargeAttemptsCount() throws IOException {
int port = IoUtil.getFreeRandomPort(30000, 60000, 1000);
try (ServerSocket ss = new ServerSocket(port)) {
assertThat(ss).isNotNull();
}
}

@Test
void invokeExceptionWhenCouldntFindPort() throws IOException {

// find open port to occupy
int foundPort = IoUtil.getFreeRandomPort(30000, 65000, 1000);

// use port
//noinspection resource
ServerSocket ignored = new ServerSocket(foundPort);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement should be enclosed in a try-with-resources block so that the "ignored" port is closed when the test completes


// try to use the used port
Exception exception = assertThrows(IllegalStateException.class,
() -> IoUtil.getFreeRandomPort(foundPort, foundPort, 3));


String expectedMessage = "Cannot find a free random port in the range [" + foundPort + ", " + foundPort + "] after 3 attempts";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
Comment on lines +77 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be converted to a single assertion using AssertJ

}

@Test
void testSanitizeFileName() {
//noinspection ConstantConditions
assertThat(IoUtil.sanitizeFileName(null)).isNull();
assertThat(IoUtil.sanitizeFileName("Hello/&%World")).isEqualTo("Hello-World");
assertThat(IoUtil.sanitizeFileName(" _H-.-e-.-l-l--.//o()")).isEqualTo("-H-e-l-l-o-");
Expand Down