diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-09 17:04:20 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-14 11:59:06 +0200 |
commit | 19f80967d511bfff7399290ccbb6c98304e6e2b2 (patch) | |
tree | 0169665598eae9be207c87ec5d7f2b0273b68e11 /sonar-batch | |
parent | bcd54407f2a6ca42c80961303fe1567e7b33621a (diff) | |
download | sonarqube-19f80967d511bfff7399290ccbb6c98304e6e2b2.tar.gz sonarqube-19f80967d511bfff7399290ccbb6c98304e6e2b2.zip |
SONAR-5715 Fix preview mode when source file contains space in its path
Diffstat (limited to 'sonar-batch')
5 files changed, 46 insertions, 13 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 d43ef8e0e71..0fcc09bcf3d 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 @@ -36,7 +36,9 @@ import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.URI; +import java.net.URLEncoder; /** * Replace the deprecated org.sonar.batch.ServerMetadata @@ -135,4 +137,13 @@ public class ServerClient implements BatchComponent { private String getPassword() { return props.property(CoreProperties.PASSWORD); } + + public static String encodeForUrl(String url) { + try { + return URLEncoder.encode(url, "UTF-8"); + + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("Encoding not supported", e); + } + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java index 8340c63a681..7ff216c4280 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java @@ -28,9 +28,6 @@ import org.sonar.batch.bootstrap.TaskProperties; import org.sonar.batch.protocol.input.ProjectReferentials; import org.sonar.batch.rule.ModuleQProfiles; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader { private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectReferentialsLoader.class); @@ -51,11 +48,7 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad if (taskProperties.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) { LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); - try { - url += "&profile=" + URLEncoder.encode(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP), "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("Unable to encode URL", e); - } + url += "&profile=" + ServerClient.encodeForUrl(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP)); } url += "&preview=" + analysisMode.isPreview(); return ProjectReferentials.fromJson(serverClient.request(url)); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java index be876cbc0c9..7bea9e46216 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java @@ -58,7 +58,8 @@ public class LastSnapshots implements BatchComponent { @CheckForNull private String loadSourceFromWs(Resource resource) { try { - return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", false, analysisMode.getPreviewReadTimeoutSec() * 1000); + return server + .request("/api/sources?resource=" + ServerClient.encodeForUrl(resource.getEffectiveKey()) + "&format=txt", false, analysisMode.getPreviewReadTimeoutSec() * 1000); } catch (HttpDownloader.HttpException he) { if (he.getResponseCode() == 404) { return ""; 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 34060a4ee30..9c9b930f99d 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 @@ -135,6 +135,11 @@ public class ServerClientTest { newServerClient().request("/foo"); } + @Test + public void testEncode() { + assertThat(ServerClient.encodeForUrl("my value")).isEqualTo("my+value"); + } + private ServerClient newServerClient() { when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort()); return new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4")); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java index 505b644d98e..1c3746408ab 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java @@ -37,7 +37,10 @@ import java.net.URISyntaxException; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; public class LastSnapshotsTest { @@ -88,7 +91,21 @@ public class LastSnapshotsTest { String source = lastSnapshots.getSource(newFile()); assertThat(source).isEqualTo("downloaded source of Bar.c"); - verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false, 30 * 1000); + verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", false, 30 * 1000); + } + + @Test + public void should_download_source_with_space_from_ws_if_preview_mode() { + db.prepareDbUnit(getClass(), "last_snapshot.xml"); + ServerClient server = mock(ServerClient.class); + when(server.request(anyString(), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Foo Bar.c"); + + when(mode.isPreview()).thenReturn(true); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); + + String source = lastSnapshots.getSource(newFile()); + assertThat(source).isEqualTo("downloaded source of Foo Bar.c"); + verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", false, 30 * 1000); } @Test @@ -113,9 +130,9 @@ public class LastSnapshotsTest { when(mode.isPreview()).thenReturn(true); LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); - String source = lastSnapshots.getSource(newFile()); + String source = lastSnapshots.getSource(newFileWithSpace()); assertThat(source).isEqualTo(""); - verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false, 30 * 1000); + verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FFoo+Bar.c&format=txt", false, 30 * 1000); } @Test @@ -134,4 +151,10 @@ public class LastSnapshotsTest { file.setEffectiveKey("myproject:org/foo/Bar.c"); return file; } + + private File newFileWithSpace() { + File file = new File("org/foo", "Foo Bar.c"); + file.setEffectiveKey("myproject:org/foo/Foo Bar.c"); + return file; + } } |