From c3174fada2ab193d03661209bee3291772b8f347 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 18 Jul 2014 12:18:39 +0200 Subject: [PATCH] SONAR-5417 Get active rules using batch protocol --- .../qualityprofile/db/ActiveRuleDao.java | 13 +- .../QProfileRepositoryExporterTest.java | 8 +- sonar-batch-protocol/pom.xml | 4 + .../batch/protocol/input/ActiveRule.java | 73 +++++++++ .../protocol/input/ProjectReferentials.java | 27 +++- .../input/{QProfiles.java => QProfile.java} | 46 +++++- .../batch/protocol/input}/package-info.java | 7 +- .../input/ProjectReferentialsTest.java | 15 +- .../batch/bootstrap/BootstrapContainer.java | 24 +-- ....java => DefaultLanguagesReferential.java} | 4 +- .../mediumtest/AnalyzerMediumTester.java | 133 +++-------------- .../DefaultProjectReferentialsLoader.java | 123 ++++++++++++++- .../ProjectReferentialsLoader.java | 6 +- .../ProjectReferentialsProvider.java | 27 ++-- .../sonar/batch/rule/ActiveRulesProvider.java | 60 +++----- .../org/sonar/batch/rule/ModuleQProfiles.java | 44 +----- .../rules/DefaultQProfileReferential.java | 52 ------- .../batch/rules/QProfilesReferential.java | 40 ----- .../batch/scan/ProjectScanContainer.java | 18 ++- .../batch/scan2/DefaultAnalyzerContext.java | 5 +- .../batch/scan2/ModuleScanContainer.java | 2 + .../batch/scan2/ProjectScanContainer.java | 14 +- .../mediumtest/fs/FileSystemMediumTest.java | 6 +- .../mediumtest/issues/IssuesMediumTest.java | 6 +- .../measures/MeasuresMediumTest.java | 2 - .../batch/rule/ActiveRulesProviderTest.java | 83 ----------- .../sonar/batch/rule/ModuleQProfilesTest.java | 141 ------------------ .../batch/scan/LanguageVerifierTest.java | 4 +- .../batch/scan/ProjectScanContainerTest.java | 17 ++- .../LanguageDetectionFactoryTest.java | 4 +- .../filesystem/LanguageDetectionTest.java | 20 +-- .../core/qualityprofile/db/ActiveRuleDto.java | 2 +- .../org/sonar/core/rule/CacheRuleFinder.java | 4 + .../java/org/sonar/api/batch/rule/Rules.java | 4 +- 34 files changed, 416 insertions(+), 622 deletions(-) create mode 100644 sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java rename sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/{QProfiles.java => QProfile.java} (52%) rename {sonar-batch/src/main/java/org/sonar/batch/rules => sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input}/package-info.java (87%) rename sonar-batch/src/main/java/org/sonar/batch/languages/{DeprecatedLanguagesReferential.java => DefaultLanguagesReferential.java} (93%) rename sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java => sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java (62%) delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java index 9538f515703..3ecb31a6e4f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java @@ -41,6 +41,7 @@ import org.sonar.server.search.action.IndexAction; import org.sonar.server.search.action.KeyIndexAction; import javax.annotation.CheckForNull; + import java.sql.Timestamp; import java.util.Date; import java.util.List; @@ -58,7 +59,7 @@ public class ActiveRuleDao extends BaseDao activeRuleArgument = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao).insert(activeRuleArgument.capture(), eq(session)); - assertThat(activeRuleArgument.getValue().getRulId()).isEqualTo(10); + assertThat(activeRuleArgument.getValue().getRuleId()).isEqualTo(10); assertThat(activeRuleArgument.getValue().getSeverityString()).isEqualTo(Severity.BLOCKER); ArgumentCaptor activeRuleParamArgument = ArgumentCaptor.forClass(ActiveRuleParamDto.class); diff --git a/sonar-batch-protocol/pom.xml b/sonar-batch-protocol/pom.xml index b57fa36fb4f..e596ef0ab90 100644 --- a/sonar-batch-protocol/pom.xml +++ b/sonar-batch-protocol/pom.xml @@ -22,6 +22,10 @@ com.google.code.gson gson + + com.google.code.findbugs + jsr305 + diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java new file mode 100644 index 00000000000..144a34b292b --- /dev/null +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java @@ -0,0 +1,73 @@ +/* + * 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.protocol.input; + +import javax.annotation.CheckForNull; + +import java.util.HashMap; +import java.util.Map; + +public class ActiveRule { + private final String repositoryKey, ruleKey; + private final String severity, internalKey, language; + private final Map params = new HashMap(); + + public ActiveRule(String repositoryKey, String ruleKey, String severity, String internalKey, String language) { + this.repositoryKey = repositoryKey; + this.ruleKey = ruleKey; + this.severity = severity; + this.internalKey = internalKey; + this.language = language; + } + + public String repositoryKey() { + return repositoryKey; + } + + public String ruleKey() { + return ruleKey; + } + + public String severity() { + return severity; + } + + public String language() { + return language; + } + + @CheckForNull + public String param(String key) { + return params.get(key); + } + + public ActiveRule addParam(String key, String value) { + params.put(key, value); + return this; + } + + public Map params() { + return params; + } + + public String internalKey() { + return internalKey; + } +} diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java index bff1a434069..c813b6c3f60 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java @@ -24,21 +24,38 @@ import com.google.gson.Gson; import java.io.Reader; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; public class ProjectReferentials { private long timestamp; - private Collection languages = new ArrayList(); private Collection metrics = new ArrayList(); - - public Collection languages() { - return languages; - } + private Map qprofilesByLanguage = new HashMap(); + private Collection activeRules = new ArrayList(); public Collection metrics() { return metrics; } + public Collection qProfiles() { + return qprofilesByLanguage.values(); + } + + public ProjectReferentials addQProfile(QProfile qProfile) { + qprofilesByLanguage.put(qProfile.language(), qProfile); + return this; + } + + public Collection activeRules() { + return activeRules; + } + + public ProjectReferentials addActiveRule(ActiveRule activeRule) { + activeRules.add(activeRule); + return this; + } + public long timestamp() { return timestamp; } diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java similarity index 52% rename from sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java rename to sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java index 66f4edf9e0a..4c6b0191046 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java @@ -19,15 +19,51 @@ */ package org.sonar.batch.protocol.input; -import java.util.Map; +import java.util.Date; -public class QProfiles { +public class QProfile { - public static class QProfile { + private final String key, name, language; + private final Date rulesUpdatedAt; - private String key, name, language; + public QProfile(String key, String name, String language, Date rulesUpdatedAt) { + this.key = key; + this.name = name; + this.language = language; + this.rulesUpdatedAt = rulesUpdatedAt; } - private Map byLanguage; + public String key() { + return key; + } + + public String name() { + return name; + } + public String language() { + return language; + } + + public Date rulesUpdatedAt() { + return rulesUpdatedAt; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + QProfile qProfile = (QProfile) o; + return key.equals(qProfile.key); + } + + @Override + public int hashCode() { + return key.hashCode(); + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java similarity index 87% rename from sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java rename to sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java index 1522091f4eb..a545fa5fdde 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java @@ -17,5 +17,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.rules; +@ParametersAreNonnullByDefault +package org.sonar.batch.protocol.input; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java index e4d5142f10d..cb5bc33d23e 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java @@ -24,23 +24,32 @@ import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; import java.io.StringReader; +import java.text.SimpleDateFormat; import static org.fest.assertions.Assertions.assertThat; public class ProjectReferentialsTest { @Test - public void testToJson() throws JSONException { + public void testToJson() throws Exception { ProjectReferentials ref = new ProjectReferentials(); ref.metrics().add(new Metric("ncloc", "INT")); + ref.addQProfile(new QProfile("squid-java", "Java", "java", new SimpleDateFormat("dd/MM/yyyy").parse("14/03/1984"))); System.out.println(ref.toJson()); - JSONAssert.assertEquals("{timestamp:0,languages:[],metrics:[{key:ncloc,valueType:INT}]}", ref.toJson(), true); + JSONAssert + .assertEquals( + "{timestamp:0,metrics:[{key:ncloc,valueType:INT}]," + + "qprofilesByLanguage:{java:{key:\"squid-java\"," + + "name:Java," + + "language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}}," + + "activeRules:[]}", + ref.toJson(), true); } @Test public void testFromJson() throws JSONException { - ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,languages:[],metrics:[{key:ncloc,valueType:INT}]}")); + ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,metrics:[{key:ncloc,valueType:INT}]}")); assertThat(ref.timestamp()).isEqualTo(1); Metric metric = ref.metrics().iterator().next(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index 7f89a3b0eaf..daa6210cd1f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -25,25 +25,19 @@ import org.sonar.api.Plugin; import org.sonar.api.config.EmailSettings; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.PluginMetadata; -import org.sonar.api.rules.RuleFinder; import org.sonar.api.utils.Durations; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; -import org.sonar.batch.components.PastMeasuresLoader; import org.sonar.batch.components.PastSnapshotFinder; import org.sonar.batch.components.PastSnapshotFinderByDate; import org.sonar.batch.components.PastSnapshotFinderByDays; import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; import org.sonar.batch.components.PastSnapshotFinderByVersion; -import org.sonar.batch.debt.DebtModelProvider; import org.sonar.batch.referential.DefaultProjectReferentialsLoader; import org.sonar.batch.referential.ProjectReferentialsLoader; -import org.sonar.batch.rule.RulesProvider; -import org.sonar.batch.rules.DefaultQProfileReferential; -import org.sonar.batch.rules.QProfilesReferential; import org.sonar.batch.settings.DefaultSettingsReferential; import org.sonar.batch.settings.SettingsReferential; import org.sonar.core.cluster.NullQueue; @@ -110,20 +104,14 @@ public class BootstrapContainer extends ComponentContainer { UriReader.class, new FileCacheProvider(), System2.INSTANCE); - if (getComponentByType(ProjectReferentialsLoader.class) == null) { - add(DefaultProjectReferentialsLoader.class); - } if (getComponentByType(SettingsReferential.class) == null) { add(DefaultSettingsReferential.class); } if (getComponentByType(PluginsReferential.class) == null) { add(DefaultPluginsReferential.class); } - if (getComponentByType(RuleFinder.class) == null) { - add(CacheRuleFinder.class); - } - if (getComponentByType(QProfilesReferential.class) == null) { - add(DefaultQProfileReferential.class); + if (getComponentByType(ProjectReferentialsLoader.class) == null) { + add(DefaultProjectReferentialsLoader.class); } } @@ -141,7 +129,8 @@ public class BootstrapContainer extends ComponentContainer { JpaDatabaseSession.class, BatchDatabaseSessionFactory.class, DaoUtils.getDaoClasses(), - PurgeProfiler.class); + PurgeProfiler.class, + CacheRuleFinder.class); } /** @@ -161,11 +150,8 @@ public class BootstrapContainer extends ComponentContainer { PastSnapshotFinderByPreviousAnalysis.class, PastSnapshotFinderByVersion.class, PastSnapshotFinderByPreviousVersion.class, - PastMeasuresLoader.class, PastSnapshotFinder.class, - Durations.class, - new DebtModelProvider(), - new RulesProvider()); + Durations.class); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java similarity index 93% rename from sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java rename to sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java index 23591b15aec..e9099c197cc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java @@ -30,11 +30,11 @@ import java.util.Collection; * Languages referential using {@link Languages} * @since 4.4 */ -public class DeprecatedLanguagesReferential implements LanguagesReferential { +public class DefaultLanguagesReferential implements LanguagesReferential { private Languages languages; - public DeprecatedLanguagesReferential(Languages languages) { + public DefaultLanguagesReferential(Languages languages) { this.languages = languages; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java index 13d43e7846b..8aa68596d07 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java @@ -19,33 +19,24 @@ */ package org.sonar.batch.mediumtest; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; -import com.google.common.collect.Maps; import org.apache.commons.io.IOUtils; import org.sonar.api.SonarPlugin; import org.sonar.api.batch.analyzer.issue.AnalyzerIssue; import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure; +import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.batch.debt.internal.DefaultDebtModel; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; -import org.sonar.api.batch.rule.internal.RulesBuilder; +import org.sonar.api.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.platform.PluginMetadata; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RuleQuery; +import org.sonar.api.resources.Languages; import org.sonar.batch.bootstrap.PluginsReferential; import org.sonar.batch.bootstrapper.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.batch.languages.Language; -import org.sonar.batch.languages.LanguagesReferential; +import org.sonar.batch.protocol.input.ActiveRule; import org.sonar.batch.protocol.input.ProjectReferentials; import org.sonar.batch.referential.ProjectReferentialsLoader; -import org.sonar.batch.rule.QProfile; -import org.sonar.batch.rules.QProfilesReferential; import org.sonar.batch.scan.filesystem.InputFileCache; import org.sonar.batch.scan2.AnalyzerIssueCache; import org.sonar.batch.scan2.AnalyzerMeasureCache; @@ -58,7 +49,6 @@ import org.sonar.core.plugins.RemotePlugin; import java.io.File; import java.io.FileReader; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -78,13 +68,7 @@ public class AnalyzerMediumTester { private final FakeProjectReferentialsLoader refProvider = new FakeProjectReferentialsLoader(); private final FakeSettingsReferential settingsReferential = new FakeSettingsReferential(); private final FackPluginsReferential pluginsReferential = new FackPluginsReferential(); - private final FakeRuleFinder ruleFinder = new FakeRuleFinder(); - private final FakeQProfileReferential qProfileReferential = new FakeQProfileReferential(); - private final FakeLanguageReferential languageReferential = new FakeLanguageReferential(); private final Map bootstrapProperties = new HashMap(); - private final RulesBuilder rulesBuilder = new RulesBuilder(); - private final ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); - private int metricId = 1; public AnalyzerMediumTester build() { return new AnalyzerMediumTester(this); @@ -109,12 +93,11 @@ public class AnalyzerMediumTester { public AnalyzerMediumTesterBuilder registerMetric(Metric metric) { refProvider.add(metric); - metricId++; return this; } public AnalyzerMediumTesterBuilder addQProfile(String language, String name) { - qProfileReferential.add(new QProfile().setKey(name).setName(name).setLanguage(language).setRulesUpdatedAt(new Date())); + refProvider.addQProfile(language, name); return this; } @@ -124,24 +107,13 @@ public class AnalyzerMediumTester { return this; } - public AnalyzerMediumTesterBuilder registerLanguage(org.sonar.api.resources.Language... languages) { - languageReferential.register(languages); - return this; - } - public AnalyzerMediumTesterBuilder bootstrapProperties(Map props) { bootstrapProperties.putAll(props); return this; } - public AnalyzerMediumTesterBuilder activateRule(RuleKey key) { - rulesBuilder.add(key); - activeRulesBuilder.create(key).activate(); - return this; - } - - public AnalyzerMediumTesterBuilder registerInactiveRule(RuleKey key) { - rulesBuilder.add(key); + public AnalyzerMediumTesterBuilder activateRule(ActiveRule activeRule) { + refProvider.addActiveRule(activeRule); return this; } @@ -163,12 +135,7 @@ public class AnalyzerMediumTester { builder.settingsReferential, builder.pluginsReferential, builder.refProvider, - builder.ruleFinder, - builder.qProfileReferential, - builder.rulesBuilder.build(), - builder.activeRulesBuilder.build(), - new DefaultDebtModel(), - builder.languageReferential) + new DefaultDebtModel()) .setBootstrapProperties(builder.bootstrapProperties) .build(); } @@ -266,14 +233,24 @@ public class AnalyzerMediumTester { private ProjectReferentials ref = new ProjectReferentials(); @Override - public ProjectReferentials load(String projectKey) { + public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) { return ref; } + public FakeProjectReferentialsLoader addQProfile(String language, String name) { + ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(name, name, language, new Date())); + return this; + } + public FakeProjectReferentialsLoader add(Metric metric) { ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metric.key(), metric.getType().name())); return this; } + + public FakeProjectReferentialsLoader addActiveRule(ActiveRule activeRule) { + ref.addActiveRule(activeRule); + return this; + } } private static class FakeSettingsReferential implements SettingsReferential { @@ -328,76 +305,4 @@ public class AnalyzerMediumTester { } - private static class FakeRuleFinder implements RuleFinder { - private BiMap rulesById = HashBiMap.create(); - private Map> rulesByRepoKeyAndRuleKey = Maps.newHashMap(); - - @Override - public Rule findById(int ruleId) { - return rulesById.get(ruleId); - } - - @Override - public Rule findByKey(String repositoryKey, String ruleKey) { - Map repository = rulesByRepoKeyAndRuleKey.get(repositoryKey); - return repository != null ? repository.get(ruleKey) : null; - } - - @Override - public Rule findByKey(RuleKey key) { - return findByKey(key.repository(), key.rule()); - } - - @Override - public Rule find(RuleQuery query) { - throw new UnsupportedOperationException(); - } - - @Override - public Collection findAll(RuleQuery query) { - throw new UnsupportedOperationException(); - } - } - - private static class FakeQProfileReferential implements QProfilesReferential { - - private Map> profiles = new HashMap>(); - - @Override - public QProfile get(String language, String name) { - return profiles.get(language).get(name); - } - - public void add(QProfile qprofile) { - if (!profiles.containsKey(qprofile.getLanguage())) { - profiles.put(qprofile.getLanguage(), new HashMap()); - } - profiles.get(qprofile.getLanguage()).put(qprofile.getName(), qprofile); - } - - } - - private static class FakeLanguageReferential implements LanguagesReferential { - - private Map languages = new HashMap(); - - public FakeLanguageReferential register(org.sonar.api.resources.Language... languages) { - for (org.sonar.api.resources.Language language : languages) { - this.languages.put(language.getKey(), new Language(language.getKey(), language.getName(), language.getFileSuffixes())); - } - return this; - } - - @Override - public Language get(String languageKey) { - return languages.get(languageKey); - } - - @Override - public Collection all() { - return languages.values(); - } - - } - } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java index 9f6bb327594..d4f15f1fa3f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java @@ -19,30 +19,141 @@ */ package org.sonar.batch.referential; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ListMultimap; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; import org.sonar.api.measures.Metric; +import org.sonar.api.resources.Language; +import org.sonar.api.resources.Languages; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.utils.MessageException; +import org.sonar.batch.protocol.input.ActiveRule; import org.sonar.batch.protocol.input.ProjectReferentials; +import org.sonar.batch.protocol.input.QProfile; +import org.sonar.batch.rule.ModuleQProfiles; +import org.sonar.core.UtcDateUtils; +import org.sonar.core.qualityprofile.db.ActiveRuleDao; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.jpa.session.DatabaseSessionFactory; +import javax.annotation.CheckForNull; + import java.util.Collection; +/** + * TODO This is currently implemented by accessing DB but should be replaced by WS call + */ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader { private static final String ENABLED = "enabled"; - private DatabaseSessionFactory sessionFactory; - protected Collection doFindAll() { - return sessionFactory.getSession().getResults(Metric.class, ENABLED, true); - } + private final DatabaseSessionFactory sessionFactory; + private final QualityProfileDao qualityProfileDao; + private final ActiveRuleDao activeRuleDao; + private final RuleFinder ruleFinder; - public DefaultProjectReferentialsLoader(DatabaseSessionFactory sessionFactory) { + public DefaultProjectReferentialsLoader(DatabaseSessionFactory sessionFactory, QualityProfileDao qualityProfileDao, + ActiveRuleDao activeRuleDao, RuleFinder ruleFinder) { this.sessionFactory = sessionFactory; + this.qualityProfileDao = qualityProfileDao; + this.activeRuleDao = activeRuleDao; + this.ruleFinder = ruleFinder; } - public ProjectReferentials load(String projectKey) { + @Override + public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) { ProjectReferentials ref = new ProjectReferentials(); for (Metric m : sessionFactory.getSession().getResults(Metric.class, ENABLED, true)) { ref.metrics().add(new org.sonar.batch.protocol.input.Metric(m.getKey(), m.getType().name())); } + + String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP); + + for (Language language : languages.all()) { + org.sonar.batch.protocol.input.QProfile profile = null; + if (StringUtils.isNotBlank(defaultName)) { + profile = loadDefaultQProfile(defaultName, language.getKey()); + } + if (profile == null) { + profile = loadQProfile(settings, language.getKey()); + } + if (profile != null) { + ref.addQProfile(profile); + } + } + + for (QProfile qProfile : ref.qProfiles()) { + ListMultimap paramDtosByActiveRuleId = ArrayListMultimap.create(); + for (ActiveRuleParamDto dto : activeRuleDao.selectParamsByProfileKey(qProfile.key())) { + paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto); + } + + for (ActiveRuleDto activeDto : activeRuleDao.selectByProfileKey(qProfile.key())) { + Rule rule = ruleFinder.findById(activeDto.getRuleId()); + if (rule != null) { + String internalKey; + Rule template = rule.getTemplate(); + if (template != null) { + internalKey = template.getConfigKey(); + } else { + internalKey = rule.getConfigKey(); + } + ActiveRule activeRule = new ActiveRule(rule.ruleKey().repository(), rule.ruleKey().rule(), activeDto.getSeverityString(), internalKey, rule.getLanguage()); + + // load parameter values + for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) { + activeRule.params().put(paramDto.getKey(), paramDto.getValue()); + } + + // load default values + for (RuleParam param : rule.getParams()) { + if (!activeRule.params().containsKey(param.getKey())) { + activeRule.params().put(param.getKey(), param.getDefaultValue()); + } + } + + ref.addActiveRule(activeRule); + } + } + } + return ref; } + + @CheckForNull + private QProfile loadQProfile(Settings settings, String language) { + String profileName = settings.getString("sonar.profile." + language); + if (profileName != null) { + QProfile dto = get(language, profileName); + if (dto == null) { + throw MessageException.of(String.format("Quality profile not found : '%s' on language '%s'", profileName, language)); + } + return dto; + } + return null; + } + + @CheckForNull + private QProfile loadDefaultQProfile(String profileName, String language) { + return get(language, profileName); + } + + protected Collection doFindAll() { + return sessionFactory.getSession().getResults(Metric.class, ENABLED, true); + } + + public QProfile get(String language, String name) { + QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language); + if (dto == null) { + return null; + } + return new org.sonar.batch.protocol.input.QProfile(dto.getKey(), dto.getName(), dto.getLanguage(), UtcDateUtils.parseDateTime(dto.getRulesUpdatedAt())); + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java index 3dff3784414..f34e358ceec 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java @@ -19,9 +19,13 @@ */ package org.sonar.batch.referential; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Languages; import org.sonar.batch.protocol.input.ProjectReferentials; public interface ProjectReferentialsLoader { - ProjectReferentials load(String projectKey); + ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages); + } diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java similarity index 62% rename from sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java rename to sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java index 8e1bd467226..75a063eb119 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java @@ -17,26 +17,17 @@ * 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.protocol.input; +package org.sonar.batch.referential; -public class Language { +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Languages; +import org.sonar.batch.protocol.input.ProjectReferentials; - private String key; +public class ProjectReferentialsProvider extends ProviderAdapter { - private String name; - - private String[] suffixes; - - public String key() { - return key; - } - - public String name() { - return name; + public ProjectReferentials provide(ProjectReferentialsLoader loader, ProjectReactor reactor, Settings settings, Languages languages) { + return loader.load(reactor, settings, languages); } - - public String[] suffixes() { - return suffixes; - } - } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java index a4fec80b1f5..9b6b009d3df 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java @@ -19,18 +19,15 @@ */ package org.sonar.batch.rule; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.ListMultimap; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; import org.sonar.api.batch.rule.internal.NewActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RuleParam; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; -import org.sonar.core.qualityprofile.db.ActiveRuleDto; -import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.api.rule.RuleKey; +import org.sonar.batch.protocol.input.ActiveRule; +import org.sonar.batch.protocol.input.ProjectReferentials; + +import java.util.Map.Entry; /** * Loads the rules that are activated on the Quality profiles @@ -40,48 +37,27 @@ public class ActiveRulesProvider extends ProviderAdapter { private ActiveRules singleton = null; - public ActiveRules provide(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) { + public ActiveRules provide(ProjectReferentials ref) { if (singleton == null) { - singleton = load(qProfiles, dao, ruleFinder); + singleton = load(ref); } return singleton; } - private ActiveRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) { + private ActiveRules load(ProjectReferentials ref) { ActiveRulesBuilder builder = new ActiveRulesBuilder(); - for (QProfile qProfile : qProfiles.findAll()) { - ListMultimap paramDtosByActiveRuleId = ArrayListMultimap.create(); - for (ActiveRuleParamDto dto : dao.selectParamsByProfileKey(qProfile.getKey())) { - paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto); - } - - for (ActiveRuleDto activeDto : dao.selectByProfileKey(qProfile.getKey())) { - Rule rule = ruleFinder.findById(activeDto.getRulId()); - if (rule != null) { - NewActiveRule newActiveRule = builder.create(rule.ruleKey()); - newActiveRule.setSeverity(activeDto.getSeverityString()); - newActiveRule.setLanguage(rule.getLanguage()); - Rule template = rule.getTemplate(); - if (template != null) { - newActiveRule.setInternalKey(template.getConfigKey()); - } else { - newActiveRule.setInternalKey(rule.getConfigKey()); - } + for (ActiveRule activeRule : ref.activeRules()) { + NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.repositoryKey(), activeRule.ruleKey())); + newActiveRule.setSeverity(activeRule.severity()); + newActiveRule.setLanguage(activeRule.language()); + newActiveRule.setInternalKey(activeRule.internalKey()); - // load parameter values - for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) { - newActiveRule.setParam(paramDto.getKey(), paramDto.getValue()); - } - - // load default values - for (RuleParam param : rule.getParams()) { - if (!newActiveRule.params().containsKey(param.getKey())) { - newActiveRule.setParam(param.getKey(), param.getDefaultValue()); - } - } - newActiveRule.activate(); - } + // load parameters + for (Entry param : activeRule.params().entrySet()) { + newActiveRule.setParam(param.getKey(), param.getValue()); } + + newActiveRule.activate(); } return builder.build(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java index 9bc04bd9c8a..dba6cd74a9b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java @@ -19,15 +19,9 @@ */ package org.sonar.batch.rule; -import org.sonar.batch.languages.Language; - import com.google.common.collect.ImmutableMap; -import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.LanguagesReferential; -import org.sonar.batch.rules.QProfilesReferential; +import org.sonar.batch.protocol.input.ProjectReferentials; import javax.annotation.CheckForNull; @@ -40,46 +34,18 @@ import java.util.Map; public class ModuleQProfiles implements BatchComponent { public static final String SONAR_PROFILE_PROP = "sonar.profile"; - private final Map byLanguage; - public ModuleQProfiles(Settings settings, LanguagesReferential languages, QProfilesReferential qProfileRef) { + public ModuleQProfiles(ProjectReferentials ref) { ImmutableMap.Builder builder = ImmutableMap.builder(); - String defaultName = settings.getString(SONAR_PROFILE_PROP); - for (Language language : languages.all()) { - QProfile profile = null; - if (StringUtils.isNotBlank(defaultName)) { - profile = loadDefaultQProfile(qProfileRef, defaultName, language.key()); - } - if (profile == null) { - profile = loadQProfile(qProfileRef, settings, language.key()); - } - if (profile != null) { - builder.put(profile.getLanguage(), profile); - } + for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) { + builder.put(qProfile.language(), + new QProfile().setKey(qProfile.key()).setName(qProfile.name()).setLanguage(qProfile.language()).setRulesUpdatedAt(qProfile.rulesUpdatedAt())); } byLanguage = builder.build(); } - @CheckForNull - private QProfile loadQProfile(QProfilesReferential qProfileRef, Settings settings, String language) { - String profileName = settings.getString("sonar.profile." + language); - if (profileName != null) { - QProfile dto = qProfileRef.get(language, profileName); - if (dto == null) { - throw MessageException.of(String.format("Quality profile not found : '%s' on language '%s'", profileName, language)); - } - return dto; - } - return null; - } - - @CheckForNull - private QProfile loadDefaultQProfile(QProfilesReferential qProfileRef, String profileName, String language) { - return qProfileRef.get(language, profileName); - } - public Collection findAll() { return byLanguage.values(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java b/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java deleted file mode 100644 index 8aee3971a80..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java +++ /dev/null @@ -1,52 +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.rules; - -import org.sonar.batch.rule.QProfile; -import org.sonar.core.UtcDateUtils; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; - -/** - * @since 4.4 - */ -public class DefaultQProfileReferential implements QProfilesReferential { - - private QualityProfileDao qualityProfileDao; - - public DefaultQProfileReferential(QualityProfileDao qualityProfileDao) { - this.qualityProfileDao = qualityProfileDao; - } - - @Override - public QProfile get(String language, String name) { - QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language); - if (dto == null) { - return null; - } - QProfile profile = new QProfile(); - profile.setKey(dto.getKey()); - profile.setName(dto.getName()); - profile.setLanguage(dto.getLanguage()); - profile.setRulesUpdatedAt(UtcDateUtils.parseDateTime(dto.getRulesUpdatedAt())); - return profile; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java deleted file mode 100644 index 5d50cf33242..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java +++ /dev/null @@ -1,40 +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.rules; - -import org.sonar.batch.rule.QProfile; - -import org.sonar.api.BatchComponent; - -import javax.annotation.CheckForNull; - -/** - * Quality profiles referential - * @since 4.4 - */ -public interface QProfilesReferential extends BatchComponent { - - /** - * Get quality profile - */ - @CheckForNull - QProfile get(String language, String name); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index e7b22e32a1b..911aede44cc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -39,7 +39,9 @@ import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; import org.sonar.batch.bootstrap.MetricProvider; +import org.sonar.batch.components.PastMeasuresLoader; import org.sonar.batch.components.PeriodsDefinition; +import org.sonar.batch.debt.DebtModelProvider; import org.sonar.batch.debt.IssueChangelogDebtCalculator; import org.sonar.batch.index.Caches; import org.sonar.batch.index.ComponentDataCache; @@ -60,10 +62,11 @@ import org.sonar.batch.issue.DeprecatedViolations; import org.sonar.batch.issue.IssueCache; import org.sonar.batch.issue.IssuePersister; import org.sonar.batch.issue.ScanIssueStorage; -import org.sonar.batch.languages.DeprecatedLanguagesReferential; +import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.phases.GraphPersister; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; -import org.sonar.batch.referential.ProjectReferentialsLoader; +import org.sonar.batch.referential.ProjectReferentialsProvider; +import org.sonar.batch.rule.RulesProvider; import org.sonar.batch.scan.filesystem.InputFileCache; import org.sonar.batch.scan.maven.FakeMavenPluginExecutor; import org.sonar.batch.scan.maven.MavenPluginExecutor; @@ -123,13 +126,11 @@ public class ProjectScanContainer extends ComponentContainer { } add(reactor); } - ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class); - add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch())); - } private void addBatchComponents() { add( + new ProjectReferentialsProvider(), DefaultResourceCreationLock.class, DefaultPersistenceManager.class, DependencyPersister.class, @@ -179,7 +180,7 @@ public class ProjectScanContainer extends ComponentContainer { // lang Languages.class, - DeprecatedLanguagesReferential.class, + DefaultLanguagesReferential.class, HighlightableBuilder.class, SymbolizableBuilder.class, @@ -193,6 +194,11 @@ public class ProjectScanContainer extends ComponentContainer { MeasureCache.class, DeprecatedMetricFinder.class, DefaultMetricFinder.class, + PastMeasuresLoader.class, + + // Rules + new RulesProvider(), + new DebtModelProvider(), ProjectSettings.class); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java index fe9c898755d..7c495df931c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java @@ -33,7 +33,6 @@ import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.Rules; import org.sonar.api.config.Settings; import org.sonar.batch.issue.IssueFilters; import org.sonar.batch.scan.AnalyzerContextAdaptor; @@ -50,10 +49,9 @@ public class DefaultAnalyzerContext implements AnalyzerContext { private final FileSystem fs; private final ActiveRules activeRules; private final IssueFilters issueFilters; - private final Rules rules; public DefaultAnalyzerContext(ProjectDefinition def, AnalyzerMeasureCache measureCache, AnalyzerIssueCache issueCache, - Settings settings, FileSystem fs, ActiveRules activeRules, IssueFilters issueFilters, Rules rules) { + Settings settings, FileSystem fs, ActiveRules activeRules, IssueFilters issueFilters) { this.def = def; this.measureCache = measureCache; this.issueCache = issueCache; @@ -61,7 +59,6 @@ public class DefaultAnalyzerContext implements AnalyzerContext { this.fs = fs; this.activeRules = activeRules; this.issueFilters = issueFilters; - this.rules = rules; } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java index d4fc0d708fd..0dcfcc18a88 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java @@ -41,6 +41,7 @@ import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader; import org.sonar.batch.issue.ignore.scanner.IssueExclusionsRegexpScanner; +import org.sonar.batch.rule.ActiveRulesProvider; import org.sonar.batch.rule.ModuleQProfiles; import org.sonar.batch.rule.QProfileVerifier; import org.sonar.batch.scan.LanguageVerifier; @@ -106,6 +107,7 @@ public class ModuleScanContainer extends ComponentContainer { // rules ModuleQProfiles.class, + new ActiveRulesProvider(), CheckFactory.class, // issues diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java index 097f835afd8..a2b602f166a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java @@ -28,13 +28,17 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; import org.sonar.api.platform.ComponentContainer; +import org.sonar.api.resources.Languages; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; import org.sonar.batch.index.Caches; +import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; +import org.sonar.batch.referential.DefaultProjectReferentialsLoader; import org.sonar.batch.referential.ProjectReferentialsLoader; +import org.sonar.batch.referential.ProjectReferentialsProvider; import org.sonar.batch.scan.ProjectReactorBuilder; import org.sonar.batch.scan.ProjectSettings; import org.sonar.batch.scan.filesystem.InputFileCache; @@ -76,15 +80,21 @@ public class ProjectScanContainer extends ComponentContainer { throw new IllegalStateException(bootstrapper + " has returned null as ProjectReactor"); } add(reactor); - ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class); - add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch())); + if (getComponentByType(ProjectReferentialsLoader.class) == null) { + add(DefaultProjectReferentialsLoader.class); + } } private void addBatchComponents() { add( + new ProjectReferentialsProvider(), ProjectSettings.class, Caches.class, + // lang + Languages.class, + DefaultLanguagesReferential.class, + // Measures DefaultMetricFinder.class, AnalyzerMeasureCache.class, diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java index 343bd741713..d572ef1b383 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java @@ -28,12 +28,11 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.MessageException; import org.sonar.batch.mediumtest.AnalyzerMediumTester; import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult; import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin; -import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo; +import org.sonar.batch.protocol.input.ActiveRule; import java.io.File; import java.io.IOException; @@ -50,9 +49,8 @@ public class FileSystemMediumTest { public AnalyzerMediumTester tester = AnalyzerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) - .registerLanguage(new Xoo()) .addDefaultQProfile("xoo", "Sonar Way") - .activateRule(RuleKey.of("xoo", "OneIssuePerLine")) + .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "xoo", "xoo")) .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java index 75663d74382..26cc47ff793 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java @@ -27,11 +27,10 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.analyzer.issue.AnalyzerIssue; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.rule.RuleKey; import org.sonar.batch.mediumtest.AnalyzerMediumTester; import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult; import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin; -import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo; +import org.sonar.batch.protocol.input.ActiveRule; import java.io.File; import java.io.IOException; @@ -45,9 +44,8 @@ public class IssuesMediumTest { public AnalyzerMediumTester tester = AnalyzerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) - .registerLanguage(new Xoo()) .addDefaultQProfile("xoo", "Sonar Way") - .activateRule(RuleKey.of("xoo", "OneIssuePerLine")) + .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "xoo", "xoo")) .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java index 017f530d521..31064275b70 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java @@ -31,7 +31,6 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.batch.mediumtest.AnalyzerMediumTester; import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult; import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin; -import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo; import java.io.File; import java.io.IOException; @@ -45,7 +44,6 @@ public class MeasuresMediumTest { public AnalyzerMediumTester tester = AnalyzerMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) - .registerLanguage(new Xoo()) .addDefaultQProfile("xoo", "Sonar Way") .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java deleted file mode 100644 index 25576c8c6fd..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java +++ /dev/null @@ -1,83 +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.rule; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.batch.rule.ActiveRule; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.Severity; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.core.persistence.AbstractDaoTestCase; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; - -import java.util.Arrays; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class ActiveRulesProviderTest extends AbstractDaoTestCase { - - ModuleQProfiles qProfiles = mock(ModuleQProfiles.class); - RuleFinder ruleFinder = mock(RuleFinder.class); - - @Before - public void init_rules() { - Rule squidRule = new Rule().setRepositoryKey("squid").setKey("S0001"); - squidRule.createParameter("min").setDefaultValue("12"); - when(ruleFinder.findById(10)).thenReturn(squidRule); - when(ruleFinder.findById(100)).thenReturn(new Rule().setRepositoryKey("phpunit").setKey("P1")); - } - - @Test - public void build_active_rules() throws Exception { - setupData("shared"); - when(qProfiles.findAll()).thenReturn(Arrays.asList( - // 1 rule is enabled on java with severity INFO - new QProfile().setKey("java-two").setName("Java Two").setLanguage("java"), - // 1 rule is enabled on php with severity BLOCKER - new QProfile().setKey("php-one").setName("Php One").setLanguage("php") - )); - - ActiveRulesProvider provider = new ActiveRulesProvider(); - ActiveRuleDao activeRuleDao = new ActiveRuleDao(getMyBatis()); - ActiveRules activeRules = provider.provide(qProfiles, activeRuleDao, ruleFinder); - - assertThat(activeRules.findAll()).hasSize(2); - assertThat(activeRules.findByRepository("squid")).hasSize(1); - assertThat(activeRules.findByRepository("phpunit")).hasSize(1); - assertThat(activeRules.findByRepository("unknown")).isEmpty(); - ActiveRule squidRule = activeRules.find(RuleKey.of("squid", "S0001")); - assertThat(squidRule.severity()).isEqualTo(Severity.INFO); - assertThat(squidRule.internalKey()).isNull(); - // "max" and "format" parameters are set in db, "min" is not set but has a default value - assertThat(squidRule.params()).hasSize(3); - assertThat(squidRule.param("min")).isEqualTo("12"); - assertThat(squidRule.param("max")).isEqualTo("20"); - assertThat(squidRule.param("format")).isEqualTo("html"); - - ActiveRule phpRule = activeRules.find(RuleKey.of("phpunit", "P1")); - assertThat(phpRule.severity()).isEqualTo(Severity.BLOCKER); - assertThat(phpRule.params()).isEmpty(); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java deleted file mode 100644 index 8c7d2d2bafc..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java +++ /dev/null @@ -1,141 +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.rule; - -import com.google.common.collect.Lists; -import org.junit.Test; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Languages; -import org.sonar.api.utils.MessageException; -import org.sonar.api.utils.System2; -import org.sonar.batch.languages.DeprecatedLanguagesReferential; -import org.sonar.batch.languages.LanguagesReferential; -import org.sonar.batch.rules.DefaultQProfileReferential; -import org.sonar.batch.rules.QProfilesReferential; -import org.sonar.core.persistence.AbstractDaoTestCase; -import org.sonar.core.qualityprofile.db.QualityProfileDao; - -import java.util.List; - -import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; - -public class ModuleQProfilesTest extends AbstractDaoTestCase { - - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new SimpleLanguage("java"), new SimpleLanguage("php"))); - Settings settings = new Settings(); - - @Test - public void find_profiles() throws Exception { - // 4 profiles in db - setupData("shared"); - QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE); - QProfilesReferential ref = new DefaultQProfileReferential(dao); - - settings.setProperty("sonar.profile.java", "Java One"); - settings.setProperty("sonar.profile.abap", "Abap One"); - settings.setProperty("sonar.profile.php", "Php One"); - - ModuleQProfiles moduleQProfiles = new ModuleQProfiles(settings, languages, ref); - List qProfiles = Lists.newArrayList(moduleQProfiles.findAll()); - - // load only the profiles of languages detected in project - assertThat(qProfiles).hasSize(2); - assertThat(moduleQProfiles.findByLanguage("java")).isNotNull(); - assertThat(moduleQProfiles.findByLanguage("php")).isNotNull(); - assertThat(moduleQProfiles.findByLanguage("abap")).isNull(); - QProfile javaProfile = qProfiles.get(0); - assertThat(javaProfile.getKey()).isEqualTo("java-one"); - assertThat(javaProfile.getName()).isEqualTo("Java One"); - assertThat(javaProfile.getLanguage()).isEqualTo("java"); - QProfile phpProfile = qProfiles.get(1); - assertThat(phpProfile.getKey()).isEqualTo("php-one"); - assertThat(phpProfile.getName()).isEqualTo("Php One"); - assertThat(phpProfile.getLanguage()).isEqualTo("php"); - } - - @Test - public void supported_deprecated_property() throws Exception { - setupData("shared"); - QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE); - QProfilesReferential ref = new DefaultQProfileReferential(dao); - - // deprecated property - settings.setProperty("sonar.profile", "Java Two"); - settings.setProperty("sonar.profile.php", "Php One"); - - ModuleQProfiles moduleQProfiles = new ModuleQProfiles(settings, languages, ref); - List qProfiles = Lists.newArrayList(moduleQProfiles.findAll()); - - assertThat(qProfiles).hasSize(2); - QProfile javaProfile = qProfiles.get(0); - assertThat(javaProfile.getKey()).isEqualTo("java-two"); - assertThat(javaProfile.getName()).isEqualTo("Java Two"); - assertThat(javaProfile.getLanguage()).isEqualTo("java"); - - // "Java Two" does not exist for PHP -> fallback to sonar.profile.php - QProfile phpProfile = qProfiles.get(1); - assertThat(phpProfile.getKey()).isEqualTo("php-one"); - assertThat(phpProfile.getName()).isEqualTo("Php One"); - assertThat(phpProfile.getLanguage()).isEqualTo("php"); - } - - @Test - public void fail_if_unknown_profile() throws Exception { - setupData("shared"); - QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE); - QProfilesReferential ref = new DefaultQProfileReferential(dao); - - settings.setProperty("sonar.profile.java", "Unknown"); - settings.setProperty("sonar.profile.php", "Php One"); - - try { - new ModuleQProfiles(settings, languages, ref); - fail(); - } catch (MessageException e) { - assertThat(e).hasMessage("Quality profile not found : 'Unknown' on language 'java'"); - } - } - - private static class SimpleLanguage implements Language { - - private final String key; - - private SimpleLanguage(String key) { - this.key = key; - } - - @Override - public String getKey() { - return key; - } - - @Override - public String getName() { - return key; - } - - @Override - public String[] getFileSuffixes() { - return new String[0]; - } - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java index 30b6b571883..eb19d5428ea 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java @@ -27,7 +27,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.DeprecatedLanguagesReferential; +import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.languages.LanguagesReferential; import static org.fest.assertions.Assertions.assertThat; @@ -35,7 +35,7 @@ import static org.fest.assertions.Assertions.assertThat; public class LanguageVerifierTest { Settings settings = new Settings(); - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(Java.INSTANCE)); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE)); DefaultFileSystem fs = new DefaultFileSystem(); @Rule diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java index 0cf0be9b57e..e3824aa70f1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java @@ -32,6 +32,7 @@ import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.platform.ComponentContainer; +import org.sonar.api.resources.Languages; import org.sonar.api.task.TaskExtension; import org.sonar.api.utils.System2; import org.sonar.batch.bootstrap.AnalysisMode; @@ -39,6 +40,8 @@ import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; +import org.sonar.batch.protocol.input.ProjectReferentials; +import org.sonar.batch.referential.ProjectReferentialsLoader; import org.sonar.batch.scan.maven.MavenPluginExecutor; import org.sonar.batch.settings.SettingsReferential; @@ -62,14 +65,22 @@ public class ProjectScanContainerTest { bootstrapProperties = new BootstrapProperties(Collections.emptyMap()); AnalysisMode analysisMode = new AnalysisMode(bootstrapProperties); when(projectBootstrapper.bootstrap()).thenReturn(new ProjectReactor(ProjectDefinition.create())); - settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), mock(SettingsReferential.class), new PropertiesConfiguration(), analysisMode); parentContainer = new ComponentContainer(); - parentContainer.add(settings); parentContainer.add(System2.INSTANCE); parentContainer.add(bootstrapProperties); parentContainer.add(analysisMode); parentContainer.add(new PropertiesConfiguration()); - parentContainer.add(mock(SettingsReferential.class)); + SettingsReferential settingsReferential = mock(SettingsReferential.class); + settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), settingsReferential, new PropertiesConfiguration(), analysisMode); + parentContainer.add(settings); + parentContainer.add(settingsReferential); + ProjectReferentialsLoader projectReferentialsLoader = new ProjectReferentialsLoader() { + @Override + public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) { + return new ProjectReferentials(); + } + }; + parentContainer.add(projectReferentialsLoader); container = new ProjectScanContainer(parentContainer); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java index cda01e16881..2cb4b862f1a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java @@ -23,7 +23,7 @@ import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; -import org.sonar.batch.languages.DeprecatedLanguagesReferential; +import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.languages.LanguagesReferential; import static org.fest.assertions.Assertions.assertThat; @@ -31,7 +31,7 @@ import static org.fest.assertions.Assertions.assertThat; public class LanguageDetectionFactoryTest { @Test public void testCreate() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(Java.INSTANCE)); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE)); LanguageDetectionFactory factory = new LanguageDetectionFactory(new Settings(), languages); LanguageDetection languageDetection = factory.create(); assertThat(languageDetection).isNotNull(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java index 7c6eb7ff4f2..b8281a3cdcf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java @@ -30,7 +30,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.DeprecatedLanguagesReferential; +import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.languages.LanguagesReferential; import java.io.File; @@ -58,7 +58,7 @@ public class LanguageDetectionTest { @Test public void search_by_file_extension() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); assertThat(detection.language(newInputFile("Foo.java"))).isEqualTo("java"); @@ -76,13 +76,13 @@ public class LanguageDetectionTest { @Test public void should_not_fail_if_no_language() throws Exception { - LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DeprecatedLanguagesReferential(new Languages()))); + LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DefaultLanguagesReferential(new Languages()))); assertThat(detection.language(newInputFile("Foo.java"))).isNull(); } @Test public void plugin_can_declare_a_file_extension_twice_for_case_sensitivity() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("abap", "abap", "ABAP"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap", "ABAP"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap"); @@ -92,7 +92,7 @@ public class LanguageDetectionTest { public void language_with_no_extension() throws Exception { // abap does not declare any file extensions. // When analyzing an ABAP project, then all source files must be parsed. - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"))); // No side-effect on non-ABAP projects LanguageDetection detection = new LanguageDetection(new Settings(), languages); @@ -110,7 +110,7 @@ public class LanguageDetectionTest { @Test public void force_language_using_deprecated_property() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); Settings settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java"); @@ -126,7 +126,7 @@ public class LanguageDetectionTest { thrown.expect(MessageException.class); thrown.expectMessage("No language is installed with key 'unknown'. Please update property 'sonar.language'"); - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); Settings settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "unknown"); new LanguageDetection(settings, languages); @@ -134,7 +134,7 @@ public class LanguageDetectionTest { @Test public void fail_if_conflicting_language_suffix() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); try { detection.language(newInputFile("abc.xhtml")); @@ -149,7 +149,7 @@ public class LanguageDetectionTest { @Test public void solve_conflict_using_filepattern() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); Settings settings = new Settings(); settings.setProperty("sonar.lang.patterns.xml", "xml/**"); @@ -161,7 +161,7 @@ public class LanguageDetectionTest { @Test public void fail_if_conflicting_filepattern() throws Exception { - LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"))); + LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"))); Settings settings = new Settings(); settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt"); settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt"); diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java index fed0a1078da..b908bb4e6bb 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java @@ -87,7 +87,7 @@ public class ActiveRuleDto extends Dto { return this; } - public Integer getRulId() { + public Integer getRuleId() { return ruleId; } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java index 4c93ba8f977..96aab429ea5 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java @@ -38,6 +38,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +/** + * @deprecated since 4.5 + */ +@Deprecated public final class CacheRuleFinder implements RuleFinder { private BiMap rulesById = HashBiMap.create(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java index 14fb6cb7ac3..3e96bd664bb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch.rule; -import com.google.common.annotations.Beta; import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; @@ -28,8 +27,9 @@ import java.util.Collection; /** * @since 4.2 + * @deprecated since 4.5 use {@link ActiveRules} */ -@Beta +@Deprecated public interface Rules { @CheckForNull -- 2.39.5