]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 add tests for RegisterQualityProfiles
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 23 May 2014 10:06:44 +0000 (12:06 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 23 May 2014 10:32:30 +0000 (12:32 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java

index 0f3e3726d7bcce3ef79008709c3228cf19c2c068..079e834df8d78c8c54f6686df08ff540f926179d 100644 (file)
@@ -120,7 +120,7 @@ public class RegisterQualityProfiles implements ServerComponent {
 
     Set<String> 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<String> defaultProfileNames(Collection<RulesProfile> profiles) {
-    Set<String> names = Sets.newHashSet();
+    Set<String> names = Sets.newTreeSet();
     for (RulesProfile profile : profiles) {
       if (profile.getDefaultProfile()) {
         names.add(profile.getName());
index 9a27b0a30bae959ac83506f1367f32bb273aa047..11186909948fb7f6d1dbba7226e0fcd95ad59650 100644 (file)
@@ -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");
 //
index 22611f3c2aae679c8891007304908fa4441629bf..ad402a28ad4a2dcd601e1294ee4503d7d1b1a9db 100644 (file)
@@ -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<PropertyDto> xooDefault = propertiesDao.selectByQuery(PropertyQuery.builder().setKey("sonar.profile.xoo").build(), session);
+    PropertiesDao propertiesDao = dbClient().propertiesDao();
+    List<PropertyDto> 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<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.findParamsByActiveRule(activeRule, session));
+    Map<String, ActiveRuleParamDto> 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<PropertyDto> 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<PropertyDto> 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;
+    }
+  }
 }