]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3895 define properties to include/exclude plugins of dry runs
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 23:38:59 +0000 (00:38 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 23:38:59 +0000 (00:38 +0100)
14 files changed:
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseCompatibility.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseCompatibilityTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java
sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java

index 1713209d4ea96ae26a775e5cf466b9d1e5076982..2816675c32e56752d626a6672fda1a9544ab5985 100644 (file)
@@ -323,8 +323,27 @@ import java.util.List;
     global = false,
     defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5,
     category = CoreProperties.CATEGORY_DIFFERENTIAL_VIEWS),
-  @Property(key = CoreProperties.DRY_RUN, defaultValue = "false", name = "Dry Run", type = PropertyType.BOOLEAN, global = false, project = false),
-  @Property(key = "sonar.dryRun.export.path", defaultValue = "dryRun.json", name = "Dry Run Results Export File", type = PropertyType.STRING, global = false, project = false),
+  @Property(
+    key = CoreProperties.DRY_RUN,
+    defaultValue = "false",
+    name = "Dry Run",
+    type = PropertyType.BOOLEAN,
+    global = false, project = false),
+  @Property(
+    key = CoreProperties.DRY_RUN_INCLUDE_PLUGINS,
+    name = "Plugins accepted for dry run",
+    global = true, project = false),
+  @Property(
+    key = CoreProperties.DRY_RUN_EXCLUDE_PLUGINS,
+    name = "Plugins excluded for dry run",
+    global = true, project = false,
+    defaultValue = "devcockpit,pdfreport,report,scmactivity,views"),
+  @Property(
+    key = "sonar.dryRun.export.path",
+    defaultValue = "dryRun.json",
+    name = "Dry Run Results Export File",
+    type = PropertyType.STRING,
+    global = false, project = false),
 
   // SERVER-SIDE TECHNICAL PROPERTIES
 
