From 219258d20fea0dd10e31c758815b285fc8df368f Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 13 May 2016 10:33:52 +0200 Subject: [PATCH] SONAR-7534 Server#getUrl must return serverBaseUrl when it is set --- .../org/sonar/server/platform/ServerImpl.java | 8 +++++++ .../sonar/server/platform/ServerImplTest.java | 21 +++++++++++++++++++ .../java/org/sonar/api/platform/Server.java | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java index 5c7a8ecd6ad..4b2e155fd8d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java @@ -198,6 +198,14 @@ public final class ServerImpl extends Server implements Startable { @Override public String getURL() { + String serverBaseUrl = settings.getString(SERVER_BASE_URL); + if (isEmpty(serverBaseUrl)) { + return computeUrl(); + } + return serverBaseUrl; + } + + private String computeUrl() { String host = settings.getString("sonar.web.host"); int port = settings.getInt("sonar.web.port"); String context = settings.getString("sonar.web.context"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java index 84b7c009c14..b933afb0dea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java @@ -36,6 +36,7 @@ import org.sonar.process.ProcessProperties; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertThat; +import static org.sonar.api.CoreProperties.SERVER_BASE_URL; public class ServerImplTest { @@ -225,6 +226,26 @@ public class ServerImplTest { assertThat(underTest.getURL()).isEqualTo("http://localhost:9000"); } + @Test + public void getUrl_returns_http_localhost_9000_when_serverBaseUrl_is_null() { + settings.setProperty(SERVER_BASE_URL, (String) null); + assertThat(underTest.getURL()).isEqualTo("http://localhost:9000"); + } + + + @Test + public void getUrl_returns_serverBaseUrl_it_is_non_empty() { + String serverBaseUrl = "whatever"; + settings.setProperty(SERVER_BASE_URL, serverBaseUrl); + assertThat(underTest.getURL()).isEqualTo(serverBaseUrl); + } + + @Test + public void getUrl_returns_http_localhost_9000_when_serverBaseUrl_is_empty() { + settings.setProperty(SERVER_BASE_URL, ""); + assertThat(underTest.getURL()).isEqualTo("http://localhost:9000"); + } + @Test public void getUrl_returns_http_localhost_9000_when_host_set_to_0_0_0_0() { settings.setProperty(HOST_PROPERTY, "0.0.0.0"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/Server.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/Server.java index b77d46aacd2..5ac727afd9e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/Server.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/Server.java @@ -71,7 +71,7 @@ public abstract class Server { /** * @return the server URL - * @since since 2.4 on batch side only, since 5.6 on both batch side and server side + * @since since 2.4 on batch side only, since 5.6 on both batch side and server side (WebServer and Compute Engine) */ public abstract String getURL(); -- 2.39.5