From 2c5cf61c68d8c4b7c7b6a41f083d09c8e6354eb6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 31 Jan 2014 15:55:43 +0100 Subject: [PATCH] SONAR-926 use the property sonar.language to detect single-lang projects --- .../org/sonar/batch/rule/ModuleQProfiles.java | 7 ++ .../batch/rule/RulesProfileProvider.java | 10 ++- .../sonar/batch/rule/RulesProfileWrapper.java | 5 +- .../batch/rule/RulesProfileProviderTest.java | 80 +++++++++++++++++++ 4 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java 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 32641f37314..e83e86acd48 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 @@ -55,6 +55,13 @@ public class ModuleQProfiles implements BatchComponent { this.version = dto.getVersion(); } + QProfile(int id, String name, String language, Integer version) { + this.id = id; + this.name = name; + this.language = language; + this.version = version; + } + public int id() { return id; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java index d8c0c00cd59..3af1866b814 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java @@ -20,8 +20,11 @@ package org.sonar.batch.rule; import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.ModuleLanguages; +import org.sonar.api.config.Settings; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rules.ActiveRule; import org.sonar.jpa.dao.ProfilesDao; @@ -35,11 +38,12 @@ public class RulesProfileProvider extends ProviderAdapter { private RulesProfile singleton = null; - public RulesProfile provide(ModuleQProfiles qProfiles, ModuleLanguages moduleLanguages, ProfilesDao dao) { + public RulesProfile provide(ModuleQProfiles qProfiles, Settings settings, ProfilesDao dao) { if (singleton == null) { - if (moduleLanguages.keys().size() == 1) { + String lang = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY); + if (StringUtils.isNotBlank(lang)) { // Backward-compatibility with single-language modules - singleton = loadSingleLanguageProfile(qProfiles, moduleLanguages.keys().iterator().next(), dao); + singleton = loadSingleLanguageProfile(qProfiles, lang, dao); } else { singleton = loadProfiles(qProfiles, dao); } 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 7011138d931..6af51ec5473 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 @@ -61,8 +61,8 @@ public class RulesProfileWrapper extends RulesProfile { } private RulesProfile getSingleProfileOrFail() { - if (singleLanguageProfile != null) { - throw new SonarException("Please update your plugin to support multi-language analysis"); + if (singleLanguageProfile == null) { + throw new IllegalStateException("Please update your plugin to support multi-language analysis"); } return singleLanguageProfile; } @@ -83,6 +83,7 @@ public class RulesProfileWrapper extends RulesProfile { return singleLanguageProfile.getLanguage(); } + // TODO remove when ProfileEventsSensor is refactored public RulesProfile getProfileByLanguage(String languageKey) { for (RulesProfile profile : profiles) { if (languageKey.equals(profile.getLanguage())) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java new file mode 100644 index 00000000000..1b43540afb3 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java @@ -0,0 +1,80 @@ +/* + * 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.rule; + +import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.jpa.dao.ProfilesDao; + +import java.util.Arrays; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RulesProfileProviderTest { + ModuleQProfiles qProfiles = mock(ModuleQProfiles.class); + Settings settings = new Settings(); + ProfilesDao dao = mock(ProfilesDao.class); + RulesProfileProvider provider = new RulesProfileProvider(); + + @Test + public void merge_profiles() throws Exception { + ModuleQProfiles.QProfile qProfile = new ModuleQProfiles.QProfile(33, "Sonar way", "java", 12); + when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile)); + RulesProfile hibernateProfile = new RulesProfile("Sonar way", "java"); + when(dao.getProfile("java", "Sonar way")).thenReturn(hibernateProfile); + + RulesProfile profile = provider.provide(qProfiles, settings, dao); + + // merge of all profiles + assertThat(profile).isNotNull().isInstanceOf(RulesProfileWrapper.class); + assertThat(profile.getLanguage()).isEqualTo(""); + assertThat(profile.getName()).isEqualTo("SonarQube"); + assertThat(profile.getAlerts()).isEmpty(); + 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() throws Exception { + settings.setProperty("sonar.language", "java"); + + ModuleQProfiles.QProfile qProfile = new ModuleQProfiles.QProfile(33, "Sonar way", "java", 12); + when(qProfiles.findByLanguage("java")).thenReturn(qProfile); + RulesProfile hibernateProfile = new RulesProfile("Sonar way", "java").setVersion(12); + when(dao.getProfile("java", "Sonar way")).thenReturn(hibernateProfile); + + RulesProfile profile = provider.provide(qProfiles, settings, dao); + + // no merge, directly the old hibernate profile + assertThat(profile).isNotNull(); + assertThat(profile.getLanguage()).isEqualTo("java"); + assertThat(profile.getName()).isEqualTo("Sonar way"); + assertThat(profile.getVersion()).isEqualTo(12); + } +} -- 2.39.5