]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7534 Server#getUrl must return serverBaseUrl when it is set
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 13 May 2016 08:33:52 +0000 (10:33 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 13 May 2016 13:24:40 +0000 (15:24 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
server/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
sonar-plugin-api/src/main/java/org/sonar/api/platform/Server.java

index 5c7a8ecd6adaf6f47338306ddf2175f1fec2392f..4b2e155fd8dd58dfa2b349f76c9d81bec00a4247 100644 (file)
@@ -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");
index 84b7c009c1405bba634a4e0e924183fca36a1b8a..b933afb0dea2a9550cb11d332850f25f5e9f8a07 100644 (file)
@@ -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");
index b77d46aacd2bb5bbaf6c8cd2fb9bc2d453a7637e..5ac727afd9e1bd9a000ab48e8bbe1ff31d919f35 100644 (file)
@@ -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();