From 55039a7a500530ae049b4b4c991bafa75faf1ac7 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 11 Mar 2016 14:26:57 +0100 Subject: [PATCH] SONAR-7235 Improve error message when server public URL is not a valid URL --- .../org/sonar/batch/report/ReportPublisher.java | 8 +++++++- .../sonar/batch/report/ReportPublisherTest.java | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java index 5ab75c42f2a..47e92a283d6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java @@ -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(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java index 9b3ce46fed3..15685428522 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java @@ -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() { -- 2.39.5