*/
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;
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;
*/
package org.sonar.batch.scan;
+import org.sonar.batch.sensor.AnalyzerOptimizer;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
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;
*/
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 {
+++ /dev/null
-/*
- * 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;
- }
-
-}
+++ /dev/null
-/*
- * 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<DefaultIssue> cache;
-
- public IssueCache(Caches caches, InputPathCache inputPathCache) {
- caches.registerValueCoder(DefaultIssue.class, new DefaultIssueValueCoder(inputPathCache));
- cache = caches.createCache("issues");
- }
-
- public Iterable<DefaultIssue> byComponent(String projectKey, String resourceKey) {
- return cache.values(projectKey, resourceKey);
- }
-
- public Iterable<DefaultIssue> all() {
- return cache.values();
- }
-
- public IssueCache put(String projectKey, String resourceKey, DefaultIssue issue) {
- cache.put(projectKey, resourceKey, issue.key(), issue);
- return this;
- }
-
- public Iterable<DefaultIssue> byModule(String projectKey) {
- return cache.values(projectKey);
- }
-
-}
*/
package org.sonar.batch.scan2;
+import org.sonar.batch.sensor.AnalyzerOptimizer;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
--- /dev/null
+/*
+ * 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;
+ }
+
+}
*/
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;
+++ /dev/null
-/*
- * 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();
- }
-}
--- /dev/null
+/*
+ * 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();
+ }
+}