From: Julien HENRY Date: Thu, 15 Jan 2015 16:34:58 +0000 (+0100) Subject: SONAR-5931 Cleanup X-Git-Tag: latest-silver-master-#65~199 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a42dd4d2b9d0eafe67bd514d56f0ab4ffc8ddbea;p=sonarqube.git SONAR-5931 Cleanup --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java index e0a0e348514..37cb0c5aa69 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.bootstrap; +import org.sonar.batch.sensor.AnalyzerOptimizer; + import com.google.common.collect.Lists; import org.apache.commons.lang.ClassUtils; import org.sonar.api.batch.CheckProject; @@ -28,7 +30,6 @@ import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; import org.sonar.batch.scan.SensorWrapper; -import org.sonar.batch.scan2.AnalyzerOptimizer; import org.sonar.batch.sensor.DefaultSensorContext; import javax.annotation.Nullable; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index a7af5621465..a2d9f3a165f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.scan; +import org.sonar.batch.sensor.AnalyzerOptimizer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; @@ -95,7 +97,6 @@ import org.sonar.batch.scan.filesystem.ProjectFileSystemAdapter; import org.sonar.batch.scan.filesystem.StatusDetectionFactory; import org.sonar.batch.scan.maven.MavenPluginsConfigurator; import org.sonar.batch.scan.report.JsonReport; -import org.sonar.batch.scan2.AnalyzerOptimizer; import org.sonar.batch.sensor.DefaultSensorContext; import org.sonar.batch.sensor.DefaultSensorStorage; import org.sonar.batch.sensor.coverage.CoverageExclusions; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java index c316078f714..8884bef521f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java @@ -19,13 +19,14 @@ */ package org.sonar.batch.scan; +import org.sonar.batch.sensor.AnalyzerOptimizer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; import org.sonar.api.resources.Project; -import org.sonar.batch.scan2.AnalyzerOptimizer; public class SensorWrapper implements org.sonar.api.batch.Sensor { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalyzerOptimizer.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalyzerOptimizer.java deleted file mode 100644 index c149a7aea15..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/AnalyzerOptimizer.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan2; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.BatchComponent; -import org.sonar.api.batch.fs.FilePredicate; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.config.Settings; - -public class AnalyzerOptimizer implements BatchComponent { - - private static final Logger LOG = LoggerFactory.getLogger(AnalyzerOptimizer.class); - - private final FileSystem fs; - private final ActiveRules activeRules; - private final Settings settings; - - public AnalyzerOptimizer(FileSystem fs, ActiveRules activeRules, Settings settings) { - this.fs = fs; - this.activeRules = activeRules; - this.settings = settings; - } - - /** - * Decide if the given Analyzer should be executed. - */ - public boolean shouldExecute(DefaultSensorDescriptor descriptor) { - if (!fsCondition(descriptor)) { - LOG.debug("'{}' skipped because there is no related file in current project", descriptor.name()); - return false; - } - if (!activeRulesCondition(descriptor)) { - LOG.debug("'{}' skipped because there is no related rule activated in the quality profile", descriptor.name()); - return false; - } - if (!settingsCondition(descriptor)) { - LOG.debug("'{}' skipped because one of the required properties is missing", descriptor.name()); - return false; - } - return true; - } - - private boolean settingsCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.properties().isEmpty()) { - for (String propertyKey : descriptor.properties()) { - if (!settings.hasKey(propertyKey)) { - return false; - } - } - } - return true; - } - - private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.ruleRepositories().isEmpty()) { - for (String repoKey : descriptor.ruleRepositories()) { - if (!activeRules.findByRepository(repoKey).isEmpty()) { - return true; - } - } - return false; - } - return true; - } - - private boolean fsCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.languages().isEmpty() || descriptor.type() != null) { - FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages()); - - FilePredicate typePredicate = descriptor.type() == null ? fs.predicates().all() : fs.predicates().hasType(descriptor.type()); - return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate)); - } - return true; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/IssueCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/IssueCache.java deleted file mode 100644 index 5de18c1b8c3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/IssueCache.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan2; - -import org.sonar.api.BatchComponent; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.batch.index.Cache; -import org.sonar.batch.index.Caches; -import org.sonar.batch.scan.filesystem.InputPathCache; - -/** - * Shared issues among all project modules - */ -public class IssueCache implements BatchComponent { - - // project key -> resource key -> issue key -> issue - private final Cache cache; - - public IssueCache(Caches caches, InputPathCache inputPathCache) { - caches.registerValueCoder(DefaultIssue.class, new DefaultIssueValueCoder(inputPathCache)); - cache = caches.createCache("issues"); - } - - public Iterable byComponent(String projectKey, String resourceKey) { - return cache.values(projectKey, resourceKey); - } - - public Iterable all() { - return cache.values(); - } - - public IssueCache put(String projectKey, String resourceKey, DefaultIssue issue) { - cache.put(projectKey, resourceKey, issue.key(), issue); - return this; - } - - public Iterable byModule(String projectKey) { - return cache.values(projectKey); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/SensorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/SensorsExecutor.java index ea872c4c1bf..dc68e4d41f4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/SensorsExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/SensorsExecutor.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.scan2; +import org.sonar.batch.sensor.AnalyzerOptimizer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/AnalyzerOptimizer.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/AnalyzerOptimizer.java new file mode 100644 index 00000000000..ed085a73db9 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/sensor/AnalyzerOptimizer.java @@ -0,0 +1,97 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.sensor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.BatchComponent; +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.rule.ActiveRules; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.config.Settings; + +public class AnalyzerOptimizer implements BatchComponent { + + private static final Logger LOG = LoggerFactory.getLogger(AnalyzerOptimizer.class); + + private final FileSystem fs; + private final ActiveRules activeRules; + private final Settings settings; + + public AnalyzerOptimizer(FileSystem fs, ActiveRules activeRules, Settings settings) { + this.fs = fs; + this.activeRules = activeRules; + this.settings = settings; + } + + /** + * Decide if the given Analyzer should be executed. + */ + public boolean shouldExecute(DefaultSensorDescriptor descriptor) { + if (!fsCondition(descriptor)) { + LOG.debug("'{}' skipped because there is no related file in current project", descriptor.name()); + return false; + } + if (!activeRulesCondition(descriptor)) { + LOG.debug("'{}' skipped because there is no related rule activated in the quality profile", descriptor.name()); + return false; + } + if (!settingsCondition(descriptor)) { + LOG.debug("'{}' skipped because one of the required properties is missing", descriptor.name()); + return false; + } + return true; + } + + private boolean settingsCondition(DefaultSensorDescriptor descriptor) { + if (!descriptor.properties().isEmpty()) { + for (String propertyKey : descriptor.properties()) { + if (!settings.hasKey(propertyKey)) { + return false; + } + } + } + return true; + } + + private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) { + if (!descriptor.ruleRepositories().isEmpty()) { + for (String repoKey : descriptor.ruleRepositories()) { + if (!activeRules.findByRepository(repoKey).isEmpty()) { + return true; + } + } + return false; + } + return true; + } + + private boolean fsCondition(DefaultSensorDescriptor descriptor) { + if (!descriptor.languages().isEmpty() || descriptor.type() != null) { + FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages()); + + FilePredicate typePredicate = descriptor.type() == null ? fs.predicates().all() : fs.predicates().hasType(descriptor.type()); + return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate)); + } + return true; + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java index 1509a65ffea..f6979d924c6 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java @@ -19,12 +19,13 @@ */ package org.sonar.batch.bootstrap; +import org.sonar.batch.sensor.AnalyzerOptimizer; + import org.junit.Test; import org.sonar.api.BatchExtension; import org.sonar.api.batch.Sensor; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; -import org.sonar.batch.scan2.AnalyzerOptimizer; import org.sonar.batch.sensor.DefaultSensorContext; import java.util.Collection; diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan2/AnalyzerOptimizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan2/AnalyzerOptimizerTest.java deleted file mode 100644 index b056715e260..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/scan2/AnalyzerOptimizerTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan2; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; -import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.config.Settings; -import org.sonar.api.rule.RuleKey; - -import static org.assertj.core.api.Assertions.assertThat; - -public class AnalyzerOptimizerTest { - - DefaultFileSystem fs = new DefaultFileSystem(); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private AnalyzerOptimizer optimizer; - - private Settings settings; - - @Before - public void prepare() { - settings = new Settings(); - optimizer = new AnalyzerOptimizer(fs, new ActiveRulesBuilder().build(), settings); - } - - @Test - public void should_run_analyzer_with_no_metadata() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); - - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } - - @Test - public void should_optimize_on_language() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() - .onlyOnLanguages("java", "php"); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java")); - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } - - @Test - public void should_optimize_on_type() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() - .onlyOnFileType(InputFile.Type.MAIN); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setType(InputFile.Type.TEST)); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - fs.add(new DefaultInputFile("foo", "src/Foo.java").setType(InputFile.Type.MAIN)); - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } - - @Test - public void should_optimize_on_both_type_and_language() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() - .onlyOnLanguages("java", "php") - .onlyOnFileType(InputFile.Type.MAIN); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setLanguage("java").setType(InputFile.Type.TEST)); - fs.add(new DefaultInputFile("foo", "src/Foo.cbl").setLanguage("cobol").setType(InputFile.Type.MAIN)); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java").setType(InputFile.Type.MAIN)); - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } - - @Test - public void should_optimize_on_repository() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() - .createIssuesForRuleRepositories("squid"); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - ActiveRules activeRules = new ActiveRulesBuilder() - .create(RuleKey.of("repo1", "foo")) - .activate() - .build(); - optimizer = new AnalyzerOptimizer(fs, activeRules, settings); - - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - activeRules = new ActiveRulesBuilder() - .create(RuleKey.of("repo1", "foo")) - .activate() - .create(RuleKey.of("squid", "rule")) - .activate() - .build(); - optimizer = new AnalyzerOptimizer(fs, activeRules, settings); - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } - - @Test - public void should_optimize_on_settings() throws Exception { - DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() - .requireProperty("sonar.foo.reportPath"); - assertThat(optimizer.shouldExecute(descriptor)).isFalse(); - - settings.setProperty("sonar.foo.reportPath", "foo"); - assertThat(optimizer.shouldExecute(descriptor)).isTrue(); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java new file mode 100644 index 00000000000..fb8e564f11d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java @@ -0,0 +1,131 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.sensor; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.rule.ActiveRules; +import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.config.Settings; +import org.sonar.api.rule.RuleKey; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AnalyzerOptimizerTest { + + DefaultFileSystem fs = new DefaultFileSystem(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + private AnalyzerOptimizer optimizer; + + private Settings settings; + + @Before + public void prepare() { + settings = new Settings(); + optimizer = new AnalyzerOptimizer(fs, new ActiveRulesBuilder().build(), settings); + } + + @Test + public void should_run_analyzer_with_no_metadata() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); + + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } + + @Test + public void should_optimize_on_language() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() + .onlyOnLanguages("java", "php"); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java")); + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } + + @Test + public void should_optimize_on_type() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() + .onlyOnFileType(InputFile.Type.MAIN); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setType(InputFile.Type.TEST)); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + fs.add(new DefaultInputFile("foo", "src/Foo.java").setType(InputFile.Type.MAIN)); + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } + + @Test + public void should_optimize_on_both_type_and_language() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() + .onlyOnLanguages("java", "php") + .onlyOnFileType(InputFile.Type.MAIN); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setLanguage("java").setType(InputFile.Type.TEST)); + fs.add(new DefaultInputFile("foo", "src/Foo.cbl").setLanguage("cobol").setType(InputFile.Type.MAIN)); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java").setType(InputFile.Type.MAIN)); + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } + + @Test + public void should_optimize_on_repository() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() + .createIssuesForRuleRepositories("squid"); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + ActiveRules activeRules = new ActiveRulesBuilder() + .create(RuleKey.of("repo1", "foo")) + .activate() + .build(); + optimizer = new AnalyzerOptimizer(fs, activeRules, settings); + + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + activeRules = new ActiveRulesBuilder() + .create(RuleKey.of("repo1", "foo")) + .activate() + .create(RuleKey.of("squid", "rule")) + .activate() + .build(); + optimizer = new AnalyzerOptimizer(fs, activeRules, settings); + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } + + @Test + public void should_optimize_on_settings() throws Exception { + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() + .requireProperty("sonar.foo.reportPath"); + assertThat(optimizer.shouldExecute(descriptor)).isFalse(); + + settings.setProperty("sonar.foo.reportPath", "foo"); + assertThat(optimizer.shouldExecute(descriptor)).isTrue(); + } +}