import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseProperties;
+import org.sonar.api.utils.HttpDownloader.HttpException;
import org.sonar.api.utils.SonarException;
-import javax.annotation.Nullable;
-
import java.io.File;
-import static org.sonar.api.utils.HttpDownloader.HttpException;
-
/**
* @since 3.4
*/
private final Settings settings;
private final ServerClient server;
private final TempDirectories tempDirectories;
- private ProjectReactor reactor;
- public DryRunDatabase(Settings settings, ServerClient server, TempDirectories tempDirectories, @Nullable ProjectReactor reactor) {
+ public DryRunDatabase(Settings settings, ServerClient server, TempDirectories tempDirectories) {
this.settings = settings;
this.server = server;
this.tempDirectories = tempDirectories;
- this.reactor = reactor;
- }
-
- public DryRunDatabase(Settings settings, ServerClient server, TempDirectories tempDirectories) {
- this(settings, server, tempDirectories, null);
}
public void start() {
private void downloadDatabase(File toFile) {
String projectKey = null;
try {
- if (reactor == null) {
+ projectKey = settings.getString(CoreProperties.PROJECT_KEY_PROPERTY);
+ String branch = settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
+ if (StringUtils.isNotBlank(branch)) {
+ projectKey = String.format("%s:%s", projectKey, branch);
+ }
+ if (StringUtils.isBlank(projectKey)) {
server.download("/batch_bootstrap/db", toFile);
} else {
- projectKey = StringUtils.defaultString(reactor.getRoot().getKey());
server.download("/batch_bootstrap/db?project=" + projectKey, toFile);
}
LOG.debug("Dry Run database size: {}", FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(toFile)));
private void replaceSettings(String databasePath) {
settings
- .removeProperty("sonar.jdbc.schema")
- .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT)
- .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER)
- .setProperty(DatabaseProperties.PROP_USER, USER)
- .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD)
- .setProperty(DatabaseProperties.PROP_URL, URL + databasePath);
+ .removeProperty("sonar.jdbc.schema")
+ .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT)
+ .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER)
+ .setProperty(DatabaseProperties.PROP_USER, USER)
+ .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD)
+ .setProperty(DatabaseProperties.PROP_URL, URL + databasePath);
}
}
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseProperties;
import org.sonar.api.utils.HttpDownloader;
import static org.mockito.Mockito.when;
public class DryRunDatabaseTest {
- Settings settings = new Settings();
+ Settings settings;
ServerClient server = mock(ServerClient.class);
TempDirectories tempDirectories = mock(TempDirectories.class);
- ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create().setKey("group:project"));
File databaseFile;
@Rule
public void setUp() throws Exception {
databaseFile = temp.newFile("dryrun.h2.db");
when(tempDirectories.getFile("", "dryrun.h2.db")).thenReturn(databaseFile);
+ settings = new Settings();
settings.setProperty(CoreProperties.DRY_RUN, true);
+ settings.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "group:project");
}
@Test
public void should_be_disabled_if_not_dry_run() {
settings.setProperty(CoreProperties.DRY_RUN, false);
- new DryRunDatabase(settings, server, tempDirectories, projectReactor).start();
+ new DryRunDatabase(settings, server, tempDirectories).start();
verifyZeroInteractions(tempDirectories, server);
}
@Test
public void should_download_database() {
- new DryRunDatabase(settings, server, tempDirectories, projectReactor).start();
+ new DryRunDatabase(settings, server, tempDirectories).start();
verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
}
+ @Test
+ public void should_download_database_on_branch() {
+ settings.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "mybranch");
+ new DryRunDatabase(settings, server, tempDirectories).start();
+
+ verify(server).download("/batch_bootstrap/db?project=group:project:mybranch", databaseFile);
+ }
+
@Test
public void should_replace_database_settings() {
- new DryRunDatabase(settings, server, tempDirectories, projectReactor).start();
+ new DryRunDatabase(settings, server, tempDirectories).start();
assertThat(settings.getString(DatabaseProperties.PROP_DIALECT)).isEqualTo("h2");
assertThat(settings.getString(DatabaseProperties.PROP_DRIVER)).isEqualTo("org.h2.Driver");
thrown.expect(SonarException.class);
thrown.expectMessage("You don't have access rights to project [group:project]");
- new DryRunDatabase(settings, server, tempDirectories, projectReactor).start();
+ new DryRunDatabase(settings, server, tempDirectories).start();
}
@Test
thrown.expect(SonarException.class);
thrown.expectMessage("BUG");
- new DryRunDatabase(settings, server, tempDirectories, projectReactor).start();
+ new DryRunDatabase(settings, server, tempDirectories).start();
}
@Test
public void project_should_be_optional() {
// on non-scan tasks
+ settings.removeProperty(CoreProperties.PROJECT_KEY_PROPERTY);
new DryRunDatabase(settings, server, tempDirectories).start();
verify(server).download("/batch_bootstrap/db", databaseFile);
}