]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7235 Improve error message when server public URL is not a valid URL
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 11 Mar 2016 13:26:57 +0000 (14:26 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 15 Mar 2016 10:51:50 +0000 (11:51 +0100)
sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java
sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java

index 5ab75c42f2a1dac676336fdf74f3ea4016a7f385..47e92a283d6b23ea19463a7556b50bb7e54fe18e 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.BatchSide;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.config.Settings;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.TempFolder;
 import org.sonar.api.utils.ZipUtils;
 import org.sonar.api.utils.log.Logger;
@@ -176,7 +177,12 @@ public class ReportPublisher implements Startable {
       metadata.put("projectKey", effectiveKey);
       metadata.put("serverUrl", publicUrl());
 
-      URL dashboardUrl = HttpUrl.parse(publicUrl()).newBuilder()
+      HttpUrl publicUrl = HttpUrl.parse(publicUrl());
+      if (publicUrl == null) {
+        throw MessageException.of("Failed to parse public URL set in SonarQube server: " + publicUrl());
+      }
+
+      URL dashboardUrl = publicUrl.newBuilder()
         .addPathSegment("dashboard").addPathSegment("index").addPathSegment(effectiveKey)
         .build()
         .url();
index 9b3ce46fed354f496b4642b0734dabd4376ccf39..156854285229607963d974993b84e9ab2e739258 100644 (file)
@@ -26,12 +26,14 @@ import java.nio.file.Path;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.mockito.Mockito;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.TempFolder;
 import org.sonar.api.utils.log.LogTester;
 import org.sonar.api.utils.log.LoggerLevel;
@@ -52,6 +54,9 @@ public class ReportPublisherTest {
 
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
+  
+  @Rule
+  public ExpectedException exception = ExpectedException.none();
 
   DefaultAnalysisMode mode = mock(DefaultAnalysisMode.class);
   Settings settings = new Settings(new PropertyDefinitions(CorePropertyDefinitions.all()));
@@ -108,6 +113,16 @@ public class ReportPublisherTest {
       "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"
     );
   }
+  
+  @Test
+  public void fail_if_public_url_malformed() throws IOException {
+    settings.setProperty(CoreProperties.SERVER_BASE_URL, "invalid");
+    ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+
+    exception.expect(MessageException.class);
+    exception.expectMessage("Failed to parse public URL set in SonarQube server: invalid");
+    underTest.logSuccess("TASK-123");
+  }
 
   @Test
   public void log_but_not_dump_information_when_report_is_not_uploaded() {