diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java index aaec57c753..6815191f7c 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java @@ -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) { @@ -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("["); diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java index 935ca8134b..1a5ef866e2 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java @@ -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(); @@ -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); + + // 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)); + } + @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-");