From 8bbac3c6e0023978a59814cd1e77fd0efcb6c265 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 12 Nov 2018 10:53:04 +0100 Subject: [PATCH] SONAR-11471 Stop providing RulesProfile on scanner side --- .../sonar/api/batch/rule/CheckFactory.java | 2 +- .../scanner/report/MetadataPublisher.java | 10 +- ...ider.java => QualityProfilesProvider.java} | 16 +- .../scanner/rule/ActiveRulesProvider.java | 6 +- .../sonar/scanner/rule/QProfileVerifier.java | 6 +- ...uleQProfiles.java => QualityProfiles.java} | 8 +- .../scanner/rule/RulesProfileProvider.java | 95 ------------ .../scanner/rule/RulesProfileWrapper.java | 139 ------------------ .../scanner/scan/ModuleScanContainer.java | 6 - .../scanner/scan/ProjectScanContainer.java | 6 +- .../scanner/report/MetadataPublisherTest.java | 4 +- .../QualityProfileProviderTest.java | 30 ++-- .../scanner/rule/ActiveRulesProviderTest.java | 11 +- .../scanner/rule/QProfileVerifierTest.java | 4 +- .../rule/RulesProfileProviderTest.java | 90 ------------ 15 files changed, 51 insertions(+), 382 deletions(-) rename sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/{QualityProfileProvider.java => QualityProfilesProvider.java} (82%) rename sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/{ModuleQProfiles.java => QualityProfiles.java} (90%) delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileProvider.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileWrapper.java delete mode 100644 sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesProfileProviderTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/CheckFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/CheckFactory.java index ff43207aefc..ae8c5016076 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/CheckFactory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/CheckFactory.java @@ -19,7 +19,7 @@ */ package org.sonar.api.batch.rule; -import org.sonar.api.batch.ScannerSide; +import org.sonar.api.scanner.ScannerSide; /** * Creates {@link org.sonar.api.batch.rule.Checks}. This class is available diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java index 6b5fbc8e15a..73eced44cda 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java @@ -37,7 +37,7 @@ import org.sonar.scanner.cpd.CpdSettings; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.rule.ModuleQProfiles; +import org.sonar.scanner.rule.QualityProfiles; import org.sonar.scanner.rule.QProfile; import org.sonar.scanner.scan.ScanProperties; import org.sonar.scanner.scan.branch.BranchConfiguration; @@ -48,7 +48,7 @@ public class MetadataPublisher implements ReportPublisherStep { private static final Logger LOG = Loggers.get(MetadataPublisher.class); private final ScanProperties properties; - private final ModuleQProfiles qProfiles; + private final QualityProfiles qProfiles; private final ProjectAnalysisInfo projectAnalysisInfo; private final InputModuleHierarchy moduleHierarchy; private final CpdSettings cpdSettings; @@ -59,8 +59,8 @@ public class MetadataPublisher implements ReportPublisherStep { private final ScmConfiguration scmConfiguration; public MetadataPublisher(ProjectAnalysisInfo projectAnalysisInfo, InputModuleHierarchy moduleHierarchy, ScanProperties properties, - ModuleQProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration, - @Nullable ScmConfiguration scmConfiguration) { + QualityProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration, + @Nullable ScmConfiguration scmConfiguration) { this.projectAnalysisInfo = projectAnalysisInfo; this.moduleHierarchy = moduleHierarchy; this.properties = properties; @@ -72,7 +72,7 @@ public class MetadataPublisher implements ReportPublisherStep { } public MetadataPublisher(ProjectAnalysisInfo projectAnalysisInfo, InputModuleHierarchy moduleHierarchy, ScanProperties properties, - ModuleQProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) { + QualityProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) { this(projectAnalysisInfo, moduleHierarchy, properties, qProfiles, cpdSettings, pluginRepository, branchConfiguration, null); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java similarity index 82% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java rename to sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java index 1cd2baa1d03..24286de6ec1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java @@ -27,15 +27,15 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.scanner.analysis.AnalysisProperties; -import org.sonar.scanner.rule.ModuleQProfiles; +import org.sonar.scanner.rule.QualityProfiles; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; -public class QualityProfileProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(QualityProfileProvider.class); +public class QualityProfilesProvider extends ProviderAdapter { + private static final Logger LOG = Loggers.get(QualityProfilesProvider.class); private static final String LOG_MSG = "Load quality profiles"; - private ModuleQProfiles profiles = null; + private QualityProfiles profiles = null; - public ModuleQProfiles provide(ProjectKey projectKey, QualityProfileLoader loader, ProjectRepositories projectRepositories, AnalysisProperties props) { + public QualityProfiles provide(ProjectKey projectKey, QualityProfileLoader loader, ProjectRepositories projectRepositories, AnalysisProperties props) { if (this.profiles == null) { List profileList; Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); @@ -45,7 +45,7 @@ public class QualityProfileProvider extends ProviderAdapter { profileList = loader.load(projectKey.get(), getSonarProfile(props)); } profiler.stopInfo(); - profiles = new ModuleQProfiles(profileList); + profiles = new QualityProfiles(profileList); } return profiles; @@ -53,9 +53,9 @@ public class QualityProfileProvider extends ProviderAdapter { @CheckForNull private static String getSonarProfile(AnalysisProperties props) { - String profile = props.property(ModuleQProfiles.SONAR_PROFILE_PROP); + String profile = props.property(QualityProfiles.SONAR_PROFILE_PROP); if (profile != null) { - LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP + LOG.warn("Ability to set quality profile from command line using '" + QualityProfiles.SONAR_PROFILE_PROP + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); } return profile; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ActiveRulesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ActiveRulesProvider.java index 309b6d4f0fd..d0cc5796176 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ActiveRulesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ActiveRulesProvider.java @@ -43,7 +43,7 @@ public class ActiveRulesProvider extends ProviderAdapter { private static final String LOG_MSG = "Load active rules"; private ActiveRules singleton = null; - public ActiveRules provide(ActiveRulesLoader loader, ModuleQProfiles qProfiles) { + public ActiveRules provide(ActiveRulesLoader loader, QualityProfiles qProfiles) { if (singleton == null) { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); singleton = load(loader, qProfiles); @@ -52,7 +52,7 @@ public class ActiveRulesProvider extends ProviderAdapter { return singleton; } - private static ActiveRules load(ActiveRulesLoader loader, ModuleQProfiles qProfiles) { + private static ActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) { Collection qProfileKeys = getKeys(qProfiles); Set loadedRulesKey = new HashSet<>(); @@ -98,7 +98,7 @@ public class ActiveRulesProvider extends ProviderAdapter { return loader.load(qProfileKey); } - private static Collection getKeys(ModuleQProfiles qProfiles) { + private static Collection getKeys(QualityProfiles qProfiles) { List keys = new ArrayList<>(qProfiles.findAll().size()); for (QProfile qp : qProfiles.findAll()) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QProfileVerifier.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QProfileVerifier.java index 49ce2203b44..21f44b0cfa5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QProfileVerifier.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QProfileVerifier.java @@ -37,9 +37,9 @@ public class QProfileVerifier { private final Configuration settings; private final FileSystem fs; - private final ModuleQProfiles profiles; + private final QualityProfiles profiles; - public QProfileVerifier(Configuration settings, FileSystem fs, ModuleQProfiles profiles) { + public QProfileVerifier(Configuration settings, FileSystem fs, QualityProfiles profiles) { this.settings = settings; this.fs = fs; this.profiles = profiles; @@ -51,7 +51,7 @@ public class QProfileVerifier { @VisibleForTesting void execute(Logger logger) { - String defaultName = settings.get(ModuleQProfiles.SONAR_PROFILE_PROP).orElse(null); + String defaultName = settings.get(QualityProfiles.SONAR_PROFILE_PROP).orElse(null); boolean defaultNameUsed = StringUtils.isBlank(defaultName); for (String lang : fs.languages()) { QProfile profile = profiles.findByLanguage(lang); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ModuleQProfiles.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QualityProfiles.java similarity index 90% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ModuleQProfiles.java rename to sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QualityProfiles.java index 83d1a8c2c41..cdfd499f161 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ModuleQProfiles.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QualityProfiles.java @@ -25,21 +25,19 @@ import java.util.HashMap; import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.ScannerSide; import org.sonar.api.utils.DateUtils; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; /** - * Lists the Quality profiles enabled on the current module. + * Lists the Quality profiles enabled on the current project. */ -@ScannerSide @Immutable -public class ModuleQProfiles { +public class QualityProfiles { public static final String SONAR_PROFILE_PROP = "sonar.profile"; private final Map byLanguage; - public ModuleQProfiles(Collection profiles) { + public QualityProfiles(Collection profiles) { Map map = new HashMap<>(profiles.size()); for (QualityProfile qProfile : profiles) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileProvider.java deleted file mode 100644 index a960a99706b..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileProvider.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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.scanner.rule; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import org.apache.commons.lang.StringUtils; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.config.Configuration; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RulePriority; - -/** - * Ensures backward-compatibility with extensions that use {@link org.sonar.api.profiles.RulesProfile}. - */ -public class RulesProfileProvider extends ProviderAdapter { - - private RulesProfile singleton = null; - - public RulesProfile provide(ModuleQProfiles qProfiles, ActiveRules activeRules, Configuration settings) { - if (singleton == null) { - String lang = settings.get(CoreProperties.PROJECT_LANGUAGE_PROPERTY).orElse(null); - if (StringUtils.isNotBlank(lang)) { - // Backward-compatibility with single-language modules - singleton = loadSingleLanguageProfile(qProfiles, activeRules, lang); - } else { - singleton = loadProfiles(qProfiles, activeRules); - } - } - return singleton; - } - - private static RulesProfile loadSingleLanguageProfile(ModuleQProfiles qProfiles, ActiveRules activeRules, String language) { - QProfile qProfile = qProfiles.findByLanguage(language); - if (qProfile != null) { - return new RulesProfileWrapper(select(qProfile, activeRules)); - } - return new RulesProfileWrapper(new ArrayList<>()); - } - - private static RulesProfile loadProfiles(ModuleQProfiles qProfiles, ActiveRules activeRules) { - Collection profiles = qProfiles.findAll(); - Collection dtos = new ArrayList<>(profiles.size()); - for (QProfile qProfile : profiles) { - dtos.add(select(qProfile, activeRules)); - } - return new RulesProfileWrapper(dtos); - } - - private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) { - RulesProfile deprecatedProfile = new RulesProfile(); - deprecatedProfile.setName(qProfile.getName()); - deprecatedProfile.setLanguage(qProfile.getLanguage()); - for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) { - Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); - rule.setConfigKey(activeRule.internalKey()); - - // SONAR-6706 - if (activeRule.templateRuleKey() != null) { - rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey())); - } - - ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, - RulePriority.valueOf(activeRule.severity())); - for (Map.Entry param : activeRule.params().entrySet()) { - rule.createParameter(param.getKey()); - deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); - } - } - return deprecatedProfile; - } - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileWrapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileWrapper.java deleted file mode 100644 index 7a42b9895fe..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileWrapper.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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.scanner.rule; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.utils.SonarException; - -/** - * This wrapper is used to try to preserve backward compatibility for plugins that used to - * depends on {@link org.sonar.api.profiles.RulesProfile} - * - * @since 4.2 - */ -public class RulesProfileWrapper extends RulesProfile { - - private static final Logger LOG = LoggerFactory.getLogger(RulesProfileWrapper.class); - private static final String DEPRECATED_USAGE_MESSAGE = "Please update your plugin to support multi-language analysis"; - - private final Collection profiles; - private final RulesProfile singleLanguageProfile; - - public RulesProfileWrapper(Collection profiles) { - this.profiles = profiles; - this.singleLanguageProfile = null; - } - - public RulesProfileWrapper(RulesProfile profile) { - this.profiles = Collections.singletonList(profile); - this.singleLanguageProfile = profile; - } - - @Override - public Integer getId() { - return getSingleProfileOrFail().getId(); - } - - private RulesProfile getSingleProfileOrFail() { - if (singleLanguageProfile == null) { - throw new IllegalStateException(DEPRECATED_USAGE_MESSAGE); - } - return singleLanguageProfile; - } - - @Override - public String getName() { - return singleLanguageProfile != null ? singleLanguageProfile.getName() : "SonarQube"; - } - - @Override - public String getLanguage() { - if (singleLanguageProfile == null) { - // Multi-languages module - // This is a hack for CommonChecksDecorator that call this method in its constructor - LOG.debug(DEPRECATED_USAGE_MESSAGE, new SonarException(DEPRECATED_USAGE_MESSAGE)); - return ""; - } - return singleLanguageProfile.getLanguage(); - } - - @Override - public List getActiveRules() { - List activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRules()); - } - return activeRules; - } - - @Override - public ActiveRule getActiveRule(String repositoryKey, String ruleKey) { - for (RulesProfile profile : profiles) { - ActiveRule activeRule = profile.getActiveRule(repositoryKey, ruleKey); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public List getActiveRulesByRepository(String repositoryKey) { - List activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRulesByRepository(repositoryKey)); - } - return activeRules; - } - - @Override - public List getActiveRules(boolean acceptDisabledRules) { - List activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRules(acceptDisabledRules)); - } - return activeRules; - } - - @Override - public ActiveRule getActiveRule(Rule rule) { - for (RulesProfile profile : profiles) { - ActiveRule activeRule = profile.getActiveRule(rule); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public Boolean getDefaultProfile() { - return getSingleProfileOrFail().getDefaultProfile(); - } - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index 033ec860832..a8cf432e5f6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -23,7 +23,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.batch.rule.CheckFactory; import org.sonar.api.issue.NoSonarFilter; import org.sonar.api.scan.filesystem.FileExclusions; import org.sonar.core.extension.CoreExtensionsInstaller; @@ -50,7 +49,6 @@ import org.sonar.scanner.phases.SensorsExecutor; import org.sonar.scanner.postjob.DefaultPostJobContext; import org.sonar.scanner.postjob.PostJobOptimizer; import org.sonar.scanner.rule.QProfileVerifier; -import org.sonar.scanner.rule.RulesProfileProvider; import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem; import org.sonar.scanner.scan.filesystem.ExclusionFilters; import org.sonar.scanner.scan.filesystem.FileIndexer; @@ -130,10 +128,6 @@ public class ModuleScanContainer extends ComponentContainer { ModuleIssueFilters.class, CoverageExclusions.class, - // rules - new RulesProfileProvider(), - CheckFactory.class, - // issues ModuleIssues.class, NoSonarFilter.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index a5c38184cba..e9c8b229b26 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -24,6 +24,7 @@ import javax.annotation.Nullable; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.SensorStrategy; +import org.sonar.api.batch.rule.CheckFactory; import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.scan.filesystem.PathResolver; @@ -76,7 +77,7 @@ import org.sonar.scanner.repository.ProjectRepositories; import org.sonar.scanner.repository.ProjectRepositoriesLoader; import org.sonar.scanner.repository.ProjectRepositoriesProvider; import org.sonar.scanner.repository.QualityProfileLoader; -import org.sonar.scanner.repository.QualityProfileProvider; +import org.sonar.scanner.repository.QualityProfilesProvider; import org.sonar.scanner.repository.ServerIssuesLoader; import org.sonar.scanner.repository.language.DefaultLanguagesRepository; import org.sonar.scanner.rule.ActiveRulesLoader; @@ -167,7 +168,8 @@ public class ProjectScanContainer extends ComponentContainer { // rules new ActiveRulesProvider(), - new QualityProfileProvider(), + new QualityProfilesProvider(), + CheckFactory.class, // issues IssueCache.class, diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 9280aeb7b2e..5831a40d59d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -43,7 +43,7 @@ import org.sonar.scanner.cpd.CpdSettings; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.rule.ModuleQProfiles; +import org.sonar.scanner.rule.QualityProfiles; import org.sonar.scanner.rule.QProfile; import org.sonar.scanner.scan.ScanProperties; import org.sonar.scanner.scan.branch.BranchConfiguration; @@ -66,7 +66,7 @@ public class MetadataPublisherTest { private DefaultInputModule rootModule; private MetadataPublisher underTest; private ScanProperties properties = mock(ScanProperties.class); - private ModuleQProfiles qProfiles = mock(ModuleQProfiles.class); + private QualityProfiles qProfiles = mock(QualityProfiles.class); private ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); private CpdSettings cpdSettings = mock(CpdSettings.class); private InputModuleHierarchy inputModuleHierarchy; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java index 0201b78839c..5c715af7e3a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java @@ -33,7 +33,7 @@ import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.scanner.analysis.AnalysisProperties; -import org.sonar.scanner.rule.ModuleQProfiles; +import org.sonar.scanner.rule.QualityProfiles; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +48,7 @@ public class QualityProfileProviderTest { @Rule public LogTester logTester = new LogTester(); - private QualityProfileProvider qualityProfileProvider; + private QualityProfilesProvider qualityProfileProvider; @Mock private QualityProfileLoader loader; @@ -64,7 +64,7 @@ public class QualityProfileProviderTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - qualityProfileProvider = new QualityProfileProvider(); + qualityProfileProvider = new QualityProfilesProvider(); when(key.get()).thenReturn("project"); when(projectRepo.exists()).thenReturn(true); @@ -76,7 +76,7 @@ public class QualityProfileProviderTest { @Test public void testProvide() { when(loader.load("project", null)).thenReturn(response); - ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); + QualityProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); verify(loader).load("project", null); @@ -87,8 +87,8 @@ public class QualityProfileProviderTest { public void testProjectDoesntExist() { when(projectRepo.exists()).thenReturn(false); when(loader.loadDefault(anyString())).thenReturn(response); - when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("profile"); - ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); + when(props.property(QualityProfiles.SONAR_PROFILE_PROP)).thenReturn("profile"); + QualityProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); verify(loader).loadDefault(anyString()); @@ -98,15 +98,15 @@ public class QualityProfileProviderTest { @Test public void testProfileProp() { when(loader.load(eq("project"), eq("custom"))).thenReturn(response); - when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); - when(props.properties()).thenReturn(ImmutableMap.of(ModuleQProfiles.SONAR_PROFILE_PROP, "custom")); + when(props.property(QualityProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); + when(props.properties()).thenReturn(ImmutableMap.of(QualityProfiles.SONAR_PROFILE_PROP, "custom")); - ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); + QualityProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); verify(loader).load(eq("project"), eq("custom")); verifyNoMoreInteractions(loader); - assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP + assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + QualityProfiles.SONAR_PROFILE_PROP + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); } @@ -114,19 +114,19 @@ public class QualityProfileProviderTest { public void testProfilePropDefault() { when(projectRepo.exists()).thenReturn(false); when(loader.loadDefault(eq("custom"))).thenReturn(response); - when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); - when(props.properties()).thenReturn(ImmutableMap.of(ModuleQProfiles.SONAR_PROFILE_PROP, "custom")); + when(props.property(QualityProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); + when(props.properties()).thenReturn(ImmutableMap.of(QualityProfiles.SONAR_PROFILE_PROP, "custom")); - ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); + QualityProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); verify(loader).loadDefault(eq("custom")); verifyNoMoreInteractions(loader); - assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP + assertThat(logTester.logs(LoggerLevel.WARN)).contains("Ability to set quality profile from command line using '" + QualityProfiles.SONAR_PROFILE_PROP + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); } - private void assertResponse(ModuleQProfiles qps) { + private void assertResponse(QualityProfiles qps) { assertThat(qps.findAll()).extracting("key").containsExactly("profile"); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesProviderTest.java index 6380d4bb803..2773208a00f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesProviderTest.java @@ -66,7 +66,7 @@ public class ActiveRulesProviderTest { when(loader.load(eq("qp2"))).thenReturn(qp2Rules); when(loader.load(eq("qp3"))).thenReturn(qp3Rules); - ModuleQProfiles profiles = mockProfiles("qp1", "qp2", "qp3"); + QualityProfiles profiles = mockProfiles("qp1", "qp2", "qp3"); ActiveRules activeRules = provider.provide(loader, profiles); assertThat(activeRules.findAll()).hasSize(3); @@ -88,20 +88,19 @@ public class ActiveRulesProviderTest { List qpRules = ImmutableList.of(r1, r2); when(loader.load(eq("qp"))).thenReturn(qpRules); - ModuleQProfiles profiles = mockProfiles("qp"); + QualityProfiles profiles = mockProfiles("qp"); ActiveRules activeRules = provider.provide(loader, profiles); assertThat(activeRules.findAll()).hasSize(2); assertThat(activeRules.findAll()).extracting("ruleKey", "params").containsOnly( Tuple.tuple(RuleKey.of("rule1", "rule1"), ImmutableMap.of()), - Tuple.tuple(RuleKey.of("rule2", "rule2"), ImmutableMap.of("foo1", "bar1", "foo2", "bar2")) - ); + Tuple.tuple(RuleKey.of("rule2", "rule2"), ImmutableMap.of("foo1", "bar1", "foo2", "bar2"))); verify(loader).load(eq("qp")); verifyNoMoreInteractions(loader); } - private static ModuleQProfiles mockProfiles(String... keys) { + private static QualityProfiles mockProfiles(String... keys) { List profiles = new LinkedList<>(); for (String k : keys) { @@ -109,7 +108,7 @@ public class ActiveRulesProviderTest { profiles.add(p); } - return new ModuleQProfiles(profiles); + return new QualityProfiles(profiles); } private static LoadedActiveRule mockRule(String name) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java index 93ee555491d..fb432157a58 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java @@ -43,13 +43,13 @@ public class QProfileVerifierTest { public ExpectedException thrown = ExpectedException.none(); private DefaultFileSystem fs; - private ModuleQProfiles profiles; + private QualityProfiles profiles; private MapSettings settings = new MapSettings(); @Before public void before() throws Exception { fs = new DefaultFileSystem(temp.newFolder().toPath()); - profiles = mock(ModuleQProfiles.class); + profiles = mock(QualityProfiles.class); QProfile javaProfile = new QProfile("p1", "My Java profile", "java", null); when(profiles.findByLanguage("java")).thenReturn(javaProfile); QProfile cobolProfile = new QProfile("p2", "My Cobol profile", "cobol", null); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesProfileProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesProfileProviderTest.java deleted file mode 100644 index ca1c2d0390f..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesProfileProviderTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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.scanner.rule; - -import java.util.Arrays; -import org.junit.Test; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; -import org.sonar.api.batch.rule.internal.NewActiveRule; -import org.sonar.api.config.internal.MapSettings; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rule.RuleKey; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class RulesProfileProviderTest { - - private ModuleQProfiles qProfiles = mock(ModuleQProfiles.class); - private MapSettings settings = new MapSettings(); - private RulesProfileProvider provider = new RulesProfileProvider(); - - @Test - public void merge_profiles() { - QProfile qProfile = new QProfile("java-sw", "Sonar way", "java", null); - when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile)); - - RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings.asConfig()); - - // merge of all profiles - assertThat(profile).isNotNull().isInstanceOf(RulesProfileWrapper.class); - assertThat(profile.getLanguage()).isEqualTo(""); - assertThat(profile.getName()).isEqualTo("SonarQube"); - assertThat(profile.getActiveRules()).isEmpty(); - try { - profile.getId(); - fail(); - } catch (IllegalStateException e) { - // id must not be used at all - } - } - - @Test - public void keep_compatibility_with_single_language_projects() { - settings.setProperty("sonar.language", "java"); - - QProfile qProfile = new QProfile("java-sw", "Sonar way", "java", null); - when(qProfiles.findByLanguage("java")).thenReturn(qProfile); - - RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings.asConfig()); - - // no merge, directly the old hibernate profile - assertThat(profile).isNotNull(); - assertThat(profile.getLanguage()).isEqualTo("java"); - assertThat(profile.getName()).isEqualTo("Sonar way"); - } - - @Test - public void support_rule_templates() { - QProfile qProfile = new QProfile("java-sw", "Sonar way", "java", null); - when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile)); - ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); - activeRulesBuilder.addRule(new NewActiveRule.Builder() - .setRuleKey(RuleKey.of("java", "S001")) - .setTemplateRuleKey("T001").setLanguage("java") - .build()); - - RulesProfile profile = provider.provide(qProfiles, activeRulesBuilder.build(), settings.asConfig()); - - assertThat(profile.getActiveRule("java", "S001").getRule().getTemplate().getKey()).isEqualTo("T001"); - } -} -- 2.39.5