diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-01-17 18:38:55 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-01-17 18:39:12 +0100 |
commit | 06270f8f95a8ea553b491d9139cad77926a93db6 (patch) | |
tree | 95dee231f154707444ca4342ad19407bc61bf3d1 /sonar-batch | |
parent | 2d80a79c182283831b0b32047fa5f78580dd767e (diff) | |
download | sonarqube-06270f8f95a8ea553b491d9139cad77926a93db6.tar.gz sonarqube-06270f8f95a8ea553b491d9139cad77926a93db6.zip |
SONAR-4048 improve access denied message during analysis
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java | 11 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java | 24 |
2 files changed, 31 insertions, 4 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index eabee747fa5..8f6b79364d9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -96,9 +96,18 @@ public class ServerClient implements BatchComponent { private SonarException handleHttpException(HttpDownloader.HttpException he) { if (he.getResponseCode() == 401) { - throw new SonarException(String.format("Not authorized. Please check the properties %s and %s.", CoreProperties.LOGIN, CoreProperties.PASSWORD)); + throw new SonarException(String.format(getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD)); } throw new SonarException(String.format("Fail to execute request [code=%s, url=%s]", he.getResponseCode(), he.getUri()), he); } + private String getMessageWhenNotAuthorized(){ + String login = settings.getProperty(CoreProperties.LOGIN); + String password = settings.getProperty(CoreProperties.PASSWORD); + if (StringUtils.isEmpty(login) && StringUtils.isEmpty(password)) { + return "Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties %s and %s."; + } + return "Not authorized. Please check the properties %s and %s."; + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java index 3a0d2dfa570..ec478306bdf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java @@ -23,6 +23,7 @@ import com.google.common.base.Charsets; import com.google.common.io.Files; import org.apache.commons.io.IOUtils; import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -52,6 +53,7 @@ import static org.mockito.Mockito.when; public class ServerClientTest { MockHttpServer server = null; + BootstrapSettings settings; @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -66,6 +68,11 @@ public class ServerClientTest { } } + @Before + public void before(){ + settings = mock(BootstrapSettings.class); + } + @Test public void shouldRemoveUrlEndingSlash() throws Exception { BootstrapSettings settings = mock(BootstrapSettings.class); @@ -97,11 +104,24 @@ public class ServerClientTest { } @Test - public void should_fail_if_unauthorized() throws Exception { + public void should_fail_if_unauthorized_with_login_password_not_provided() throws Exception { server = new MockHttpServer(); server.start(); server.setMockResponseStatus(401); + thrown.expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); + newServerClient().request("/foo"); + } + + @Test + public void should_fail_if_unauthorized_with_login_password_provided() throws Exception { + server = new MockHttpServer(); + server.start(); + server.setMockResponseStatus(401); + + when(settings.getProperty(eq("sonar.login"))).thenReturn("login"); + when(settings.getProperty(eq("sonar.password"))).thenReturn("password"); + thrown.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password"); newServerClient().request("/foo"); } @@ -117,12 +137,10 @@ public class ServerClientTest { } private ServerClient newServerClient() { - BootstrapSettings settings = mock(BootstrapSettings.class); when(settings.getProperty(eq("sonar.host.url"), anyString())).thenReturn("http://localhost:" + server.getPort()); return new ServerClient(settings, new EnvironmentInformation("Junit", "4")); } - static class MockHttpServer { private Server server; private String responseBody; |