From 8e276be6823a033405ebfa917809df850f2ea84f Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Mon, 30 Jan 2023 14:29:59 -0500 Subject: [PATCH] CodeQL fixes to SimpleHostAndPort (#886) Fixes #862 Fixes #867 --- .../org/kiwiproject/net/SimpleHostAndPort.java | 18 +++++++++++++++--- .../kiwiproject/net/SimpleHostAndPortTest.java | 13 ++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/kiwiproject/net/SimpleHostAndPort.java b/src/main/java/org/kiwiproject/net/SimpleHostAndPort.java index 050fd6ad..1c03872a 100644 --- a/src/main/java/org/kiwiproject/net/SimpleHostAndPort.java +++ b/src/main/java/org/kiwiproject/net/SimpleHostAndPort.java @@ -2,6 +2,7 @@ import static com.google.common.base.Preconditions.checkState; import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -50,15 +51,26 @@ public static SimpleHostAndPort from(String hostPortString, String defaultHost, * * @param hostPortString a string containing host and port, e.g. foo.com:9000 * @return a new SimpleHostAndPort instance - * @throws NullPointerException if {@code hostPortString} is null * @throws IllegalStateException if not in the expected format - * @throws NumberFormatException if port is not a valid number + * @throws IllegalArgumentException if hostPortString is blank or port is not a valid number + * @implNote Does no validation on the host part */ public static SimpleHostAndPort from(String hostPortString) { + checkArgumentNotBlank(hostPortString, "hostAndPortString must not be blank"); + var split = hostPortString.split(":"); checkState(split.length == 2, "%s is not in format host:port", hostPortString); - return new SimpleHostAndPort(split[0], Integer.parseInt(split[1], 10)); + var port = getPortOrThrow(split); + return new SimpleHostAndPort(split[0], port); + } + + private static int getPortOrThrow(String[] split) { + try { + return Integer.parseInt(split[1], 10); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(e); + } } /** diff --git a/src/test/java/org/kiwiproject/net/SimpleHostAndPortTest.java b/src/test/java/org/kiwiproject/net/SimpleHostAndPortTest.java index 59056907..9436e6e4 100644 --- a/src/test/java/org/kiwiproject/net/SimpleHostAndPortTest.java +++ b/src/test/java/org/kiwiproject/net/SimpleHostAndPortTest.java @@ -48,14 +48,9 @@ void testFromWithDefaults_WhenHostOnlyString() { @ParameterizedTest @ArgumentsSource(BlankStringArgumentsProvider.class) void testFromWithNoDefaults_WithBlankStrings(String input) { - if (isNull(input)) { - assertThatThrownBy(() -> SimpleHostAndPort.from(null)) - .isExactlyInstanceOf(NullPointerException.class); - } else { - assertThatThrownBy(() -> SimpleHostAndPort.from(input)) - .isExactlyInstanceOf(IllegalStateException.class) - .hasMessageEndingWith("is not in format host:port"); - } + assertThatThrownBy(() -> SimpleHostAndPort.from(null)) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessage("hostAndPortString must not be blank"); } @Test @@ -68,7 +63,7 @@ void testFromWithNoDefaults_WithValidHostPortString() { @Test void testFromWithNoDefaults_WithInvalidPort() { assertThatThrownBy(() -> SimpleHostAndPort.from("192.168.1.101:abc")) - .isExactlyInstanceOf(NumberFormatException.class); + .isExactlyInstanceOf(IllegalArgumentException.class); } @Test