From 824807d26ee2b58346ed18f67ac7bc8244c139ee Mon Sep 17 00:00:00 2001 From: Fabian Streitel Date: Thu, 28 Mar 2024 10:20:26 +0100 Subject: [PATCH] don't warn about proxy port when no port is set --- CHANGELOG.md | 1 + .../client/ProxySystemProperties.java | 16 ++++++++++- .../client/ProxySystemPropertiesTest.java | 28 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 teamscale-client/src/test/java/com/teamscale/client/ProxySystemPropertiesTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b829dde8..15dc43377 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ We use [semantic versioning](http://semver.org/): # Next Release - [feature] Docker: agent copies itself to `/transfer` if this is mounted into the container +- [fix] Disable warning about proxy port not being correct when no proxy port was set at all # 33.1.2 - [fix] _teamscale-maven-plugin_: Revision and end commit could not be set via command line (user property) diff --git a/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java b/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java index 403971c23..2a862112f 100644 --- a/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java +++ b/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java @@ -70,7 +70,8 @@ public String getProxyHost() { } /** - * Read the http(s).proxyPort system variable + * Read the http(s).proxyPort system variable. + * Returns -1 if no or an invalid port was set. */ public int getProxyPort() { return parsePort(System.getProperty(getProxyPortSystemPropertyName())); @@ -102,6 +103,14 @@ public void setProxyPort(String proxyPort) { System.setProperty(getProxyPortSystemPropertyName(), proxyPort); } + /** + * Removes the http(s).proxyPort system variable. + * For testing. + */ + /*package*/ void removeProxyPort() { + System.clearProperty(getProxyPortSystemPropertyName()); + } + @NotNull private String getProxyPortSystemPropertyName() { return protocol + PROXY_PORT_SYSTEM_PROPERTY; @@ -146,7 +155,12 @@ private String getProxyPasswordSystemPropertyName() { return protocol + PROXY_PASSWORD_SYSTEM_PROPERTY; } + /** Parses the given port string. Returns -1 if the string is null or not a valid number. */ private int parsePort(String portString) { + if (portString == null) { + return -1; + } + try { return Integer.parseInt(portString); } catch (NumberFormatException e) { diff --git a/teamscale-client/src/test/java/com/teamscale/client/ProxySystemPropertiesTest.java b/teamscale-client/src/test/java/com/teamscale/client/ProxySystemPropertiesTest.java new file mode 100644 index 000000000..84ce8d74a --- /dev/null +++ b/teamscale-client/src/test/java/com/teamscale/client/ProxySystemPropertiesTest.java @@ -0,0 +1,28 @@ +package com.teamscale.client; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +class ProxySystemPropertiesTest { + + private static ProxySystemProperties properties = new ProxySystemProperties(ProxySystemProperties.Protocol.HTTP); + + @AfterAll + static void teardown() { + properties.removeProxyPort(); + } + + @Test + void testPortParsing() { + properties.setProxyPort(9876); + Assertions.assertThat(properties.getProxyPort()).isEqualTo(9876); + properties.setProxyPort(""); + Assertions.assertThat(properties.getProxyPort()).isEqualTo(-1); + properties.setProxyPort("nonsense"); + Assertions.assertThat(properties.getProxyPort()).isEqualTo(-1); + properties.removeProxyPort(); + Assertions.assertThat(properties.getProxyPort()).isEqualTo(-1); + } + +} \ No newline at end of file