From e9c584937072015234a4df4250aaffaf87c5d7ea Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 23 May 2014 12:06:44 +0200 Subject: [PATCH] SONAR-5007 add tests for RegisterQualityProfiles --- .../RegisterQualityProfiles.java | 5 +- .../qualityprofile/QProfilesMediumTest.java | 6 +- .../RegisterQualityProfilesMediumTest.java | 98 ++++++++++++++----- 3 files changed, 81 insertions(+), 28 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index 0f3e3726d7b..079e834df8d 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -120,7 +120,7 @@ public class RegisterQualityProfiles implements ServerComponent { Set defaultProfileNames = defaultProfileNames(profiles); if (defaultProfileNames.size() > 1) { - throw new IllegalStateException("Several Quality Profiles are flagged as default for the language " + language + ": " + defaultProfileNames); + throw new IllegalStateException("Several Quality profiles are flagged as default for the language " + language + ": " + defaultProfileNames); } } @@ -129,7 +129,6 @@ public class RegisterQualityProfiles implements ServerComponent { QualityProfileDto profileDto = dbClient.qualityProfileDao().getByKey(key, session); if (profileDto != null) { - // cleanup cleanUp(key, profileDto, session); } insertNewProfile(key, session); @@ -217,7 +216,7 @@ public class RegisterQualityProfiles implements ServerComponent { } private static Set defaultProfileNames(Collection profiles) { - Set names = Sets.newHashSet(); + Set names = Sets.newTreeSet(); for (RulesProfile profile : profiles) { if (profile.getDefaultProfile()) { names.add(profile.getName()); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java index 9a27b0a30ba..11186909948 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java @@ -52,9 +52,9 @@ public class QProfilesMediumTest { public void recreate_built_in_profile_from_language() throws Exception { // MockUserSession.set().setLogin("julien").setName("Julien").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); // -// QProfiles qProfiles = serverTester.get(QProfiles.class); -// QProfileBackup qProfileBackup = serverTester.get(QProfileBackup.class); -// Rules rules = serverTester.get(Rules.class); +// QProfiles qProfiles = tester.get(QProfiles.class); +// QProfileBackup qProfileBackup = tester.get(QProfileBackup.class); +// Rules rules = tester.get(Rules.class); // // QProfile profile = qProfiles.profile("Basic", "xoo"); // diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java index 22611f3c2aa..ad402a28ad4 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java @@ -21,7 +21,6 @@ package org.sonar.server.qualityprofile; import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; @@ -54,50 +53,49 @@ import static org.fest.assertions.Assertions.assertThat; public class RegisterQualityProfilesMediumTest { - @org.junit.Rule - public ServerTester serverTester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); - - DbClient dbClient; - DbSession session; - - @Before - public void setup() { - dbClient = serverTester.get(DbClient.class); - session = dbClient.openSession(false); - } + ServerTester tester; + DbSession dbSession; @After public void tearDown() throws Exception { - session.close(); + if (dbSession != null) { + dbSession.close(); + } + if (tester != null) { + tester.stop(); + } } @Test public void register_profile_definitions() throws Exception { + tester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); + tester.start(); + dbSession = dbClient().openSession(false); QualityProfileKey qualityProfileKey = QualityProfileKey.of("Basic", "xoo"); // Check Profile in DB - QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao(); - assertThat(qualityProfileDao.findAll(session)).hasSize(1); - assertThat(qualityProfileDao.getByKey(qualityProfileKey, session)).isNotNull(); + QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao(); + assertThat(qualityProfileDao.findAll(dbSession)).hasSize(1); + assertThat(qualityProfileDao.getByKey(qualityProfileKey, dbSession)).isNotNull(); // Check Default Profile - PropertiesDao propertiesDao = dbClient.propertiesDao(); - List xooDefault = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), session); + PropertiesDao propertiesDao = dbClient().propertiesDao(); + List xooDefault = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), dbSession); assertThat(xooDefault).hasSize(1); assertThat(xooDefault.get(0).getValue()).isEqualTo("Basic"); // Check ActiveRules in DB - ActiveRuleDao activeRuleDao = dbClient.activeRuleDao(); - assertThat(activeRuleDao.findByProfileKey(qualityProfileKey, session)).hasSize(2); + ActiveRuleDao activeRuleDao = dbClient().activeRuleDao(); + assertThat(activeRuleDao.findByProfileKey(qualityProfileKey, dbSession)).hasSize(2); RuleKey ruleKey = RuleKey.of("xoo", "x1"); - ActiveRuleDto activeRule = activeRuleDao.getByKey(ActiveRuleKey.of(qualityProfileKey, ruleKey), session); + ActiveRuleDto activeRule = activeRuleDao.getByKey(ActiveRuleKey.of(qualityProfileKey, ruleKey), dbSession); assertThat(activeRule.getKey().qProfile()).isEqualTo(qualityProfileKey); assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey); assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL); // Check ActiveRuleParameters in DB - Map params = ActiveRuleParamDto.groupByKey(activeRuleDao.findParamsByActiveRule(activeRule, session)); + Map params = ActiveRuleParamDto.groupByKey(activeRuleDao.findParamsByActiveRule(activeRule, dbSession)); assertThat(params).hasSize(2); // set by profile assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true"); @@ -105,6 +103,45 @@ public class RegisterQualityProfilesMediumTest { assertThat(params.get("max").getValue()).isEqualTo("10"); } + @Test + public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() throws Exception { + tester = new ServerTester().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true)); + + try { + tester.start(); + } catch (IllegalStateException e) { + assertThat(e).hasMessage("Several Quality profiles are flagged as default for the language xoo: [one, two]"); + } + } + + @Test + public void mark_profile_as_default() throws Exception { + tester = new ServerTester().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true)); + + tester.start(); + dbSession = dbClient().openSession(false); + PropertiesDao propertiesDao = dbClient().propertiesDao(); + List props = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), dbSession); + assertThat(props).hasSize(1); + assertThat(props.get(0).getValue()).isEqualTo("two"); + } + + @Test + public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() throws Exception { + tester = new ServerTester().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false)); + + tester.start(); + dbSession = dbClient().openSession(false); + PropertiesDao propertiesDao = dbClient().propertiesDao(); + List props = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), dbSession); + assertThat(props).hasSize(1); + assertThat(props.get(0).getValue()).isEqualTo("Sonar way"); + } + + private DbClient dbClient() { + return tester.get(DbClient.class); + } + public static class XooProfileDefinition extends ProfileDefinition { @Override public RulesProfile createProfile(ValidationMessages validation) { @@ -143,4 +180,21 @@ public class RegisterQualityProfilesMediumTest { repository.done(); } } + + public static class SimpleProfileDefinition extends ProfileDefinition { + private final boolean asDefault; + private final String name; + + public SimpleProfileDefinition(String name, boolean asDefault) { + this.name = name; + this.asDefault = asDefault; + } + + @Override + public RulesProfile createProfile(ValidationMessages validation) { + RulesProfile profile = RulesProfile.create(name, "xoo"); + profile.setDefaultProfile(asDefault); + return profile; + } + } } -- 2.39.5