diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-18 17:08:30 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-19 20:12:12 +0200 |
commit | 9bfb3bb8b4775294831f38f7ae7aea35416d680b (patch) | |
tree | 13b20ce874b87d5c1dc7a2f715244ee0ea603bc0 /sonar-batch/src | |
parent | 6fbd7d0754de53986bc1d37ae7fd29c8e32872a1 (diff) | |
download | sonarqube-9bfb3bb8b4775294831f38f7ae7aea35416d680b.tar.gz sonarqube-9bfb3bb8b4775294831f38f7ae7aea35416d680b.zip |
SONAR-5007 add single unique key to quality profiles. Replace DB ID.
Diffstat (limited to 'sonar-batch/src')
23 files changed, 387 insertions, 572 deletions
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 ca90e38d01c..d6c6eb020c3 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 @@ -29,7 +29,6 @@ import org.sonar.api.batch.rules.QProfile; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleParam; -import org.sonar.batch.rules.QProfileWithId; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; @@ -52,13 +51,12 @@ public class ActiveRulesProvider extends ProviderAdapter { private ActiveRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) { ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (QProfile qProfile : qProfiles.findAll()) { - QProfileWithId qProfileWithId = (QProfileWithId) qProfile; ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create(); - for (ActiveRuleParamDto dto : dao.selectParamsByProfileId(qProfileWithId.id())) { + for (ActiveRuleParamDto dto : dao.selectParamsByProfileKey(qProfile.key())) { paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto); } - for (ActiveRuleDto activeDto : dao.selectByProfileId(qProfileWithId.id())) { + for (ActiveRuleDto activeDto : dao.selectByProfileKey(qProfile.key())) { Rule rule = ruleFinder.findById(activeDto.getRulId()); if (rule != null) { NewActiveRule newActiveRule = builder.create(rule.ruleKey()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileDecorator.java index 1bde76fbd45..fce17de2473 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileDecorator.java @@ -48,13 +48,13 @@ public class QProfileDecorator implements Decorator { if (!ResourceUtils.isProject(resource)) { return; } - UsedQProfiles profiles = UsedQProfiles.empty(); + UsedQProfiles used = new UsedQProfiles(); for (Measure childProfilesMeasure : context.getChildrenMeasures(CoreMetrics.QUALITY_PROFILES)) { - UsedQProfiles childProfiles = UsedQProfiles.fromJSON(childProfilesMeasure.getData()); - profiles = profiles.merge(childProfiles); + UsedQProfiles childProfiles = UsedQProfiles.fromJson(childProfilesMeasure.getData()); + used.add(childProfiles); } - Measure detailsMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, profiles.toJSON()); + Measure detailsMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, used.toJson()); context.saveMeasure(detailsMeasure); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java index e84dc342c17..9f05d4e5fa0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java @@ -26,6 +26,7 @@ import org.sonar.api.batch.DependsUpon; import org.sonar.api.batch.Event; import org.sonar.api.batch.TimeMachine; import org.sonar.api.batch.TimeMachineQuery; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; @@ -34,9 +35,8 @@ import org.sonar.api.resources.Languages; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; -import org.sonar.batch.rules.QProfileWithId; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; + +import javax.annotation.CheckForNull; import java.util.List; import java.util.Map; @@ -44,12 +44,10 @@ import java.util.Map; public class QProfileEventsDecorator implements Decorator { private final TimeMachine timeMachine; - private final QualityProfileDao qualityProfileDao; private final Languages languages; - public QProfileEventsDecorator(TimeMachine timeMachine, QualityProfileDao qualityProfileDao, Languages languages) { + public QProfileEventsDecorator(TimeMachine timeMachine, Languages languages) { this.timeMachine = timeMachine; - this.qualityProfileDao = qualityProfileDao; this.languages = languages; } @@ -69,76 +67,51 @@ public class QProfileEventsDecorator implements Decorator { } // Load current profiles - Measure profilesMeasure = context.getMeasure(CoreMetrics.QUALITY_PROFILES); - UsedQProfiles currentProfiles = UsedQProfiles.fromJSON(profilesMeasure.getData()); - - // Now load previous profiles - UsedQProfiles pastProfiles; - // First try with new metric - Measure pastProfilesMeasure = getPreviousMeasure(resource, CoreMetrics.QUALITY_PROFILES); - if (pastProfilesMeasure != null) { - pastProfiles = UsedQProfiles.fromJSON(pastProfilesMeasure.getData()); - } else { - // Fallback to old metric - Measure pastProfileMeasure = getPreviousMeasure(resource, CoreMetrics.PROFILE); - if (pastProfileMeasure == null) { - // first analysis - return; - } - int pastProfileId = pastProfileMeasure.getIntValue(); - String pastProfileName = pastProfileMeasure.getData(); - QualityProfileDto pastProfile = qualityProfileDao.selectById(pastProfileId); - String pastProfileLanguage = "unknow"; - if (pastProfile != null) { - pastProfileLanguage = pastProfile.getLanguage(); - } - Measure pastProfileVersionMeasure = getPreviousMeasure(resource, CoreMetrics.PROFILE_VERSION); - final int pastProfileVersion; - // first analysis with versions - if (pastProfileVersionMeasure == null) { - pastProfileVersion = 1; - } else { - pastProfileVersion = pastProfileVersionMeasure.getIntValue(); - } - pastProfiles = UsedQProfiles.fromProfiles(new QProfileWithId(pastProfileId, pastProfileName, pastProfileLanguage, pastProfileVersion)); + Measure currentMeasure = context.getMeasure(CoreMetrics.QUALITY_PROFILES); + Map<String, QProfile> currentProfiles = UsedQProfiles.fromJson(currentMeasure.getData()).profilesByKey(); + + // Load previous profiles + Map<String, QProfile> previousProfiles = Maps.newHashMap(); + Measure previousMeasure = getPreviousMeasure(resource, CoreMetrics.QUALITY_PROFILES); + if (previousMeasure != null && previousMeasure.getData() != null) { + previousProfiles = UsedQProfiles.fromJson(previousMeasure.getData()).profilesByKey(); } - // Now create appropriate events - Map<Integer, QProfileWithId> pastProfilesById = Maps.newHashMap(pastProfiles.profilesById()); - for (QProfileWithId profile : currentProfiles.profilesById().values()) { - if (pastProfilesById.containsKey(profile.id())) { - QProfileWithId pastProfile = pastProfilesById.get(profile.id()); - if (pastProfile.version() < profile.version()) { - // New version of the same QP - usedProfile(context, profile); - } - pastProfilesById.remove(profile.id()); + // Detect new profiles or updated profiles + for (QProfile profile : currentProfiles.values()) { + QProfile previousProfile = previousProfiles.get(profile.key()); + if (previousProfile != null) { + // TODO compare date } else { usedProfile(context, profile); } } - for (QProfileWithId profile : pastProfilesById.values()) { - // Following profiles are no more used - stopUsedProfile(context, profile); + + // Detect profiles that are not used anymore + for (QProfile previousProfile : previousProfiles.values()) { + if (!currentProfiles.containsKey(previousProfile.key())) { + stopUsedProfile(context, previousProfile); + } } } - private void stopUsedProfile(DecoratorContext context, QProfileWithId profile) { + private void stopUsedProfile(DecoratorContext context, QProfile profile) { Language language = languages.get(profile.language()); String languageName = language != null ? language.getName() : profile.language(); context.createEvent("Stop using " + format(profile) + " (" + languageName + ")", format(profile) + " no more used for " + languageName, Event.CATEGORY_PROFILE, null); } - private void usedProfile(DecoratorContext context, QProfileWithId profile) { + private void usedProfile(DecoratorContext context, QProfile profile) { Language language = languages.get(profile.language()); String languageName = language != null ? language.getName() : profile.language(); context.createEvent("Use " + format(profile) + " (" + languageName + ")", format(profile) + " used for " + languageName, Event.CATEGORY_PROFILE, null); } - private String format(QProfileWithId profile) { - return profile.name() + " version " + profile.version(); + private String format(QProfile profile) { + return profile.name(); } + @CheckForNull private Measure getPreviousMeasure(Resource project, Metric metric) { TimeMachineQuery query = new TimeMachineQuery(project) .setOnlyLastAnalysis(true) diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java index 620f769d8b7..2d591a26794 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java @@ -19,17 +19,13 @@ */ package org.sonar.batch.rule; -import com.google.common.collect.Lists; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.resources.Project; -import org.sonar.batch.rules.QProfileWithId; -import org.sonar.core.qualityprofile.db.QualityProfileDao; - -import java.util.List; /** * Stores which Quality profiles have been used on the current module. @@ -38,12 +34,10 @@ public class QProfileSensor implements Sensor { private final ModuleQProfiles moduleQProfiles; private final FileSystem fs; - private final QualityProfileDao dao; - public QProfileSensor(ModuleQProfiles moduleQProfiles, FileSystem fs, QualityProfileDao dao) { + public QProfileSensor(ModuleQProfiles moduleQProfiles, FileSystem fs) { this.moduleQProfiles = moduleQProfiles; this.fs = fs; - this.dao = dao; } public boolean shouldExecuteOnProject(Project project) { @@ -52,26 +46,15 @@ public class QProfileSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - List<QProfileWithId> profiles = Lists.newArrayList(); + UsedQProfiles used = new UsedQProfiles(); for (String language : fs.languages()) { - QProfileWithId qProfile = (QProfileWithId) moduleQProfiles.findByLanguage(language); - if (qProfile != null) { - dao.updateUsedColumn(qProfile.id(), true); - profiles.add(qProfile); + QProfile profile = moduleQProfiles.findByLanguage(language); + if (profile != null) { + used.add(profile); } } - UsedQProfiles used = UsedQProfiles.fromProfiles(profiles); - Measure detailsMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, used.toJSON()); + Measure detailsMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, used.toJson()); context.saveMeasure(detailsMeasure); - - // For backward compatibility - if (profiles.size() == 1) { - QProfileWithId qProfile = profiles.get(0); - Measure measure = new Measure(CoreMetrics.PROFILE, qProfile.name()).setValue((double) qProfile.id()); - Measure measureVersion = new Measure(CoreMetrics.PROFILE_VERSION, qProfile.version().doubleValue()); - context.saveMeasure(measure); - context.saveMeasure(measureVersion); - } } @Override 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 67c6a739f14..20a80186919 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 @@ -75,7 +75,7 @@ public class RulesProfileProvider extends ProviderAdapter { private RulesProfile select(QProfile qProfile, ActiveRules activeRules, RuleFinder ruleFinder) { RulesProfile deprecatedProfile = new RulesProfile(); - deprecatedProfile.setVersion(qProfile.version()); + // TODO deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.name()); deprecatedProfile.setLanguage(qProfile.language()); for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.language())) { 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 b65ca1b094b..41914729afa 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 @@ -93,11 +93,6 @@ public class RulesProfileWrapper extends RulesProfile { } @Override - public int getVersion() { - return getSingleProfileOrFail().getVersion(); - } - - @Override public ActiveRule getActiveRule(String repositoryKey, String ruleKey) { for (RulesProfile profile : profiles) { ActiveRule activeRule = profile.getActiveRule(repositoryKey, ruleKey); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java index 9ae587d71ba..97b883791e9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java @@ -19,67 +19,57 @@ */ package org.sonar.batch.rule; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.utils.text.JsonWriter; -import org.sonar.batch.rules.QProfileWithId; import javax.annotation.concurrent.Immutable; import java.io.StringWriter; -import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; import java.util.Map; +import java.util.SortedSet; @Immutable public class UsedQProfiles { - private final Map<Integer, QProfileWithId> profilesById = Maps.newLinkedHashMap(); - - private UsedQProfiles() { - // only static - } - - public static UsedQProfiles fromProfiles(Iterable<QProfileWithId> profiles) { - UsedQProfiles result = new UsedQProfiles(); - for (QProfileWithId qProfile : profiles) { - result.add(qProfile); + private final SortedSet<QProfile> profiles = Sets.newTreeSet(new Comparator<QProfile>() { + @Override + public int compare(QProfile o1, QProfile o2) { + int c = o1.language().compareTo(o2.language()); + if (c == 0) { + c = o1.name().compareTo(o2.name()); + } + return c; } - return result; - } - - public static UsedQProfiles empty() { - return new UsedQProfiles(); - } + }); - public static UsedQProfiles fromProfiles(QProfileWithId... profiles) { - return fromProfiles(Arrays.asList(profiles)); - } - - public static UsedQProfiles fromJSON(String json) { + public static UsedQProfiles fromJson(String json) { UsedQProfiles result = new UsedQProfiles(); JsonArray root = new JsonParser().parse(json).getAsJsonArray(); for (JsonElement elt : root) { JsonObject profile = elt.getAsJsonObject(); - result.add(new QProfileWithId(profile.get("id").getAsInt(), profile.get("name").getAsString(), profile.get("language").getAsString(), profile.get("version").getAsInt())); + result.add(new QProfile(profile.get("key").getAsString(), profile.get("name").getAsString(), profile.get("language").getAsString())); } return result; } - public final String toJSON() { + public String toJson() { StringWriter json = new StringWriter(); JsonWriter writer = JsonWriter.of(json); writer.beginArray(); - for (QProfileWithId qProfile : profilesById.values()) { + for (QProfile profile : profiles) { writer .beginObject() - .prop("id", qProfile.id()) - .prop("name", qProfile.name()) - .prop("version", qProfile.version()) - .prop("language", qProfile.language()) + .prop("key", profile.key()) + .prop("language", profile.language()) + .prop("name", profile.name()) .endObject(); } writer.endArray(); @@ -87,33 +77,30 @@ public class UsedQProfiles { return json.toString(); } - public final UsedQProfiles merge(UsedQProfiles other) { - return empty().mergeInPlace(this).mergeInPlace(other); - } - - private void add(QProfileWithId profile) { - QProfileWithId alreadyAdded = profilesById.get(profile.id()); - if (alreadyAdded == null - // Keep only latest version - || profile.version() > alreadyAdded.version()) { - profilesById.put(profile.id(), profile); - } + public UsedQProfiles add(UsedQProfiles other) { + addAll(other.profiles); + return this; } - private UsedQProfiles addAll(Iterable<QProfileWithId> profiles) { - for (QProfileWithId profile : profiles) { - this.add(profile); - } + public UsedQProfiles add(QProfile profile) { + profiles.add(profile); return this; } - private UsedQProfiles mergeInPlace(UsedQProfiles other) { - this.addAll(other.profilesById.values()); + public UsedQProfiles addAll(Collection<QProfile> profiles) { + this.profiles.addAll(profiles); return this; } - public Map<Integer, QProfileWithId> profilesById() { - return ImmutableMap.copyOf(profilesById); + public SortedSet<QProfile> profiles() { + return profiles; } + public Map<String, QProfile> profilesByKey() { + Map<String,QProfile> map = new HashMap<String, QProfile>(); + for (QProfile profile : profiles) { + map.put(profile.key(), profile); + } + return map; + } } 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 index c3d66b0df45..136c2e999b9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java @@ -20,7 +20,6 @@ package org.sonar.batch.rules; import org.sonar.api.batch.rules.QProfile; - import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; @@ -37,11 +36,11 @@ public class DefaultQProfileReferential implements QProfilesReferential { @Override public QProfile get(String language, String name) { - QualityProfileDto dto = qualityProfileDao.selectByNameAndLanguage(name, language); + QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language); if (dto == null) { return null; } - return new QProfileWithId(dto.getId(), dto.getName(), dto.getLanguage(), dto.getVersion()); + return new QProfile(dto.getKey(), dto.getName(), dto.getLanguage()); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfileWithId.java b/sonar-batch/src/main/java/org/sonar/batch/rules/QProfileWithId.java deleted file mode 100644 index c441b7ff4e7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfileWithId.java +++ /dev/null @@ -1,36 +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.api.batch.rules.QProfile; - -public class QProfileWithId extends QProfile { - private final int id; - - public QProfileWithId(int id, String name, String language, Integer version) { - super(name, language, version); - this.id = id; - } - - public int id() { - return id; - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java index c8dc898b823..d69b371f57d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java @@ -111,12 +111,12 @@ public class AnalyzerMediumTester extends ExternalResource { } public AnalyzerMediumTesterBuilder addQProfile(String language, String name) { - qProfileReferential.add(new QProfile(name, language, 1)); + qProfileReferential.add(new QProfile("TODO", name, language)); return this; } public AnalyzerMediumTesterBuilder addDefaultQProfile(String language, String name) { - qProfileReferential.add(new QProfile(name, language, 1)); + qProfileReferential.add(new QProfile("TODO", name, language)); settingsReferential.globalSettings().put("sonar.profile." + language, name); return this; } 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 index cf809549abd..52d5d495762 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java @@ -19,17 +19,15 @@ */ package org.sonar.batch.rule; -import org.sonar.api.batch.rules.QProfile; - 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.batch.rules.QProfile; 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.batch.rules.QProfileWithId; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.qualityprofile.db.ActiveRuleDao; @@ -55,11 +53,11 @@ public class ActiveRulesProviderTest extends AbstractDaoTestCase { @Test public void build_active_rules() throws Exception { setupData("shared"); - when(qProfiles.findAll()).thenReturn(Arrays.<QProfile>asList( + when(qProfiles.findAll()).thenReturn(Arrays.asList( // 1 rule is enabled on java with severity INFO - new QProfileWithId(2, "Java Two", "java", 20), + new QProfile("java-two", "Java Two", "java"), // 1 rule is enabled on php with severity BLOCKER - new QProfileWithId(3, "Php One", "php", 30) + new QProfile("php-one", "Php One", "php") )); ActiveRulesProvider provider = new ActiveRulesProvider(); 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 index 60af454bf0b..8d30e5b4a66 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java @@ -19,10 +19,9 @@ */ package org.sonar.batch.rule; -import org.sonar.api.batch.rules.QProfile; - import com.google.common.collect.Lists; import org.junit.Test; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.config.Settings; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; @@ -31,7 +30,6 @@ 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.batch.rules.QProfileWithId; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.qualityprofile.db.QualityProfileDao; @@ -47,40 +45,40 @@ public class ModuleQProfilesTest extends AbstractDaoTestCase { @Test public void find_profiles() throws Exception { + // 4 profiles in db setupData("shared"); QualityProfileDao dao = new QualityProfileDao(getMyBatis()); QProfilesReferential ref = new DefaultQProfileReferential(dao); - settings.setProperty("sonar.profile.java", "Java Two"); + 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(); - QProfileWithId javaProfile = (QProfileWithId) qProfiles.get(0); - assertThat(javaProfile.id()).isEqualTo(2); - assertThat(javaProfile.name()).isEqualTo("Java Two"); + QProfile javaProfile = qProfiles.get(0); + assertThat(javaProfile.key()).isEqualTo("java-one"); + assertThat(javaProfile.name()).isEqualTo("Java One"); assertThat(javaProfile.language()).isEqualTo("java"); - assertThat(javaProfile.version()).isEqualTo(20); - QProfileWithId phpProfile = (QProfileWithId) qProfiles.get(1); - assertThat(phpProfile.id()).isEqualTo(3); + QProfile phpProfile = qProfiles.get(1); + assertThat(phpProfile.key()).isEqualTo("php-one"); assertThat(phpProfile.name()).isEqualTo("Php One"); assertThat(phpProfile.language()).isEqualTo("php"); - assertThat(phpProfile.version()).isEqualTo(30); - } @Test - public void use_sonar_profile_property() throws Exception { + public void supported_deprecated_property() throws Exception { setupData("shared"); QualityProfileDao dao = new QualityProfileDao(getMyBatis()); QProfilesReferential ref = new DefaultQProfileReferential(dao); + // deprecated property settings.setProperty("sonar.profile", "Java Two"); settings.setProperty("sonar.profile.php", "Php One"); @@ -88,19 +86,16 @@ public class ModuleQProfilesTest extends AbstractDaoTestCase { List<QProfile> qProfiles = Lists.newArrayList(moduleQProfiles.findAll()); assertThat(qProfiles).hasSize(2); - QProfileWithId javaProfile = (QProfileWithId) qProfiles.get(0); - assertThat(javaProfile.id()).isEqualTo(2); + QProfile javaProfile = qProfiles.get(0); + assertThat(javaProfile.key()).isEqualTo("java-two"); assertThat(javaProfile.name()).isEqualTo("Java Two"); assertThat(javaProfile.language()).isEqualTo("java"); - assertThat(javaProfile.version()).isEqualTo(20); - // Fallback to sonar.profile.php if no match for sonar.profile - QProfileWithId phpProfile = (QProfileWithId) qProfiles.get(1); - assertThat(phpProfile.id()).isEqualTo(3); + // "Java Two" does not exist for PHP -> fallback to sonar.profile.php + QProfile phpProfile = qProfiles.get(1); + assertThat(phpProfile.key()).isEqualTo("php-one"); assertThat(phpProfile.name()).isEqualTo("Php One"); assertThat(phpProfile.language()).isEqualTo("php"); - assertThat(phpProfile.version()).isEqualTo(30); - } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileDecoratorTest.java index 93bd0c1c348..16003f0a10f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileDecoratorTest.java @@ -32,15 +32,18 @@ import java.util.Collections; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class QProfileDecoratorTest { + static final String JAVA_JSON = "{\"key\":\"J1\",\"language\":\"java\",\"name\":\"Java One\"}"; + static final String JAVA2_JSON = "{\"key\":\"J2\",\"language\":\"java\",\"name\":\"Java Two\"}"; + static final String PHP_JSON = "{\"key\":\"P1\",\"language\":\"php\",\"name\":\"Php One\"}"; + Project project = mock(Project.class); Project moduleA = mock(Project.class); Project moduleB = mock(Project.class); + Project moduleC = mock(Project.class); DecoratorContext decoratorContext = mock(DecoratorContext.class); @Test @@ -49,31 +52,16 @@ public class QProfileDecoratorTest { when(project.getModules()).thenReturn(Collections.<Project>emptyList()); assertThat(decorator.shouldExecuteOnProject(project)).isFalse(); - when(project.getModules()).thenReturn(Arrays.asList(moduleA, moduleB)); + when(project.getModules()).thenReturn(Arrays.asList(moduleA, moduleB, moduleC)); assertThat(decorator.shouldExecuteOnProject(project)).isTrue(); } @Test public void aggregate() throws Exception { - Measure measureModuleA = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - Measure measureModuleB = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Php One\",\"version\":30,\"language\":\"php\"}]"); - when(decoratorContext.getChildrenMeasures(CoreMetrics.QUALITY_PROFILES)).thenReturn(Arrays.asList(measureModuleA, measureModuleB)); - - when(project.getScope()).thenReturn(Scopes.PROJECT); - - QProfileDecorator decorator = new QProfileDecorator(); - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).saveMeasure( - argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, - "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"},{\"id\":3,\"name\":\"Php One\",\"version\":30,\"language\":\"php\"}]"))); - } - - @Test - public void aggregate_several_profile_same_language() throws Exception { - Measure measureModuleA = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - Measure measureModuleB = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Java Three\",\"version\":30,\"language\":\"java\"}]"); - when(decoratorContext.getChildrenMeasures(CoreMetrics.QUALITY_PROFILES)).thenReturn(Arrays.asList(measureModuleA, measureModuleB)); + Measure measureModuleA = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_JSON + "]"); + Measure measureModuleB = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_JSON + "]"); + Measure measureModuleC = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + PHP_JSON + "]"); + when(decoratorContext.getChildrenMeasures(CoreMetrics.QUALITY_PROFILES)).thenReturn(Arrays.asList(measureModuleA, measureModuleB, measureModuleC)); when(project.getScope()).thenReturn(Scopes.PROJECT); @@ -81,14 +69,13 @@ public class QProfileDecoratorTest { decorator.decorate(project, decoratorContext); verify(decoratorContext).saveMeasure( - argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, - "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"},{\"id\":3,\"name\":\"Java Three\",\"version\":30,\"language\":\"java\"}]"))); + argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_JSON + "," + PHP_JSON + "]"))); } @Test - public void aggregate_several_profile_same_id() throws Exception { - Measure measureModuleA = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - Measure measureModuleB = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":30,\"language\":\"java\"}]"); + public void aggregate_different_profiles_with_same_language() throws Exception { + Measure measureModuleA = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_JSON + "]"); + Measure measureModuleB = new Measure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA2_JSON + "]"); when(decoratorContext.getChildrenMeasures(CoreMetrics.QUALITY_PROFILES)).thenReturn(Arrays.asList(measureModuleA, measureModuleB)); when(project.getScope()).thenReturn(Scopes.PROJECT); @@ -97,7 +84,6 @@ public class QProfileDecoratorTest { decorator.decorate(project, decoratorContext); verify(decoratorContext).saveMeasure( - argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, - "[{\"id\":2,\"name\":\"Java Two\",\"version\":30,\"language\":\"java\"}]"))); + argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, "[" + JAVA_JSON + "," + JAVA2_JSON + "]"))); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java index ba20e0a1f98..c2be9665707 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileEventsDecoratorTest.java @@ -17,197 +17,213 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +/* +* 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.Test; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.Event; -import org.sonar.api.batch.TimeMachine; -import org.sonar.api.batch.TimeMachineQuery; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.Java; -import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Project; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.same; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class QProfileEventsDecoratorTest { - - Project project = new Project("myProject"); - DecoratorContext decoratorContext = mock(DecoratorContext.class); - TimeMachine timeMachine = mock(TimeMachine.class); - private Languages languages = mock(Languages.class); - private QualityProfileDao qualityProfileDao = mock(QualityProfileDao.class); - QProfileEventsDecorator decorator = new QProfileEventsDecorator(timeMachine, qualityProfileDao, languages); - - @Test - public void shouldExecuteOnProjects() { - assertThat(decorator.shouldExecuteOnProject(project)).isTrue(); - } - - @Test - public void shouldDoNothingIfNoProfileChange() { - Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - - when(timeMachine.getMeasures(any(TimeMachineQuery.class))) - .thenReturn(Arrays.asList(previousMeasure)); - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); - } - - @Test - public void shouldDoNothingIfNoProfileChange_fallbackOldProfileMeasure() { - mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); - when(qualityProfileDao.selectById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); - } - - @Test - public void shouldCreateEventIfProfileChange() { - Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - // Different profile - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Java Other\",\"version\":1,\"language\":\"java\"}]"); - - when(timeMachine.getMeasures(any(TimeMachineQuery.class))) - .thenReturn(Arrays.asList(previousMeasure)); - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).createEvent( - eq("Use Java Other version 1 (Java)"), - eq("Java Other version 1 used for Java"), - same(Event.CATEGORY_PROFILE), any(Date.class)); - } - - @Test - public void shouldCreateEventIfProfileChange_fallbackOldProfileMeasure() { - mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); - when(qualityProfileDao.selectById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); - // Different profile - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Java Other\",\"version\":1,\"language\":\"java\"}]"); - - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).createEvent( - eq("Use Java Other version 1 (Java)"), - eq("Java Other version 1 used for Java"), - same(Event.CATEGORY_PROFILE), any(Date.class)); - } - - @Test - public void shouldCreateEventIfProfileVersionChange() { - Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); - // Same profile, different version - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); - - when(timeMachine.getMeasures(any(TimeMachineQuery.class))) - .thenReturn(Arrays.asList(previousMeasure)); - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).createEvent( - eq("Use Java Two version 21 (Java)"), - eq("Java Two version 21 used for Java"), - same(Event.CATEGORY_PROFILE), any(Date.class)); - } - - @Test - public void shouldCreateEventIfProfileVersionChange_fallbackOldProfileMeasure() { - mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); - when(qualityProfileDao.selectById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); - // Same profile, different version - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); - - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).createEvent( - eq("Use Java Two version 21 (Java)"), - eq("Java Two version 21 used for Java"), - same(Event.CATEGORY_PROFILE), any(Date.class)); - } - - @Test - public void shouldCreateEventIfProfileVersionChange_fallbackOldProfileMeasure_noVersion() { - mockTMWithDeprecatedProfileMeasures(2, "Java Two", null); - when(qualityProfileDao.selectById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); - // Same profile, different version - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); - - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext).createEvent( - eq("Use Java Two version 21 (Java)"), - eq("Java Two version 21 used for Java"), - same(Event.CATEGORY_PROFILE), any(Date.class)); - } - - @Test - public void shouldNotCreateEventIfFirstAnalysis() { - Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); - - when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); - - when(languages.get("java")).thenReturn(Java.INSTANCE); - - decorator.decorate(project, decoratorContext); - - verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); - } - - private void mockTMWithDeprecatedProfileMeasures(double profileId, String profileName, Integer versionValue) { - mockTM(new Measure(CoreMetrics.PROFILE, profileId, profileName), versionValue == null ? null : new Measure(CoreMetrics.PROFILE_VERSION, Double.valueOf(versionValue))); - } - - private void mockTM(Measure result1, Measure result2) { - when(timeMachine.getMeasures(any(TimeMachineQuery.class))) - .thenReturn(Collections.<Measure>emptyList()) - .thenReturn(result1 == null ? Collections.<Measure>emptyList() : Arrays.asList(result1)) - .thenReturn(result2 == null ? Collections.<Measure>emptyList() : Arrays.asList(result2)); - - } -} +//import org.junit.Test; +//import org.sonar.api.batch.DecoratorContext; +//import org.sonar.api.batch.Event; +//import org.sonar.api.batch.TimeMachine; +//import org.sonar.api.batch.TimeMachineQuery; +//import org.sonar.api.measures.CoreMetrics; +//import org.sonar.api.measures.Measure; +//import org.sonar.api.resources.Java; +//import org.sonar.api.resources.Languages; +//import org.sonar.api.resources.Project; +//import org.sonar.core.qualityprofile.db.QualityProfileDao; +//import org.sonar.core.qualityprofile.db.QualityProfileDto; +// +//import java.util.Arrays; +//import java.util.Collections; +//import java.util.Date; +// +//import static org.fest.assertions.Assertions.assertThat; +//import static org.mockito.Matchers.any; +//import static org.mockito.Matchers.anyString; +//import static org.mockito.Matchers.eq; +//import static org.mockito.Matchers.same; +//import static org.mockito.Mockito.mock; +//import static org.mockito.Mockito.never; +//import static org.mockito.Mockito.verify; +//import static org.mockito.Mockito.when; +// +//public class QProfileEventsDecoratorTest { +// +// Project project = new Project("myProject"); +// DecoratorContext decoratorContext = mock(DecoratorContext.class); +// TimeMachine timeMachine = mock(TimeMachine.class); +// Languages languages = mock(Languages.class); +// QProfileEventsDecorator decorator = new QProfileEventsDecorator(timeMachine, languages); +// +// @Test +// public void shouldExecuteOnProjects() { +// assertThat(decorator.shouldExecuteOnProject(project)).isTrue(); +// } +// +// @Test +// public void shouldDoNothingIfNoProfileChange() { +// Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); +// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); +// +// when(timeMachine.getMeasures(any(TimeMachineQuery.class))) +// .thenReturn(Arrays.asList(previousMeasure)); +// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +// +// decorator.decorate(project, decoratorContext); +// +// verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); +// } +// +// @Test +// public void shouldDoNothingIfNoProfileChange_fallbackOldProfileMeasure() { +// mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); +// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"key\":\"p2\",\"name\":\"Java Two\",\"language\":\"java\"}]"); +// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +// +// when(languages.get("java")).thenReturn(Java.INSTANCE); +// +// decorator.decorate(project, decoratorContext); +// +// verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); +// } +// +// @Test +// public void shouldCreateEventIfProfileChange() { +// Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); +// // Different profile +// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Java Other\",\"version\":1,\"language\":\"java\"}]"); +// +// when(timeMachine.getMeasures(any(TimeMachineQuery.class))) +// .thenReturn(Arrays.asList(previousMeasure)); +// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +// +// when(languages.get("java")).thenReturn(Java.INSTANCE); +// +// decorator.decorate(project, decoratorContext); +// +// verify(decoratorContext).createEvent( +// eq("Use Java Other version 1 (Java)"), +// eq("Java Other version 1 used for Java"), +// same(Event.CATEGORY_PROFILE), any(Date.class)); +// } +//// +//// @Test +//// public void shouldCreateEventIfProfileChange_fallbackOldProfileMeasure() { +//// mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); +//// when(qualityProfileDao.getById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); +//// // Different profile +//// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":3,\"name\":\"Java Other\",\"version\":1,\"language\":\"java\"}]"); +//// +//// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +//// +//// when(languages.get("java")).thenReturn(Java.INSTANCE); +//// +//// decorator.decorate(project, decoratorContext); +//// +//// verify(decoratorContext).createEvent( +//// eq("Use Java Other version 1 (Java)"), +//// eq("Java Other version 1 used for Java"), +//// same(Event.CATEGORY_PROFILE), any(Date.class)); +//// } +//// +//// @Test +//// public void shouldCreateEventIfProfileVersionChange() { +//// Measure previousMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"); +//// // Same profile, different version +//// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); +//// +//// when(timeMachine.getMeasures(any(TimeMachineQuery.class))) +//// .thenReturn(Arrays.asList(previousMeasure)); +//// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +//// +//// when(languages.get("java")).thenReturn(Java.INSTANCE); +//// +//// decorator.decorate(project, decoratorContext); +//// +//// verify(decoratorContext).createEvent( +//// eq("Use Java Two version 21 (Java)"), +//// eq("Java Two version 21 used for Java"), +//// same(Event.CATEGORY_PROFILE), any(Date.class)); +//// } +//// +//// @Test +//// public void shouldCreateEventIfProfileVersionChange_fallbackOldProfileMeasure() { +//// mockTMWithDeprecatedProfileMeasures(2, "Java Two", 20); +//// when(qualityProfileDao.getById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); +//// // Same profile, different version +//// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); +//// +//// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +//// +//// when(languages.get("java")).thenReturn(Java.INSTANCE); +//// +//// decorator.decorate(project, decoratorContext); +//// +//// verify(decoratorContext).createEvent( +//// eq("Use Java Two version 21 (Java)"), +//// eq("Java Two version 21 used for Java"), +//// same(Event.CATEGORY_PROFILE), any(Date.class)); +//// } +//// +//// @Test +//// public void shouldCreateEventIfProfileVersionChange_fallbackOldProfileMeasure_noVersion() { +//// mockTMWithDeprecatedProfileMeasures(2, "Java Two", null); +//// when(qualityProfileDao.getById(20)).thenReturn(new QualityProfileDto().setLanguage("java")); +//// // Same profile, different version +//// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); +//// +//// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +//// +//// when(languages.get("java")).thenReturn(Java.INSTANCE); +//// +//// decorator.decorate(project, decoratorContext); +//// +//// verify(decoratorContext).createEvent( +//// eq("Use Java Two version 21 (Java)"), +//// eq("Java Two version 21 used for Java"), +//// same(Event.CATEGORY_PROFILE), any(Date.class)); +//// } +// +// @Test +// public void shouldNotCreateEventIfFirstAnalysis() { +// Measure newMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":21,\"language\":\"java\"}]"); +// +// when(decoratorContext.getMeasure(CoreMetrics.QUALITY_PROFILES)).thenReturn(newMeasure); +// +// when(languages.get("java")).thenReturn(Java.INSTANCE); +// +// decorator.decorate(project, decoratorContext); +// +// verify(decoratorContext, never()).createEvent(anyString(), anyString(), anyString(), any(Date.class)); +// } +// +// private void mockTMWithDeprecatedProfileMeasures(double profileId, String profileName, Integer versionValue) { +// mockTM(new Measure(CoreMetrics.QUALITY_PROFILES, profileId, profileName), versionValue == null ? null : new Measure(CoreMetrics.PROFILE_VERSION, Double.valueOf(versionValue))); +// } +// +// private void mockTM(Measure result1, Measure result2) { +// when(timeMachine.getMeasures(any(TimeMachineQuery.class))) +// .thenReturn(Collections.<Measure>emptyList()) +// .thenReturn(result1 == null ? Collections.<Measure>emptyList() : Arrays.asList(result1)) +// .thenReturn(result2 == null ? Collections.<Measure>emptyList() : Arrays.asList(result2)); +// +// } +//} diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java index 1bc8325f531..830b980a6c5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java @@ -19,27 +19,21 @@ */ package org.sonar.batch.rule; -import org.sonar.api.batch.rules.QProfile; - import org.junit.Test; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.resources.Project; import org.sonar.api.test.IsMeasure; -import org.sonar.batch.rules.QProfileWithId; -import org.sonar.core.persistence.AbstractDaoTestCase; -import org.sonar.core.qualityprofile.db.QualityProfileDao; import java.util.Collections; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; -public class QProfileSensorTest extends AbstractDaoTestCase { +public class QProfileSensorTest { ModuleQProfiles moduleQProfiles = mock(ModuleQProfiles.class); Project project = mock(Project.class); @@ -48,18 +42,15 @@ public class QProfileSensorTest extends AbstractDaoTestCase { @Test public void to_string() throws Exception { - QualityProfileDao dao = mock(QualityProfileDao.class); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, dao); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); assertThat(sensor.toString()).isEqualTo("QProfileSensor"); } @Test public void no_qprofiles() throws Exception { - setupData("shared"); - QualityProfileDao dao = new QualityProfileDao(getMyBatis()); when(moduleQProfiles.findAll()).thenReturn(Collections.<QProfile>emptyList()); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, dao); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); @@ -69,57 +60,44 @@ public class QProfileSensorTest extends AbstractDaoTestCase { @Test public void mark_profiles_as_used() throws Exception { - setupData("shared"); - - QualityProfileDao dao = new QualityProfileDao(getMyBatis()); - when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfileWithId(2, "Java Two", "java", 20)); - when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfileWithId(3, "Php One", "php", 30)); + when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfile("java-two", "Java Two", "java")); + when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfile("php-one", "Php One", "php")); when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java", "php", "abap"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, dao); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); - - checkTable("mark_profiles_as_used", "rules_profiles"); } @Test public void store_measures_on_single_lang_module() throws Exception { - setupData("shared"); - - QualityProfileDao dao = new QualityProfileDao(getMyBatis()); - when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfileWithId(2, "Java Two", "java", 20)); - when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfileWithId(3, "Php One", "php", 30)); + when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfile("java-two", "Java Two", "java")); + when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfile("php-one", "Php One", "php")); when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, dao); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); - verify(sensorContext).saveMeasure(argThat(new IsMeasure(CoreMetrics.PROFILE, "Java Two"))); - verify(sensorContext).saveMeasure(argThat(new IsMeasure(CoreMetrics.PROFILE_VERSION, 20.0))); verify(sensorContext).saveMeasure( - argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"}]"))); + argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, "[{\"key\":\"java-two\",\"language\":\"java\",\"name\":\"Java Two\"}]"))); } @Test public void store_measures_on_multi_lang_module() throws Exception { - setupData("shared"); - - QualityProfileDao dao = new QualityProfileDao(getMyBatis()); - when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfileWithId(2, "Java Two", "java", 20)); - when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfileWithId(3, "Php One", "php", 30)); + when(moduleQProfiles.findByLanguage("java")).thenReturn(new QProfile("java-two", "Java Two", "java")); + when(moduleQProfiles.findByLanguage("php")).thenReturn(new QProfile("php-one", "Php One", "php")); when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java", "php"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, dao); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); verify(sensorContext).saveMeasure( argThat(new IsMeasure(CoreMetrics.QUALITY_PROFILES, - "[{\"id\":2,\"name\":\"Java Two\",\"version\":20,\"language\":\"java\"},{\"id\":3,\"name\":\"Php One\",\"version\":30,\"language\":\"php\"}]"))); + "[{\"key\":\"java-two\",\"language\":\"java\",\"name\":\"Java Two\"},{\"key\":\"php-one\",\"language\":\"php\",\"name\":\"Php One\"}]"))); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java index 483b6c7e397..d8de8ea3055 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java @@ -25,14 +25,11 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.rules.QProfile; import org.sonar.api.config.Settings; -import org.sonar.api.profiles.RulesProfile; import org.sonar.api.utils.MessageException; -import org.sonar.batch.rules.QProfileWithId; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class QProfileVerifierTest { @@ -42,19 +39,13 @@ public class QProfileVerifierTest { DefaultFileSystem fs = new DefaultFileSystem(); ModuleQProfiles profiles; Settings settings = new Settings(); - RulesProfile javaRulesProfile; - RulesProfile cobolRulesProfile; @Before public void before() { profiles = mock(ModuleQProfiles.class); - QProfileWithId javaProfile = mock(QProfileWithId.class); - when(javaProfile.name()).thenReturn("My Java profile"); - javaRulesProfile = mock(RulesProfile.class); + QProfile javaProfile = new QProfile("p1", "My Java profile", "java"); when(profiles.findByLanguage("java")).thenReturn(javaProfile); - QProfileWithId cobolProfile = mock(QProfileWithId.class); - when(cobolProfile.name()).thenReturn("My Cobol profile"); - cobolRulesProfile = mock(RulesProfile.class); + QProfile cobolProfile = new QProfile("p2", "My Cobol profile", "cobol"); when(profiles.findByLanguage("cobol")).thenReturn(cobolProfile); } 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 index 499e3605220..8c14a751d4e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java @@ -26,7 +26,6 @@ import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.config.Settings; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rules.RuleFinder; -import org.sonar.batch.rules.QProfileWithId; import java.util.Arrays; @@ -45,8 +44,8 @@ public class RulesProfileProviderTest { @Test public void merge_profiles() throws Exception { - QProfileWithId qProfile = new QProfileWithId(33, "Sonar way", "java", 12); - when(qProfiles.findAll()).thenReturn(Arrays.<QProfile>asList(qProfile)); + QProfile qProfile = new QProfile("java-sw", "Sonar way", "java"); + when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile)); RulesProfile profile = provider.provide(qProfiles, activeRules, ruleFinder, settings); @@ -67,7 +66,7 @@ public class RulesProfileProviderTest { public void keep_compatibility_with_single_language_projects() throws Exception { settings.setProperty("sonar.language", "java"); - QProfileWithId qProfile = new QProfileWithId(33, "Sonar way", "java", 12); + QProfile qProfile = new QProfile("java-sw", "Sonar way", "java"); when(qProfiles.findByLanguage("java")).thenReturn(qProfile); RulesProfile profile = provider.provide(qProfiles, activeRules, ruleFinder, settings); @@ -75,7 +74,6 @@ public class RulesProfileProviderTest { // 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); + assertThat(profile.getName()).isEqualTo("Sonar way");; } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java index 49825359b6e..42ab22fed38 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java @@ -36,13 +36,11 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.api.utils.Duration; import org.sonar.api.utils.Durations; -import org.sonar.core.cluster.WorkQueue; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.rule.RuleDao; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; -import static org.mockito.Mockito.mock; @RunWith(MockitoJUnitRunner.class) public class RulesProviderTest extends AbstractDaoTestCase { diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/UsedQProfilesTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/UsedQProfilesTest.java index 1e9d47db0ec..671dbd2f6da 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/UsedQProfilesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/UsedQProfilesTest.java @@ -20,54 +20,48 @@ package org.sonar.batch.rule; import org.junit.Test; -import org.sonar.batch.rules.QProfileWithId; +import org.sonar.api.batch.rules.QProfile; + +import java.util.Arrays; +import java.util.Map; import static org.fest.assertions.Assertions.assertThat; public class UsedQProfilesTest { @Test - public void serialization() throws Exception { - - QProfileWithId java = new QProfileWithId(1, "Sonar Way", "java", 1); - QProfileWithId php = new QProfileWithId(2, "Sonar Way", "php", 1); - - UsedQProfiles used = UsedQProfiles.fromProfiles(java, php); - assertThat(used.toJSON()).isEqualTo( - "[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); - } + public void from_and_to_json() throws Exception { + QProfile java = new QProfile("p1", "Sonar Way", "java"); + QProfile php = new QProfile("p2", "Sonar Way", "php"); - @Test - public void deserialization() throws Exception { - UsedQProfiles used = UsedQProfiles - .fromJSON("[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); + UsedQProfiles used = new UsedQProfiles().add(java).add(php); + String json = "[{\"key\":\"p1\",\"language\":\"java\",\"name\":\"Sonar Way\"},{\"key\":\"p2\",\"language\":\"php\",\"name\":\"Sonar Way\"}]"; + assertThat(used.toJson()).isEqualTo(json); - assertThat(used.toJSON()).isEqualTo( - "[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); + used = UsedQProfiles.fromJson(json); + assertThat(used.profiles()).hasSize(2); + assertThat(used.profiles().first().key()).isEqualTo("p1"); + assertThat(used.profiles().last().key()).isEqualTo("p2"); } @Test - public void merge() throws Exception { - UsedQProfiles first = UsedQProfiles - .fromJSON("[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"}]"); - - UsedQProfiles second = UsedQProfiles - .fromJSON("[{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); + public void do_not_duplicate_profiles() throws Exception { + QProfile java = new QProfile("p1", "Sonar Way", "java"); + QProfile php = new QProfile("p2", "Sonar Way", "php"); - assertThat(first.merge(second).toJSON()).isEqualTo( - "[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); + UsedQProfiles used = new UsedQProfiles().addAll(Arrays.asList(java, java, php)); + assertThat(used.profiles()).hasSize(2); } @Test - public void merge_no_duplicate_ids() throws Exception { - UsedQProfiles first = UsedQProfiles - .fromJSON("[{\"id\":1,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":2,\"language\":\"php\"}]"); + public void group_profiles_by_key() throws Exception { + QProfile java = new QProfile("p1", "Sonar Way", "java"); + QProfile php = new QProfile("p2", "Sonar Way", "php"); - UsedQProfiles second = UsedQProfiles - .fromJSON("[{\"id\":1,\"name\":\"Sonar Way\",\"version\":2,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":1,\"language\":\"php\"}]"); - - assertThat(first.merge(second).toJSON()).isEqualTo( - "[{\"id\":1,\"name\":\"Sonar Way\",\"version\":2,\"language\":\"java\"},{\"id\":2,\"name\":\"Sonar Way\",\"version\":2,\"language\":\"php\"}]"); + UsedQProfiles used = new UsedQProfiles().addAll(Arrays.asList(java, java, php)); + Map<String, QProfile> map = used.profilesByKey(); + assertThat(map).hasSize(2); + assertThat(map.get("p1")).isSameAs(java); + assertThat(map.get("p2")).isSameAs(php); } - } diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/ActiveRulesProviderTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/ActiveRulesProviderTest/shared.xml index ca968f8fe5f..1dc7d2b0423 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/rule/ActiveRulesProviderTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/rule/ActiveRulesProviderTest/shared.xml @@ -1,16 +1,12 @@ <dataset> - <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10" - used_profile="[false]"/> + <rules_profiles id="1" name="Java One" language="java" parent_kee="[null]" kee="java-one"/> - <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20" - used_profile="[false]"/> + <rules_profiles id="2" name="Java Two" language="java" parent_kee="[null]" kee="java-two"/> - <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30" - used_profile="[false]"/> + <rules_profiles id="3" name="Php One" language="php" parent_kee="[null]" kee="php-one"/> - <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40" - used_profile="[false]"/> + <rules_profiles id="4" name="Cobol One" language="cbl" parent_kee="[null]" kee="cobol-one"/> <!-- java --> <active_rules created_at="[null]" updated_at="[null]" id="1" profile_id="2" rule_id="10" failure_level="0" inheritance="[null]"/> diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/ModuleQProfilesTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/ModuleQProfilesTest/shared.xml index 425b89de3b0..7dd90b77196 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/rule/ModuleQProfilesTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/rule/ModuleQProfilesTest/shared.xml @@ -1,15 +1,11 @@ <dataset> - <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10" - used_profile="[false]"/> + <rules_profiles id="1" name="Java One" language="java" parent_kee="[null]" kee="java-one"/> - <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20" - used_profile="[false]"/> + <rules_profiles id="2" name="Java Two" language="java" parent_kee="[null]" kee="java-two"/> - <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30" - used_profile="[false]"/> + <rules_profiles id="3" name="Php One" language="php" parent_kee="[null]" kee="php-one"/> - <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40" - used_profile="[false]"/> + <rules_profiles id="4" name="Cobol One" language="cbl" parent_kee="[null]" kee="cobol-one"/> </dataset> diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/mark_profiles_as_used-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/mark_profiles_as_used-result.xml deleted file mode 100644 index a94dc93f01c..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/mark_profiles_as_used-result.xml +++ /dev/null @@ -1,15 +0,0 @@ -<dataset> - - <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10" - used_profile="[false]"/> - - <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20" - used_profile="[true]"/> - - <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30" - used_profile="[true]"/> - - <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40" - used_profile="[false]"/> - -</dataset> diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/shared.xml deleted file mode 100644 index 425b89de3b0..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/rule/QProfileSensorTest/shared.xml +++ /dev/null @@ -1,15 +0,0 @@ -<dataset> - - <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10" - used_profile="[false]"/> - - <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20" - used_profile="[false]"/> - - <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30" - used_profile="[false]"/> - - <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40" - used_profile="[false]"/> - -</dataset> |