index 21335c46d0af5a9942bacab32a8f36bd4cf50ff5..ca3f7a4323d89a17bd40e3657318ec5ab20be16c 100644 (file)
@@ -56,7 +56,7 @@ public class IndexFactory implements BatchExtension {
     boolean crossProject = false;
 
     if (settings.getBoolean(CoreProperties.CPD_CROSS_RPOJECT)) {
-      if (settings.getBoolean("sonar.dryRun")) {
+      if (settings.getBoolean(CoreProperties.DRY_RUN)) {
         logger.info("Cross-project analysis disabled. Not supported on dry runs.");
       } else if (StringUtils.isNotBlank(project.getBranch())) {
         logger.info("Cross-project analysis disabled. Not supported on project branches.");
index e73bffa4acc6fa075609266daae1f00a4f91b395..e59da4f6a4b98deabea2fd33f3cceed521d7ad34 100644 (file)
@@ -63,7 +63,7 @@ public class IndexFactoryTest {
   @Test
   public void cross_project_should_be_disabled_on_dry_run() {
     settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true");
-    settings.setProperty("sonar.dryRun", "true");
+    settings.setProperty(CoreProperties.DRY_RUN, "true");
     assertThat(factory.verifyCrossProject(project, logger)).isFalse();
     verify(logger).info("Cross-project analysis disabled. Not supported on dry runs.");
   }
index 8f40cd8d07e423374b4dd7e8d7411ec871c33d4b..c0dddd09215f46720048d9d6031d436899d24f56 100644 (file)
@@ -64,17 +64,15 @@ public class BatchPluginRepository implements PluginRepository {
   }
 
   void doStart(List<RemotePlugin> remotePlugins) {
-    Set<String> whiteList = initWhiteList();
-    Set<String> blackList = initBlackList();
-
+    PluginFilter filter = new PluginFilter(settings);
     PluginInstaller extractor = new PluginInstaller();
     metadataByKey = Maps.newHashMap();
     for (RemotePlugin remote : remotePlugins) {
-      if (isAccepted(remote.getKey(), whiteList, blackList)) {
+      if (filter.accepts(remote.getKey())) {
         List<File> pluginFiles = pluginDownloader.downloadPlugin(remote);
         List<File> extensionFiles = pluginFiles.subList(1, pluginFiles.size());
         PluginMetadata metadata = extractor.installInSameLocation(pluginFiles.get(0), remote.isCore(), extensionFiles);
-        if (StringUtils.isBlank(metadata.getBasePlugin()) || isAccepted(metadata.getBasePlugin(), whiteList, blackList)) {
+        if (StringUtils.isBlank(metadata.getBasePlugin()) || filter.accepts(metadata.getBasePlugin())) {
           LOG.debug("Excluded plugin: " + metadata.getKey());
           metadataByKey.put(metadata.getKey(), metadata);
         }
@@ -84,24 +82,6 @@ public class BatchPluginRepository implements PluginRepository {
     pluginsByKey = classLoaders.init(metadataByKey.values());
   }
 
-  private Set<String> initBlackList() {
-    Set<String> blackList = null;
-    if (settings.hasKey(CoreProperties.BATCH_EXCLUDE_PLUGINS)) {
-      blackList = Sets.newTreeSet(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_EXCLUDE_PLUGINS)));
-      LOG.info("Exclude plugins: " + Joiner.on(", ").join(blackList));
-    }
-    return blackList;
-  }
-
-  private Set<String> initWhiteList() {
-    Set<String> whiteList = null;
-    if (settings.hasKey(CoreProperties.BATCH_INCLUDE_PLUGINS)) {
-      whiteList = Sets.newTreeSet(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_INCLUDE_PLUGINS)));
-      LOG.info("Include plugins: " + Joiner.on(", ").join(whiteList));
-    }
-    return whiteList;
-  }
-
   public void stop() {
     if (classLoaders != null) {
       classLoaders.clean();
@@ -121,16 +101,6 @@ public class BatchPluginRepository implements PluginRepository {
     return metadataByKey.get(pluginKey);
   }
 
-  static boolean isAccepted(String pluginKey, Set<String> whiteList, Set<String> blackList) {
-    if (CORE_PLUGIN.equals(pluginKey) || ENGLISH_PACK_PLUGIN.equals(pluginKey)) {
-      return true;
-    }
-    if (whiteList != null) {
-      return whiteList.contains(pluginKey);
-    }
-    return blackList == null || !blackList.contains(pluginKey);
-  }
-
   public Map<PluginMetadata, Plugin> getPluginsByMetadata() {
     Map<PluginMetadata, Plugin> result = Maps.newHashMap();
     for (Map.Entry<String, PluginMetadata> entry : metadataByKey.entrySet()) {
@@ -140,4 +110,37 @@ public class BatchPluginRepository implements PluginRepository {
     }
     return result;
   }
+
+  static class PluginFilter {
+    Set<String> whites = Sets.newHashSet(), blacks = Sets.newHashSet();
+
+    PluginFilter(Settings settings) {
+      if (settings.hasKey(CoreProperties.BATCH_INCLUDE_PLUGINS)) {
+        whites.addAll(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_INCLUDE_PLUGINS)));
+      }
+      if (settings.hasKey(CoreProperties.BATCH_EXCLUDE_PLUGINS)) {
+        blacks.addAll(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_EXCLUDE_PLUGINS)));
+      }
+      if (settings.getBoolean(CoreProperties.DRY_RUN)) {
+        whites.addAll(Arrays.asList(settings.getStringArray(CoreProperties.DRY_RUN_INCLUDE_PLUGINS)));
+        blacks.addAll(Arrays.asList(settings.getStringArray(CoreProperties.DRY_RUN_EXCLUDE_PLUGINS)));
+      }
+      if (!whites.isEmpty()) {
+        LOG.info("Include plugins: " + Joiner.on(", ").join(whites));
+      }
+      if (!blacks.isEmpty()) {
+        LOG.info("Exclude plugins: " + Joiner.on(", ").join(blacks));
+      }
+    }
+
+    boolean accepts(String pluginKey) {
+      if (CORE_PLUGIN.equals(pluginKey) || ENGLISH_PACK_PLUGIN.equals(pluginKey)) {
+        return true;
+      }
+      if (!whites.isEmpty()) {
+        return whites.contains(pluginKey);
+      }
+      return blacks.isEmpty() || !blacks.contains(pluginKey);
+    }
+  }
 }
index d1b0e6ee75313c515b5c8202958894fddc9d675a..609253a04d36db405e31f7ef503de87f115a8dbd 100644 (file)
@@ -42,7 +42,7 @@ public class DatabaseCompatibility implements BatchComponent {
   }
 
   public void start() {
-    if (!settings.getBoolean("sonar.dryRun")) {
+    if (!settings.getBoolean(CoreProperties.DRY_RUN)) {
       checkCorrectServerId();
       checkDatabaseStatus();
     }
index 4651bcb0db84baadb5a53f9ef5da056be40bf98f..0f4898263459ef569e92bc7fe53e9aa03e8173a7 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils;
 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;
@@ -60,7 +61,7 @@ public class DryRunDatabase implements BatchComponent {
   }
 
   public void start() {
-    if (settings.getBoolean("sonar.dryRun")) {
+    if (settings.getBoolean(CoreProperties.DRY_RUN)) {
       LOG.info("Dry run");
       File databaseFile = tempDirectories.getFile("", "dryrun.h2.db");
       downloadDatabase(reactor.getRoot().getKey(), databaseFile);
index 42dbd7a414e64962edb3de90469104fdd4d59473..b480a2447c7d469f5eac8110d37615b831614e3c 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.batch.bootstrap;
 
 import org.sonar.api.BatchComponent;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.ExtensionProvider;
 import org.sonar.api.Plugin;
 import org.sonar.api.batch.InstantiationStrategy;
@@ -47,7 +48,7 @@ public class ExtensionInstaller implements BatchComponent {
   }
 
   public void install(ComponentContainer container, String instantiationStrategy) {
-    boolean dryRun = settings.getBoolean("sonar.dryRun");
+    boolean dryRun = settings.getBoolean(CoreProperties.DRY_RUN);
     for (Map.Entry<PluginMetadata, Plugin> entry : pluginRepository.getPluginsByMetadata().entrySet()) {
       PluginMetadata metadata = entry.getKey();
       Plugin plugin = entry.getValue();
index 74c091e4de33cbde25a87642b07d7d45647e3da9..80b4f8e1e43d329476c6558a7233d1b1e398c855 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.batch.bootstrap;
 import com.google.common.annotations.VisibleForTesting;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.SonarException;
 
@@ -54,7 +55,7 @@ public class JdbcDriverHolder {
   }
 
   public void start() {
-    if (!settings.getBoolean("sonar.dryRun")) {
+    if (!settings.getBoolean(CoreProperties.DRY_RUN)) {
       LOG.info("Install JDBC driver");
       File jdbcDriver = new File(tempDirectories.getRoot(), "jdbc-driver.jar");
       serverClient.download("/deploy/jdbc-driver.jar", jdbcDriver);
index e58d334c104206b6181bc9ad1c2ed9af978fcdeb..6da2f0192bf808011138bb03cd9bcc510b96fcb8 100644 (file)
@@ -20,9 +20,7 @@
 package org.sonar.batch.bootstrap;
 
 import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
 import org.codehaus.plexus.util.FileUtils;
-import org.hamcrest.Matchers;
 import org.junit.After;
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
@@ -34,11 +32,8 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Set;
 
-import static org.hamcrest.Matchers.not;
-import static org.hamcrest.core.IsNull.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -64,10 +59,10 @@ public class BatchPluginRepositoryTest {
 
     repository.doStart(Arrays.asList(checkstyle));
 
-    assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
-    assertThat(repository.getMetadata().size(), Matchers.is(1));
-    assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle"));
-    assertThat(repository.getMetadata("checkstyle").getDeployedFiles().size(), Matchers.is(4)); // plugin + 3 dependencies
+    assertThat(repository.getPlugin("checkstyle")).isNotNull();
+    assertThat(repository.getMetadata()).hasSize(1);
+    assertThat(repository.getMetadata("checkstyle").getName()).isEqualTo("Checkstyle");
+    assertThat(repository.getMetadata("checkstyle").getDeployedFiles()).hasSize(4); // plugin + 3 dependencies
   }
 
   @Test
@@ -83,11 +78,11 @@ public class BatchPluginRepositoryTest {
 
     repository.doStart(Arrays.asList(checkstyle, checkstyleExt));
 
-    assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
-    assertThat(repository.getPlugin("checkstyleextensions"), not(nullValue()));
-    assertThat(repository.getMetadata().size(), Matchers.is(2));
-    assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle"));
-    assertThat(repository.getMetadata("checkstyleextensions").getVersion(), Matchers.is("0.1-SNAPSHOT"));
+    assertThat(repository.getPlugin("checkstyle")).isNotNull();
+    assertThat(repository.getPlugin("checkstyleextensions")).isNotNull();
+    assertThat(repository.getMetadata()).hasSize(2);
+    assertThat(repository.getMetadata("checkstyle").getName()).isEqualTo("Checkstyle");
+    assertThat(repository.getMetadata("checkstyleextensions").getVersion()).isEqualTo("0.1-SNAPSHOT");
   }
 
   @Test
@@ -102,10 +97,10 @@ public class BatchPluginRepositoryTest {
 
     repository.doStart(Arrays.asList(checkstyle));
 
-    assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
-    assertThat(repository.getMetadata().size(), Matchers.is(1));
-    assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle"));
-    assertThat(repository.getMetadata("checkstyle").getDeployedFiles().size(), Matchers.is(5)); // plugin + 3 dependencies + 1 deprecated
+    assertThat(repository.getPlugin("checkstyle")).isNotNull();
+    assertThat(repository.getMetadata()).hasSize(1);
+    assertThat(repository.getMetadata("checkstyle").getName()).isEqualTo("Checkstyle");
+    assertThat(repository.getMetadata("checkstyle").getDeployedFiles()).hasSize(5); // plugin + 3 dependencies + 1 deprecated
     // extension
   }
 
@@ -124,7 +119,7 @@ public class BatchPluginRepositoryTest {
 
     repository.doStart(Arrays.asList(checkstyle, checkstyleExt));
 
-    assertThat(repository.getMetadata().size(), Matchers.is(0));
+    assertThat(repository.getMetadata()).isEmpty();
   }
 
   private List<File> copyFiles(String... filenames) throws IOException {
@@ -141,58 +136,73 @@ public class BatchPluginRepositoryTest {
 
   @Test
   public void shouldAlwaysAcceptIfNoWhiteListAndBlackList() {
-    repository = new BatchPluginRepository(mock(PluginDownloader.class), new Settings());
-    assertThat(repository.isAccepted("pmd", null, null), Matchers.is(true));
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(new Settings());
+    assertThat(filter.accepts("pmd")).isTrue();
   }
 
   @Test
   public void whiteListShouldTakePrecedenceOverBlackList() {
-    Set<String> whiteList = Sets.newHashSet("checkstyle", "pmd", "findbugs");
-    Set<String> blackList = Sets.newHashSet("cobertura", "pmd");
-    assertThat(BatchPluginRepository.isAccepted("pmd", whiteList, blackList), Matchers.is(true));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_INCLUDE_PLUGINS, "checkstyle,pmd,findbugs")
+      .setProperty(CoreProperties.BATCH_EXCLUDE_PLUGINS, "cobertura,pmd");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("pmd")).isTrue();
   }
 
   @Test
   public void corePluginShouldAlwaysBeInWhiteList() {
-    Set<String> whiteList = Sets.newHashSet("checkstyle", "pmd", "findbugs");
-    Set<String> blackList = null;
-
-    assertThat(BatchPluginRepository.isAccepted("core", whiteList, blackList), Matchers.is(true));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_INCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("core")).isTrue();
   }
 
   @Test
   public void corePluginShouldNeverBeInBlackList() {
-    Set<String> whiteList = null;
-    Set<String> blackList = Sets.newHashSet("core", "findbugs");
-
-    assertThat(BatchPluginRepository.isAccepted("core", whiteList, blackList), Matchers.is(true));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_EXCLUDE_PLUGINS, "core,findbugs");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("core")).isTrue();
   }
 
   // English Pack plugin should never be blacklisted as it is mandatory for the I18nManager on batch side
   @Test
   public void englishPackPluginShouldNeverBeInBlackList() {
-    Set<String> whiteList = null;
-    Set<String> blackList = Sets.newHashSet("l10nen", "findbugs");
-    assertThat(BatchPluginRepository.isAccepted("l10nen", whiteList, blackList), Matchers.is(true));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_EXCLUDE_PLUGINS, "l10nen,findbugs");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("l10nen")).isTrue();
   }
 
   @Test
   public void shouldCheckWhitelist() {
-    Set<String> whiteList = Sets.newHashSet("checkstyle", "pmd", "findbugs");
-    Set<String> blackList = null;
-
-    assertThat(BatchPluginRepository.isAccepted("checkstyle", whiteList, blackList), Matchers.is(true));
-    assertThat(BatchPluginRepository.isAccepted("pmd", whiteList, blackList), Matchers.is(true));
-    assertThat(BatchPluginRepository.isAccepted("cobertura", whiteList, blackList), Matchers.is(false));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_INCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("checkstyle")).isTrue();
+    assertThat(filter.accepts("pmd")).isTrue();
+    assertThat(filter.accepts("cobertura")).isFalse();
   }
 
   @Test
   public void shouldCheckBlackListIfNoWhiteList() {
-    Set<String> whiteList = null;
-    Set<String> blackList = Sets.newHashSet("checkstyle", "pmd", "findbugs");
-    assertThat(BatchPluginRepository.isAccepted("checkstyle", whiteList, blackList), Matchers.is(false));
-    assertThat(BatchPluginRepository.isAccepted("pmd", whiteList, blackList), Matchers.is(false));
-    assertThat(BatchPluginRepository.isAccepted("cobertura", whiteList, blackList), Matchers.is(true));
+    Settings settings = new Settings()
+      .setProperty(CoreProperties.BATCH_EXCLUDE_PLUGINS, "checkstyle,pmd,findbugs");
+    BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+    assertThat(filter.accepts("checkstyle")).isFalse();
+    assertThat(filter.accepts("pmd")).isFalse();
+    assertThat(filter.accepts("cobertura")).isTrue();
   }
 
+  @Test
+    public void should_concatenate_dry_run_filters() {
+      Settings settings = new Settings()
+        .setProperty(CoreProperties.DRY_RUN, true)
+        .setProperty(CoreProperties.DRY_RUN_INCLUDE_PLUGINS, "cockpit")
+        .setProperty(CoreProperties.DRY_RUN_EXCLUDE_PLUGINS, "views")
+        .setProperty(CoreProperties.BATCH_EXCLUDE_PLUGINS, "checkstyle,pmd");
+      BatchPluginRepository.PluginFilter filter = new BatchPluginRepository.PluginFilter(settings);
+      assertThat(filter.whites).containsOnly("cockpit");
+      assertThat(filter.blacks).containsOnly("views", "checkstyle", "pmd");
+    }
 }
index 5ed24eb4fb15da1f9613e976698dbe7303bb6ea2..a99fab4d76a743356c346083067ddce53b24144d 100644 (file)
@@ -51,7 +51,7 @@ public class DatabaseCompatibilityTest {
     settings.setProperty(DatabaseProperties.PROP_URL, "jdbc:postgresql://localhost/foo");
     settings.setProperty(DatabaseProperties.PROP_USER, "bar");
     settings.setProperty(CoreProperties.SERVER_ID, "123456");
-    settings.setProperty("sonar.dryRun", false);
+    settings.setProperty(CoreProperties.DRY_RUN, false);
 
     databaseVersion = mock(DatabaseVersion.class);
   }
@@ -119,7 +119,7 @@ public class DatabaseCompatibilityTest {
   @Test
   public void should_not_verify_compatibility_if_dry_run() {
     settings.setProperty(CoreProperties.SERVER_ID, "11111111");
-    settings.setProperty("sonar.dryRun", true);
+    settings.setProperty(CoreProperties.DRY_RUN, true);
 
     new DatabaseCompatibility(databaseVersion, server, settings).start();
 
index 55b4b9366904bbd9293b292093986f67a67779a7..ae4f2826f0102dd2be0b7c5a6998515af43d8573 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+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;
@@ -56,13 +57,13 @@ public class DryRunDatabaseTest {
   public void setUp() {
     databaseFile = new File("/tmp/dryrun.h2.db");
     when(tempDirectories.getFile("", "dryrun.h2.db")).thenReturn(databaseFile);
-    settings.setProperty("sonar.dryRun", true);
+    settings.setProperty(CoreProperties.DRY_RUN, true);
     dryRunDatabase = new DryRunDatabase(settings, server, tempDirectories, projectReactor, mock(ProjectReactorReady.class));
   }
 
   @Test
   public void should_be_disabled_if_not_dry_run() {
-    settings.setProperty("sonar.dryRun", false);
+    settings.setProperty(CoreProperties.DRY_RUN, false);
     dryRunDatabase.start();
 
     verifyZeroInteractions(tempDirectories, server);
index 57a0fd417d290b6c0a663c3664d99283fe832a29..20db293ba018cab283a64aa8eada4acedd36e505 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.batch.bootstrap;
 
+import com.google.common.io.Files;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.IOFileFilter;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -33,20 +36,23 @@ import static org.fest.assertions.Assertions.assertThat;
 
 public class TempDirectoriesTest {
 
-  private TempDirectories tempDirectories;
-
   @Rule
   public TemporaryFolder folder = new TemporaryFolder();
 
+  TempDirectories tempDirectories;
+  File workDir;
+  ProjectReactor reactor;
+
   @Before
   public void before() throws IOException {
-    ProjectDefinition project = ProjectDefinition.create().setKey("foo").setWorkDir(folder.newFolder());
-    ProjectReactor reactor = new ProjectReactor(project);
-    tempDirectories = new TempDirectories(reactor);
+    workDir = folder.newFolder();
+    ProjectDefinition project = ProjectDefinition.create().setKey("foo").setWorkDir(workDir);
+    reactor = new ProjectReactor(project);
   }
 
   @Test
-  public void should_create_root_temp_dir() {
+  public void should_create_root_temp_dir() throws IOException {
+    tempDirectories = new TempDirectories(reactor);
     assertThat(tempDirectories.getRoot()).isNotNull();
     assertThat(tempDirectories.getRoot()).exists();
     assertThat(tempDirectories.getRoot()).isDirectory();
@@ -54,12 +60,27 @@ public class TempDirectoriesTest {
   }
 
   @Test
-  public void should_accept_empty_dirname() {
+  public void should_clean_root_temp_dir_at_startup() throws IOException {
+    File tempFile = new File(workDir, "_tmp/foo.txt");
+    FileUtils.touch(tempFile);
+    assertThat(tempFile).exists();
+
+    tempDirectories = new TempDirectories(reactor);
+    assertThat(tempDirectories.getRoot().getParentFile()).isEqualTo(workDir);
+    assertThat(tempDirectories.getRoot()).exists();
+    assertThat(tempFile).doesNotExist();
+    assertThat(FileUtils.listFiles(tempDirectories.getRoot(), new String[]{"txt"}, true)).isEmpty();
+  }
+
+  @Test
+  public void should_accept_empty_dirname() throws IOException {
+    tempDirectories = new TempDirectories(reactor);
     assertThat(tempDirectories.getDir("")).isEqualTo(tempDirectories.getRoot());
   }
 
   @Test
-  public void should_create_sub_directory() {
+  public void should_create_sub_directory() throws IOException {
+    tempDirectories = new TempDirectories(reactor);
     File findbugsDir = tempDirectories.getDir("findbugs");
     assertThat(findbugsDir).isNotNull();
     assertThat(findbugsDir).exists();
@@ -68,7 +89,8 @@ public class TempDirectoriesTest {
   }
 
   @Test
-  public void should_delete_temp_dir_on_shutdown() {
+  public void should_delete_temp_dir_on_shutdown() throws IOException {
+    tempDirectories = new TempDirectories(reactor);
     File root = tempDirectories.getRoot();
     File findbugsDir = tempDirectories.getDir("findbugs");
     assertThat(findbugsDir).exists();
@@ -80,7 +102,8 @@ public class TempDirectoriesTest {
   }
 
   @Test
-  public void should_create_parent_directory() {
+  public void should_create_parent_directory() throws IOException {
+    tempDirectories = new TempDirectories(reactor);
     File file = tempDirectories.getFile("findbugs", "bcel.jar");
     assertThat(file).isNotNull();
     assertThat(file.getParentFile().getName()).isEqualTo("findbugs");
index 5f6c01e442968025980fdd253e2ecb258a76521f..0cf496019836f267b93bcd53209a176e2085671e 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils;
 import org.hibernate.cfg.Environment;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.database.DatabaseProperties;
 import org.sonar.core.persistence.dialect.Dialect;
@@ -101,7 +102,7 @@ public class DefaultDatabase implements Database {
     if (dialect == null) {
       throw new IllegalStateException("Can not guess the JDBC dialect. Please check the property sonar.jdbc.url.");
     }
-    if (H2.ID.equals(dialect.getId()) && !settings.getBoolean("sonar.dryRun")) {
+    if (H2.ID.equals(dialect.getId()) && !settings.getBoolean(CoreProperties.DRY_RUN)) {
       LoggerFactory.getLogger(DefaultDatabase.class).warn("H2 database should be used for evaluation purpose only");
     }
     if (!properties.containsKey("sonar.jdbc.driverClassName")) {
index 88e87de56616c7de0e6f1db8a94b4ad25c8ee1f3..43a10d9338d0c85ff12b11a2616b5f69330954f0 100644 (file)
@@ -161,6 +161,16 @@ public interface CoreProperties {
   String BATCH_INCLUDE_PLUGINS = "sonar.includePlugins";
   String BATCH_EXCLUDE_PLUGINS = "sonar.excludePlugins";
 
+  /**
+   * @since 3.4
+   */
+  String DRY_RUN_INCLUDE_PLUGINS = "sonar.dryRun.includePlugins";
+
+  /**
+   * @since 3.4
+   */
+  String DRY_RUN_EXCLUDE_PLUGINS = "sonar.dryRun.excludePlugins";
+
   /**
    * @since 2.10
    */