From 58f7ccdb23474f33c7d61c60f6e03cdb3ad67a5b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 27 Jan 2014 09:51:31 +0100 Subject: [PATCH] SONAR-926 fix loading of ModuleRules --- .../core/sensors/ProfileEventsSensor.java | 2 +- .../core/sensors/ProfileEventsSensorTest.java | 46 +++--- .../org/sonar/batch/RulesProfileWrapper.java | 151 ------------------ .../org/sonar/batch/rule/ModuleQProfiles.java | 3 + .../sonar/batch/rule/ModuleRulesProvider.java | 2 +- .../sonar/batch/rule/RulesProfileWrapper.java | 15 +- .../sonar/batch/scan/ModuleScanContainer.java | 2 + 7 files changed, 39 insertions(+), 182 deletions(-) delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/RulesProfileWrapper.java diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java index 5771b1a9327..642b7e3ae0e 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProfileEventsSensor.java @@ -30,7 +30,7 @@ import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; -import org.sonar.batch.RulesProfileWrapper; +import org.sonar.batch.rule.RulesProfileWrapper; import java.util.List; diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java index 47bf93bcd7e..7ebb236e586 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProfileEventsSensorTest.java @@ -32,31 +32,26 @@ import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; import org.sonar.api.resources.Project; -import org.sonar.batch.RulesProfileWrapper; +import org.sonar.batch.rule.RulesProfileWrapper; import org.sonar.batch.scan.language.DefaultModuleLanguages; import java.util.Arrays; import java.util.Collections; import java.util.Date; -import java.util.HashMap; -import java.util.Map; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.same; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class ProfileEventsSensorTest { - private Project project; - private SensorContext context; - private DefaultModuleLanguages moduleLanguages; - private RulesProfileWrapper wrapper; - private RulesProfile profile; + Project project; + SensorContext context; + DefaultModuleLanguages moduleLanguages; + RulesProfileWrapper wrapper; + RulesProfile profile; @Before public void prepare() { @@ -65,10 +60,9 @@ public class ProfileEventsSensorTest { moduleLanguages = new DefaultModuleLanguages(new Settings(), new Languages(Java.INSTANCE)); moduleLanguages.addLanguage("java"); - Map ruleProfilesPerLanguages = new HashMap(); profile = mock(RulesProfile.class); - ruleProfilesPerLanguages.put("java", profile); - wrapper = new RulesProfileWrapper(moduleLanguages, ruleProfilesPerLanguages); + when(profile.getLanguage()).thenReturn("java"); + wrapper = new RulesProfileWrapper(profile); } @Test @@ -110,9 +104,9 @@ public class ProfileEventsSensorTest { sensor.analyse(project, context); verify(context).createEvent(same(project), - eq("Foo version 1"), - eq("Foo version 1 is used instead of Bar version 1"), - same(Event.CATEGORY_PROFILE), any(Date.class)); + eq("Foo version 1"), + eq("Foo version 1 is used instead of Bar version 1"), + same(Event.CATEGORY_PROFILE), any(Date.class)); } @Test @@ -124,9 +118,9 @@ public class ProfileEventsSensorTest { sensor.analyse(project, context); verify(context).createEvent(same(project), - eq("Foo version 2"), - eq("Foo version 2 is used instead of Foo version 1"), - same(Event.CATEGORY_PROFILE), any(Date.class)); + eq("Foo version 2"), + eq("Foo version 2 is used instead of Foo version 1"), + same(Event.CATEGORY_PROFILE), any(Date.class)); } @Test @@ -149,9 +143,9 @@ public class ProfileEventsSensorTest { sensor.analyse(project, context); verify(context).createEvent(same(project), - eq("Foo version 2"), - eq("Foo version 2 is used instead of Foo version 1"), - same(Event.CATEGORY_PROFILE), any(Date.class)); + eq("Foo version 2"), + eq("Foo version 2 is used instead of Foo version 1"), + same(Event.CATEGORY_PROFILE), any(Date.class)); } private void mockProfileWithVersion(int version) { @@ -168,8 +162,8 @@ public class ProfileEventsSensorTest { TimeMachine timeMachine = mock(TimeMachine.class); when(timeMachine.getMeasures(any(TimeMachineQuery.class))) - .thenReturn(result1 == null ? Collections.emptyList() : Arrays.asList(result1)) - .thenReturn(result2 == null ? Collections.emptyList() : Arrays.asList(result2)); + .thenReturn(result1 == null ? Collections.emptyList() : Arrays.asList(result1)) + .thenReturn(result2 == null ? Collections.emptyList() : Arrays.asList(result2)); return timeMachine; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/RulesProfileWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/RulesProfileWrapper.java deleted file mode 100644 index 416d2af87f5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/RulesProfileWrapper.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.ModuleLanguages; -import org.sonar.api.profiles.Alert; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.utils.SonarException; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * This wrapper is used to try to preserve backward compatibility for plugins that used to - * depends on {@link RulesProfile} - * @since 4.2 - */ -public class RulesProfileWrapper extends RulesProfile { - - private static final Logger LOG = LoggerFactory.getLogger(RulesProfileWrapper.class); - - private final Map ruleProfilesPerLanguages; - private ModuleLanguages moduleLanguages; - - public RulesProfileWrapper(ModuleLanguages moduleLanguages, Map ruleProfilesPerLanguages) { - this.moduleLanguages = moduleLanguages; - this.ruleProfilesPerLanguages = ruleProfilesPerLanguages; - } - - @Override - public Integer getId() { - return getSingleProfileOrFail().getId(); - } - - private RulesProfile getSingleProfileOrFail() { - if (moduleLanguages.keys().size() != 1) { - throw new SonarException("Please update your plugin to support multi-language analysis"); - } - return ruleProfilesPerLanguages.get(moduleLanguages.keys().iterator().next()); - } - - public RulesProfile getProfileByLanguage(String languageKey) { - return ruleProfilesPerLanguages.get(languageKey); - } - - @Override - public String getName() { - return getSingleProfileOrFail().getName(); - } - - @Override - public String getLanguage() { - if (moduleLanguages.keys().size() != 1) { - // FIXME This is a hack for CommonChecksDecorator that call this method in its constructor - LOG.debug("Please update your plugin to support multi-language analysis", new SonarException("Please update your plugin to support multi-language analysis")); - return ""; - } - return ruleProfilesPerLanguages.get(moduleLanguages.keys().iterator().next()).getLanguage(); - } - - @Override - public List getAlerts() { - List result = new ArrayList(); - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - result.addAll(profile.getAlerts()); - } - return result; - } - - @Override - public List getActiveRules() { - List activeRules = new ArrayList(); - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - activeRules.addAll(profile.getActiveRules()); - } - return activeRules; - } - - @Override - public int getVersion() { - return getSingleProfileOrFail().getVersion(); - } - - @Override - public ActiveRule getActiveRule(String repositoryKey, String ruleKey) { - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - ActiveRule activeRule = profile.getActiveRule(repositoryKey, ruleKey); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public List getActiveRulesByRepository(String repositoryKey) { - List activeRules = new ArrayList(); - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - activeRules.addAll(profile.getActiveRulesByRepository(repositoryKey)); - } - return activeRules; - } - - @Override - public List getActiveRules(boolean acceptDisabledRules) { - List activeRules = new ArrayList(); - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - activeRules.addAll(profile.getActiveRules(acceptDisabledRules)); - } - return activeRules; - } - - @Override - public ActiveRule getActiveRule(Rule rule) { - for (RulesProfile profile : ruleProfilesPerLanguages.values()) { - ActiveRule activeRule = profile.getActiveRule(rule); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public Boolean getDefaultProfile() { - return getSingleProfileOrFail().getDefaultProfile(); - } - -} 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 47da55b29fd..7330ecf50ff 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 @@ -74,6 +74,9 @@ public class ModuleQProfiles implements BatchComponent { private final Map byLanguage; + public ModuleQProfiles(Settings settings, Languages languages, QualityProfileDao dao) { + this(settings, languages, dao, new ProfileLoader[0]); + } public ModuleQProfiles(Settings settings, Languages languages, QualityProfileDao dao, ProfileLoader[] loaders) { ImmutableMap.Builder builder = ImmutableMap.builder(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleRulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleRulesProvider.java index a4bcdc3f00b..ef4ec47ed0d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleRulesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleRulesProvider.java @@ -33,7 +33,7 @@ import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; /** * Loads the rules that are activated on the Quality profiles - * used by the current module. + * used by the current module and build {@link org.sonar.api.batch.rule.ModuleRules}. */ public class ModuleRulesProvider extends ProviderAdapter { diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java index 8bd0c36e98b..ffa23f300a0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java @@ -45,12 +45,12 @@ public class RulesProfileWrapper extends RulesProfile { private final Collection profiles; private final RulesProfile singleLanguageProfile; - RulesProfileWrapper(Collection profiles) { + public RulesProfileWrapper(Collection profiles) { this.profiles = profiles; this.singleLanguageProfile = null; } - RulesProfileWrapper(RulesProfile profile) { + public RulesProfileWrapper(RulesProfile profile) { this.profiles = Lists.newArrayList(profile); this.singleLanguageProfile = profile; } @@ -69,7 +69,7 @@ public class RulesProfileWrapper extends RulesProfile { @Override public String getName() { - return getSingleProfileOrFail().getName(); + return singleLanguageProfile!= null ? singleLanguageProfile.getName() : "SonarQube"; } @Override @@ -83,6 +83,15 @@ public class RulesProfileWrapper extends RulesProfile { return singleLanguageProfile.getName(); } + public RulesProfile getProfileByLanguage(String languageKey) { + for (RulesProfile profile : profiles) { + if (languageKey.equals(profile.getLanguage())) { + return profile; + } + } + return null; + } + @Override public List getAlerts() { List result = new ArrayList(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index b34065bff14..56659948df0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -42,6 +42,7 @@ import org.sonar.batch.issue.IssueFilters; import org.sonar.batch.issue.ModuleIssues; import org.sonar.batch.phases.PhaseExecutor; import org.sonar.batch.phases.PhasesTimeProfiler; +import org.sonar.batch.rule.ModuleQProfiles; import org.sonar.batch.rule.ModuleRulesProvider; import org.sonar.batch.rule.QProfileSensor; import org.sonar.batch.rule.RulesProfileProvider; @@ -119,6 +120,7 @@ public class ModuleScanContainer extends ComponentContainer { ResourceFilters.class, // rules + ModuleQProfiles.class, new ModuleRulesProvider(), new RulesProfileProvider(), QProfileSensor.class, -- 2.39.5