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
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);
+ }
+ }
}
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);
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));
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 {
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
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
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;
+ }
}