]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4433 Fix regression in dryRun
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 25 Jun 2013 12:28:00 +0000 (14:28 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 25 Jun 2013 12:32:08 +0000 (14:32 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java

index 1f42d9e23751f42ad798e7320c5f3e2f8b33e91d..0f73212e3376eb0b5f697895891e2d74be93f03a 100644 (file)
@@ -26,17 +26,13 @@ import org.slf4j.Logger;
 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
  */
@@ -52,17 +48,11 @@ public class DryRunDatabase implements BatchComponent {
   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() {
@@ -79,10 +69,14 @@ public class DryRunDatabase implements BatchComponent {
   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)));
@@ -97,11 +91,11 @@ public class DryRunDatabase implements BatchComponent {
 
   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);
   }
 }
index 62135edc925140552b14ec19ef2ecbfd9d1c68f5..f19dbc663af1ed1d185ba4df2985cfb75af509e4 100644 (file)
@@ -26,8 +26,6 @@ import org.junit.Test;
 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;
@@ -43,10 +41,9 @@ import static org.mockito.Mockito.verifyZeroInteractions;
 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
@@ -59,27 +56,37 @@ public class DryRunDatabaseTest {
   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");
@@ -95,7 +102,7 @@ public class DryRunDatabaseTest {
     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
@@ -105,12 +112,13 @@ public class DryRunDatabaseTest {
     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);
   }