diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-03 16:24:11 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-03 16:24:11 +0200 |
commit | 04ea2d7fef9dc9e08e47976addfeb1333660a0eb (patch) | |
tree | a1e4718ee0745023006247e09bba87450616ccac /sonar-batch | |
parent | e9323945d40f6e77ee1f75a3acce0ae6eea3bcd3 (diff) | |
download | sonarqube-04ea2d7fef9dc9e08e47976addfeb1333660a0eb.tar.gz sonarqube-04ea2d7fef9dc9e08e47976addfeb1333660a0eb.zip |
SONAR-2497 Add batch properties to include/exclude plugins to load
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java | 41 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java | 65 |
2 files changed, 98 insertions, 8 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index 47270c198ab..c6b3289fd61 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -19,11 +19,17 @@ */ package org.sonar.batch.bootstrap; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonar.api.CoreProperties; import org.sonar.api.Plugin; import org.sonar.api.Properties; import org.sonar.api.Property; @@ -37,10 +43,7 @@ import org.sonar.core.plugin.JpaPluginFile; import java.io.File; import java.net.MalformedURLException; import java.net.URL; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; public class BatchPluginRepository implements PluginRepository { @@ -49,15 +52,31 @@ public class BatchPluginRepository implements PluginRepository { private JpaPluginDao dao; private ArtifactDownloader artifactDownloader; private Map<String, Plugin> pluginsByKey; + private String[] whiteList = null; + private String[] blackList = null; - public BatchPluginRepository(JpaPluginDao dao, ArtifactDownloader artifactDownloader) { + public BatchPluginRepository(JpaPluginDao dao, ArtifactDownloader artifactDownloader, Configuration configuration) { this.dao = dao; this.artifactDownloader = artifactDownloader; + if (configuration.getString(CoreProperties.INCLUDE_PLUGINS)!=null) { + whiteList = configuration.getStringArray(CoreProperties.INCLUDE_PLUGINS); + } + if (configuration.getString(CoreProperties.EXCLUDE_PLUGINS)!=null) { + blackList = configuration.getStringArray(CoreProperties.EXCLUDE_PLUGINS); + } // TODO reactivate somewhere else: LOG.info("Execution environment: {} {}", environment.getKey(), environment.getVersion()); } public void start() { - doStart(dao.getPlugins()); + doStart(filter(dao.getPlugins())); + } + + List<JpaPlugin> filter(List<JpaPlugin> plugins) { + return ImmutableList.copyOf(Iterables.filter(plugins, new Predicate<JpaPlugin>() { + public boolean apply(JpaPlugin p) { + return isAccepted(p.getKey()) && (StringUtils.isBlank(p.getBasePlugin()) || isAccepted(p.getBasePlugin())); + } + })); } public void doStart(List<JpaPlugin> basePlugins) { @@ -153,4 +172,14 @@ public class BatchPluginRepository implements PluginRepository { } return new Property[0]; } + + boolean isAccepted(String pluginKey) { + if (whiteList!=null) { + return ArrayUtils.contains(whiteList, pluginKey); + } + if (blackList!=null) { + return !ArrayUtils.contains(blackList, pluginKey); + } + return true; + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java index 0c617bf4271..af8dbec7198 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java @@ -19,17 +19,24 @@ */ package org.sonar.batch.bootstrap; +import com.google.common.collect.Lists; +import org.apache.commons.configuration.PropertiesConfiguration; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.util.FileUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; +import org.hamcrest.Matchers; import org.junit.Test; +import org.sonar.api.CoreProperties; import org.sonar.api.Plugin; import org.sonar.core.plugin.JpaPlugin; +import org.sonar.core.plugin.JpaPluginDao; import org.sonar.core.plugin.JpaPluginFile; import java.util.Arrays; +import java.util.List; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.not; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.nullValue; @@ -46,7 +53,7 @@ public class BatchPluginRepositoryTest { ArtifactDownloader extensionDownloader = mock(ArtifactDownloader.class); when(extensionDownloader.downloadExtension(any(JpaPluginFile.class))).thenReturn( FileUtils.toFile(getClass().getResource("/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar"))); - BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader); + BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader, new PropertiesConfiguration()); JpaPlugin plugin = new JpaPlugin("artifactsize"); plugin.setPluginClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin"); @@ -67,7 +74,7 @@ public class BatchPluginRepositoryTest { ArtifactDownloader extensionDownloader = mock(ArtifactDownloader.class); prepareDownloader(extensionDownloader, "artifactsize", "/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-artifact-size-plugin-0.2.jar"); prepareDownloader(extensionDownloader, "clirr", "/org/sonar/batch/bootstrap/BatchPluginRepositoryTest/sonar-clirr-plugin-1.1.jar"); - BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader); + BatchPluginRepository repository = new BatchPluginRepository(null, extensionDownloader, new PropertiesConfiguration()); JpaPlugin pluginBase = new JpaPlugin("artifactsize"); pluginBase.setPluginClass("org.sonar.plugins.artifactsize.ArtifactSizePlugin"); @@ -96,4 +103,58 @@ public class BatchPluginRepositoryTest { } }))).thenReturn(FileUtils.toFile(getClass().getResource(filename))); } + + @Test + public void shouldAlwaysAcceptIfNoWhiteListAndBlackList() { + BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), new PropertiesConfiguration()); + assertThat(repository.isAccepted("pmd"), Matchers.is(true)); + } + + @Test + public void whiteListShouldTakePrecedenceOverBlackList() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.INCLUDE_PLUGINS, "checkstyle,pmd,findbugs"); + conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "cobertura,pmd"); + BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf); + + assertThat(repository.isAccepted("pmd"), Matchers.is(true)); + } + + @Test + public void shouldCheckWhitelist() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.INCLUDE_PLUGINS, "checkstyle,pmd,findbugs"); + BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf); + + assertThat(repository.isAccepted("checkstyle"), Matchers.is(true)); + assertThat(repository.isAccepted("pmd"), Matchers.is(true)); + assertThat(repository.isAccepted("cobertura"), Matchers.is(false)); + } + + @Test + public void shouldCheckBlackListIfNoWhiteList() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "checkstyle,pmd,findbugs"); + BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf); + + assertThat(repository.isAccepted("checkstyle"), Matchers.is(false)); + assertThat(repository.isAccepted("pmd"), Matchers.is(false)); + assertThat(repository.isAccepted("cobertura"), Matchers.is(true)); + } + + @Test + public void shouldExcludePluginDependents() { + JpaPlugin pmd = new JpaPlugin("pmd"); + JpaPlugin checkstyle = new JpaPlugin("checkstyle"); + JpaPlugin checkstyleExtension = new JpaPlugin("checkstyle-ext"); + checkstyleExtension.setBasePlugin("checkstyle"); + + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.EXCLUDE_PLUGINS, "checkstyle"); + BatchPluginRepository repository = new BatchPluginRepository(mock(JpaPluginDao.class), mock(ArtifactDownloader.class), conf); + + List<JpaPlugin> filteredPlugins = repository.filter(Arrays.asList(checkstyle, checkstyleExtension, pmd)); + assertThat(filteredPlugins.size(), Matchers.is(1)); + assertThat(filteredPlugins, hasItem(pmd)); + } } |