From cb09ebf0493b270a8c2d18c9b18f1c4d358d80f6 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Mon, 30 Jan 2023 22:16:23 +0000 Subject: [PATCH] Add more tests for Processes and KiwiUrls Sonar failed the last commit, but the auto-merge merged anyway. (I guess we don't have something correctly set up) --- .../kiwiproject/base/process/Processes.java | 1 + .../java/org/kiwiproject/net/KiwiUrls.java | 6 ++-- .../base/process/ProcessesTest.java | 12 ++++++++ .../org/kiwiproject/net/KiwiUrlsTest.java | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kiwiproject/base/process/Processes.java b/src/main/java/org/kiwiproject/base/process/Processes.java index 79099139..6af45056 100644 --- a/src/main/java/org/kiwiproject/base/process/Processes.java +++ b/src/main/java/org/kiwiproject/base/process/Processes.java @@ -505,6 +505,7 @@ private static Pair pairFromPgrepLine(String line) { return Pair.of(pid, command); } + @VisibleForTesting static Long getPidOrThrow(String pidString) { try { return Long.valueOf(pidString); diff --git a/src/main/java/org/kiwiproject/net/KiwiUrls.java b/src/main/java/org/kiwiproject/net/KiwiUrls.java index 78f6c9bd..ab53da56 100644 --- a/src/main/java/org/kiwiproject/net/KiwiUrls.java +++ b/src/main/java/org/kiwiproject/net/KiwiUrls.java @@ -12,6 +12,7 @@ import static org.kiwiproject.collect.KiwiLists.first; import static org.kiwiproject.collect.KiwiMaps.isNullOrEmpty; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Splitter; import com.google.common.collect.ListMultimap; import com.google.common.collect.Multimap; @@ -318,7 +319,7 @@ public static URL createHttpsUrlObject(String hostname, int port, String path) { * * * @param url the URL to analyze - * @return the {@link Components} found or an empty {@link Components} object if the URL was invalid + * @return the {@link Components} found or an "empty" {@link Components} object if the URL was invalid * @throws IllegalArgumentException if the port in the URL is not a number * @implNote This method does not check if the URL is valid or not. */ @@ -472,7 +473,8 @@ public static OptionalInt extractPortFrom(String url) { return OptionalInt.empty(); } - private static int getPortOrThrow(String portString) { + @VisibleForTesting + static int getPortOrThrow(String portString) { try { return Integer.parseInt(portString); } catch (NumberFormatException e) { diff --git a/src/test/java/org/kiwiproject/base/process/ProcessesTest.java b/src/test/java/org/kiwiproject/base/process/ProcessesTest.java index ed5b7fda..7e4efd1b 100644 --- a/src/test/java/org/kiwiproject/base/process/ProcessesTest.java +++ b/src/test/java/org/kiwiproject/base/process/ProcessesTest.java @@ -2,6 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.mockito.Mockito.mock; @@ -238,4 +239,15 @@ void shouldReturnTrueForNonzero(int code) { assertThat(Processes.isNonzeroExitCode(code)).isTrue(); } } + + @Nested + class GetPidOrThrow { + + @ParameterizedTest + @ValueSource(strings = { "a", "", "foo", "12_000"}) + void shouldThrowIllegalArgument_WhenPidIsNotNumeric(String pidString) { + assertThatIllegalArgumentException() + .isThrownBy(() -> Processes.getPidOrThrow(pidString)); + } + } } diff --git a/src/test/java/org/kiwiproject/net/KiwiUrlsTest.java b/src/test/java/org/kiwiproject/net/KiwiUrlsTest.java index 5d69c6cf..ec280c12 100644 --- a/src/test/java/org/kiwiproject/net/KiwiUrlsTest.java +++ b/src/test/java/org/kiwiproject/net/KiwiUrlsTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.params.provider.ValueSource; import org.kiwiproject.base.KiwiStrings; import org.kiwiproject.collect.KiwiMaps; +import org.kiwiproject.junit.jupiter.ClearBoxTest; import java.net.MalformedURLException; import java.net.URLEncoder; @@ -179,6 +180,28 @@ void testExtractAll_WithPort() { assertThat(components.getPath()).isEmpty(); } + @Test + void testExtractAll_WithNonNumericPort() { + var components = KiwiUrls.extractAllFrom("http://prod-server-8.xxx.prod:foobar/"); + + assertThat(components.getScheme()).isNull(); + assertThat(components.getSubDomainName()).isNull(); + assertThat(components.getDomainName()).isNull(); + assertThat(components.getPort()).isEmpty(); + assertThat(components.getPath()).isEmpty(); + } + + @Test + void testExtractAll_WithNegativePort() { + var components = KiwiUrls.extractAllFrom("http://prod-server-8.xxx.prod:-8080/"); + + assertThat(components.getScheme()).isNull(); + assertThat(components.getSubDomainName()).isNull(); + assertThat(components.getDomainName()).isNull(); + assertThat(components.getPort()).isEmpty(); + assertThat(components.getPath()).isEmpty(); + } + @ParameterizedTest @CsvSource(value = { "https://news.bbc.co.uk:8080/a-news-article/about-stuff, news.bbc.co.uk", @@ -267,6 +290,12 @@ void testExtractPortFrom_WithBadUrl_ShouldBeEmpty() { assertThat(KiwiUrls.extractPortFrom(BAD_URL)).isEmpty(); } + @ClearBoxTest + void testGetPortOrThrow_WithNonNumericPort_ShouldThrowIllegalArgumentException() { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiUrls.getPortOrThrow("foobar")); + } + @ParameterizedTest @CsvSource(value = { "https://news.bbc.co.uk:8080/a-news-article/about-stuff, news",