diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-05 14:22:11 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-05 14:23:47 +0100 |
commit | 3df00fb97661f12dabe98cd9a279080e586e9c0d (patch) | |
tree | 2c0285918454c3a3bbd734b5c54a2d3e1dde5f5f /plugins | |
parent | 81b76d8b6cbc8cc685c57e42d7a9c5a8dfac2ab6 (diff) | |
download | sonarqube-3df00fb97661f12dabe98cd9a279080e586e9c0d.tar.gz sonarqube-3df00fb97661f12dabe98cd9a279080e586e9c0d.zip |
SONAR-3895 load settings from web service
Diffstat (limited to 'plugins')
7 files changed, 85 insertions, 81 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java index fc5e21bc95f..d2a4e7b3f81 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import org.slf4j.LoggerFactory; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.batch.DryRunIncompatible; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; @@ -30,6 +31,7 @@ import org.sonar.api.security.ResourcePermissions; import java.util.Set; +@DryRunIncompatible public class ApplyProjectRolesDecorator implements Decorator { private final ResourcePermissions resourcePermissions; diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.java index d911beb4399..81ac5d09f36 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.java @@ -20,7 +20,11 @@ package org.sonar.plugins.cpd; import com.google.common.collect.ImmutableList; -import org.sonar.api.*; +import org.sonar.api.CoreProperties; +import org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.PropertyType; +import org.sonar.api.SonarPlugin; import org.sonar.plugins.cpd.decorators.DuplicationDensityDecorator; import org.sonar.plugins.cpd.decorators.SumDuplicationsDecorator; import org.sonar.plugins.cpd.index.IndexFactory; @@ -37,6 +41,15 @@ import java.util.List; module = true, global = true, category = CoreProperties.CATEGORY_DUPLICATIONS, + type = PropertyType.BOOLEAN), + @Property( + key = CoreProperties.CPD_SKIP_PROPERTY, + defaultValue = "false", + name = "Skip", + description = "Disable detection of duplications", + // not displayed in UI + project = false, module = false, global = false, + category = CoreProperties.CATEGORY_DUPLICATIONS, type = PropertyType.BOOLEAN) }) public final class CpdPlugin extends SonarPlugin { diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java index 754ef176ee0..bd3f6420710 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java @@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; public class CpdSensor implements Sensor { @@ -34,10 +35,12 @@ public class CpdSensor implements Sensor { private CpdEngine sonarEngine; private CpdEngine sonarBridgeEngine; + private Settings settings; - public CpdSensor(SonarEngine sonarEngine, SonarBridgeEngine sonarBridgeEngine) { + public CpdSensor(SonarEngine sonarEngine, SonarBridgeEngine sonarBridgeEngine, Settings settings) { this.sonarEngine = sonarEngine; this.sonarBridgeEngine = sonarBridgeEngine; + this.settings = settings; } public boolean shouldExecuteOnProject(Project project) { @@ -65,9 +68,11 @@ public class CpdSensor implements Sensor { @VisibleForTesting boolean isSkipped(Project project) { - Configuration conf = project.getConfiguration(); - return conf.getBoolean("sonar.cpd." + project.getLanguageKey() + ".skip", - conf.getBoolean(CoreProperties.CPD_SKIP_PROPERTY, false)); + String key = "sonar.cpd." + project.getLanguageKey() + ".skip"; + if (settings.hasKey(key)) { + return settings.getBoolean(key); + } + return settings.getBoolean(CoreProperties.CPD_SKIP_PROPERTY); } public void analyse(Project project, SensorContext context) { diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java index 033e359ec00..21335c46d0a 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/IndexFactory.java @@ -38,15 +38,6 @@ public class IndexFactory implements BatchExtension { private final ResourcePersister resourcePersister; private final DuplicationDao dao; - /** - * For dry run, where is no access to database. - */ - public IndexFactory(Settings settings) { - this.settings = settings; - this.resourcePersister = null; - this.dao = null; - } - public IndexFactory(Settings settings, ResourcePersister resourcePersister, DuplicationDao dao) { this.settings = settings; this.resourcePersister = resourcePersister; @@ -54,23 +45,28 @@ public class IndexFactory implements BatchExtension { } public SonarDuplicationsIndex create(Project project) { - if (isCrossProject(project)) { - LOG.info("Cross-project analysis enabled"); + if (verifyCrossProject(project, LOG)) { return new SonarDuplicationsIndex(new DbDuplicationsIndex(resourcePersister, project, dao)); - } else { - LOG.info("Cross-project analysis disabled"); - return new SonarDuplicationsIndex(); } + return new SonarDuplicationsIndex(); } - /** - * @return true, if was enabled by user and database is available - */ @VisibleForTesting - boolean isCrossProject(Project project) { - return settings.getBoolean(CoreProperties.CPD_CROSS_RPOJECT) - && resourcePersister != null && dao != null - && StringUtils.isBlank(project.getBranch()); - } + boolean verifyCrossProject(Project project, Logger logger) { + boolean crossProject = false; + if (settings.getBoolean(CoreProperties.CPD_CROSS_RPOJECT)) { + if (settings.getBoolean("sonar.dryRun")) { + 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."); + } else { + logger.info("Cross-project analysis enabled"); + crossProject = true; + } + } else { + logger.info("Cross-project analysis disabled"); + } + return crossProject; + } } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdPluginTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdPluginTest.java index 0c309396e55..a3ec3f05048 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdPluginTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdPluginTest.java @@ -19,14 +19,14 @@ */ package org.sonar.plugins.cpd; -import static org.hamcrest.number.OrderingComparisons.greaterThan; -import static org.junit.Assert.assertThat; import org.junit.Test; +import static org.fest.assertions.Assertions.assertThat; + public class CpdPluginTest { @Test public void getExtensions() { - assertThat(new CpdPlugin().getExtensions().size(), greaterThan(1)); + assertThat(new CpdPlugin().getExtensions()).hasSize(6); } } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java index d49dd75daf2..4d17611c5f0 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java @@ -22,70 +22,55 @@ package org.sonar.plugins.cpd; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Before; import org.junit.Test; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; import org.sonar.plugins.cpd.index.IndexFactory; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; public class CpdSensorTest { - private SonarEngine sonarEngine; - private SonarBridgeEngine sonarBridgeEngine; - private CpdSensor sensor; + SonarEngine sonarEngine; + SonarBridgeEngine sonarBridgeEngine; + CpdSensor sensor; + Settings settings; @Before public void setUp() { - IndexFactory indexFactory = new IndexFactory(null); + IndexFactory indexFactory = mock(IndexFactory.class); sonarEngine = new SonarEngine(indexFactory); sonarBridgeEngine = new SonarBridgeEngine(indexFactory); - sensor = new CpdSensor(sonarEngine, sonarBridgeEngine); + settings = new Settings(new PropertyDefinitions(CpdPlugin.class)); + sensor = new CpdSensor(sonarEngine, sonarBridgeEngine, settings); } @Test - public void generalSkip() { - PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty("sonar.cpd.skip", "true"); - - Project project = createJavaProject().setConfiguration(conf); - - assertTrue(sensor.isSkipped(project)); + public void test_global_skip() { + settings.setProperty("sonar.cpd.skip", true); + assertThat(sensor.isSkipped(createJavaProject())).isTrue(); } @Test - public void doNotSkipByDefault() { - Project project = createJavaProject().setConfiguration(new PropertiesConfiguration()); - - assertFalse(sensor.isSkipped(project)); + public void should_not_skip_by_default() { + assertThat(sensor.isSkipped(createJavaProject())).isFalse(); } @Test - public void shouldSkipByLanguage() { - - Project phpProject = createPhpProject(); - phpProject.getConfiguration().setProperty("sonar.cpd.skip", "false"); - phpProject.getConfiguration().setProperty("sonar.cpd.php.skip", "true"); - assertTrue(sensor.isSkipped(phpProject)); - - Project javaProject = createJavaProject(); - javaProject.getConfiguration().setProperty("sonar.cpd.skip", "false"); - javaProject.getConfiguration().setProperty("sonar.cpd.php.skip", "true"); - assertFalse(sensor.isSkipped(javaProject)); - + public void should_skip_by_language() { + settings.setProperty("sonar.cpd.skip", false); + settings.setProperty("sonar.cpd.php.skip", true); + assertThat(sensor.isSkipped(createPhpProject())).isTrue(); + assertThat(sensor.isSkipped(createJavaProject())).isFalse(); } @Test - public void engine() { - Project phpProject = createPhpProject(); - Project javaProject = createJavaProject(); - - assertThat(sensor.getEngine(javaProject), is((CpdEngine) sonarEngine)); - assertThat(sensor.getEngine(phpProject), is((CpdEngine) sonarBridgeEngine)); + public void test_engine() { + assertThat(sensor.getEngine(createJavaProject())).isSameAs(sonarEngine); + assertThat(sensor.getEngine(createPhpProject())).isSameAs(sonarBridgeEngine); } private Project createJavaProject() { diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java index 840a08a1af9..e73bffa4acc 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/IndexFactoryTest.java @@ -21,54 +21,57 @@ package org.sonar.plugins.cpd.index; import org.junit.Before; import org.junit.Test; +import org.slf4j.Logger; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.batch.index.ResourcePersister; import org.sonar.core.duplication.DuplicationDao; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class IndexFactoryTest { - private Project project; - private Settings settings; + Project project; + Settings settings; + IndexFactory factory; + Logger logger; @Before public void setUp() { project = new Project("foo"); settings = new Settings(); + factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); + logger = mock(Logger.class); } @Test public void crossProjectEnabled() { settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); - IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); - assertThat(factory.isCrossProject(project), is(true)); + assertThat(factory.verifyCrossProject(project, logger)).isTrue(); } @Test public void noCrossProjectWithBranch() { settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); - IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); project.setBranch("branch"); - assertThat(factory.isCrossProject(project), is(false)); + assertThat(factory.verifyCrossProject(project, logger)).isFalse(); } @Test - public void noCrossProjectWithoutDatabase() { + public void cross_project_should_be_disabled_on_dry_run() { settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "true"); - IndexFactory factory = new IndexFactory(settings); - assertThat(factory.isCrossProject(project), is(false)); + settings.setProperty("sonar.dryRun", "true"); + assertThat(factory.verifyCrossProject(project, logger)).isFalse(); + verify(logger).info("Cross-project analysis disabled. Not supported on dry runs."); } @Test public void crossProjectDisabled() { settings.setProperty(CoreProperties.CPD_CROSS_RPOJECT, "false"); - IndexFactory factory = new IndexFactory(settings, mock(ResourcePersister.class), mock(DuplicationDao.class)); - assertThat(factory.isCrossProject(project), is(false)); + assertThat(factory.verifyCrossProject(project, logger)).isFalse(); } } |