diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-18 12:18:39 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-18 15:43:39 +0200 |
commit | c3174fada2ab193d03661209bee3291772b8f347 (patch) | |
tree | 2c421a541d108f24bbab0c861c6ab9aaedfa7bdc /sonar-batch | |
parent | 274e9cefeb16fab1d5e35088289338f12993b208 (diff) | |
download | sonarqube-c3174fada2ab193d03661209bee3291772b8f347.tar.gz sonarqube-c3174fada2ab193d03661209bee3291772b8f347.zip |
SONAR-5417 Get active rules using batch protocol
Diffstat (limited to 'sonar-batch')
23 files changed, 240 insertions, 576 deletions
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 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<String, String> bootstrapProperties = new HashMap<String, String>(); - 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<String, String> 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<Integer, Rule> rulesById = HashBiMap.create(); - private Map<String, Map<String, Rule>> rulesByRepoKeyAndRuleKey = Maps.newHashMap(); - - @Override - public Rule findById(int ruleId) { - return rulesById.get(ruleId); - } - - @Override - public Rule findByKey(String repositoryKey, String ruleKey) { - Map<String, Rule> 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<Rule> findAll(RuleQuery query) { - throw new UnsupportedOperationException(); - } - } - - private static class FakeQProfileReferential implements QProfilesReferential { - - private Map<String, Map<String, QProfile>> profiles = new HashMap<String, Map<String, QProfile>>(); - - @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<String, QProfile>()); - } - profiles.get(qprofile.getLanguage()).put(qprofile.getName(), qprofile); - } - - } - - private static class FakeLanguageReferential implements LanguagesReferential { - - private Map<String, Language> languages = new HashMap<String, Language>(); - - 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<Language> 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<Metric> 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<Integer, ActiveRuleParamDto> 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<Metric> 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/src/main/java/org/sonar/batch/rules/QProfilesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java index 5d50cf33242..75a063eb119 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java @@ -17,24 +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.rules; +package org.sonar.batch.referential; -import org.sonar.batch.rule.QProfile; +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; -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); +public class ProjectReferentialsProvider extends ProviderAdapter { + public ProjectReferentials provide(ProjectReferentialsLoader loader, ProjectReactor reactor, Settings settings, Languages languages) { + return loader.load(reactor, settings, languages); + } } 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<Integer, ActiveRuleParamDto> 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<String, String> 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<String, QProfile> byLanguage; - public ModuleQProfiles(Settings settings, LanguagesReferential languages, QProfilesReferential qProfileRef) { + public ModuleQProfiles(ProjectReferentials ref) { ImmutableMap.Builder<String, QProfile> 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<QProfile> 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/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java deleted file mode 100644 index 1522091f4eb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java +++ /dev/null @@ -1,21 +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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.rules; 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<QProfile> 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<QProfile> 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.<String, String>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"); |