Browse Source

SONAR-11471 Stop providing RulesProfile on scanner side

tags/7.6
Julien HENRY 5 years ago
parent
commit
8bbac3c6e0

+ 1
- 1
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/CheckFactory.java View File

@@ -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

+ 5
- 5
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java View File

@@ -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);
}


sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java → sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java View File

@@ -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<QualityProfile> 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;

+ 3
- 3
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ActiveRulesProvider.java View File

@@ -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<String> qProfileKeys = getKeys(qProfiles);
Set<RuleKey> loadedRulesKey = new HashSet<>();
@@ -98,7 +98,7 @@ public class ActiveRulesProvider extends ProviderAdapter {
return loader.load(qProfileKey);
}

private static Collection<String> getKeys(ModuleQProfiles qProfiles) {
private static Collection<String> getKeys(QualityProfiles qProfiles) {
List<String> keys = new ArrayList<>(qProfiles.findAll().size());

for (QProfile qp : qProfiles.findAll()) {

+ 3
- 3
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QProfileVerifier.java View File

@@ -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);

sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/ModuleQProfiles.java → sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/QualityProfiles.java View File

@@ -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<String, QProfile> byLanguage;

public ModuleQProfiles(Collection<QualityProfile> profiles) {
public QualityProfiles(Collection<QualityProfile> profiles) {
Map<String, QProfile> map = new HashMap<>(profiles.size());

for (QualityProfile qProfile : profiles) {

+ 0
- 95
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileProvider.java View File

@@ -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<QProfile> profiles = qProfiles.findAll();
Collection<RulesProfile> 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<String, String> param : activeRule.params().entrySet()) {
rule.createParameter(param.getKey());
deprecatedActiveRule.setParameter(param.getKey(), param.getValue());
}
}
return deprecatedProfile;
}

}

+ 0
- 139
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RulesProfileWrapper.java View File

@@ -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<RulesProfile> profiles;
private final RulesProfile singleLanguageProfile;

public RulesProfileWrapper(Collection<RulesProfile> 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<ActiveRule> getActiveRules() {
List<ActiveRule> 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<ActiveRule> getActiveRulesByRepository(String repositoryKey) {
List<ActiveRule> activeRules = new ArrayList<>();
for (RulesProfile profile : profiles) {
activeRules.addAll(profile.getActiveRulesByRepository(repositoryKey));
}
return activeRules;
}

@Override
public List<ActiveRule> getActiveRules(boolean acceptDisabledRules) {
List<ActiveRule> 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();
}

}

+ 0
- 6
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java View File

@@ -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,

+ 4
- 2
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java View File

@@ -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,

+ 2
- 2
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java View File

@@ -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;

+ 15
- 15
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java View File

@@ -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");

}

+ 5
- 6
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesProviderTest.java View File

@@ -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<LoadedActiveRule> 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<QualityProfile> 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) {

+ 2
- 2
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java View File

@@ -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);

+ 0
- 90
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesProfileProviderTest.java View File

@@ -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");
}
}

Loading…
Cancel
Save