]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5715 Fix preview mode when source file contains space in its path
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 9 Oct 2014 15:04:20 +0000 (17:04 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 14 Oct 2014 09:59:06 +0000 (11:59 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java

index d43ef8e0e711ded34dc5f5416efb6eb1ee6c8765..0fcc09bcf3d671f618ba7f17b76c2f26d562c55a 100644 (file)
@@ -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);
+    }
+  }
 }
index 8340c63a6813b24003144be9ad72826340aff096..7ff216c42805d9c843e74b2bcc5803809050b523 100644 (file)
@@ -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));
index be876cbc0c9230a5a49f24ceb69676b8b112f447..7bea9e46216ef0014fbb11b6a0c7d878d09597ca 100644 (file)
@@ -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 "";
index 34060a4ee30dc5d8faefc49ad11074516de912d9..9c9b930f99defaa4b0f49dd33f7070db0aa92cb4 100644 (file)
@@ -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"));
index 505b644d98e0771b45f4fd88c4da3e34a48289f2..1c3746408abd91df643054b0383546f79aabd809 100644 (file)
@@ -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;
+  }
 }