Skip to content

Commit

Permalink
Add more tests for Processes and KiwiUrls (#894)
Browse files Browse the repository at this point in the history
Sonar failed the last commit, but the auto-merge merged anyway.

(I guess we don't have something correctly set up)
  • Loading branch information
sleberknight authored Jan 30, 2023
1 parent 0e1cd0a commit 49ad29f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/kiwiproject/base/process/Processes.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ private static Pair<Long, String> pairFromPgrepLine(String line) {
return Pair.of(pid, command);
}

@VisibleForTesting
static Long getPidOrThrow(String pidString) {
try {
return Long.valueOf(pidString);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/kiwiproject/net/KiwiUrls.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,7 +319,7 @@ public static URL createHttpsUrlObject(String hostname, int port, String path) {
* </ul>
*
* @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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/kiwiproject/base/process/ProcessesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}
29 changes: 29 additions & 0 deletions src/test/java/org/kiwiproject/net/KiwiUrlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 49ad29f

Please sign in to comment.