]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 improve medium tests for testing server startup tasks
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 12 Jun 2014 12:55:43 +0000 (14:55 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 12 Jun 2014 12:57:50 +0000 (14:57 +0200)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java
sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java

index 76146a8f8ac3ea5f4dae223cc60f2a6a2a905d72..dc1ce4f53b9c8ea504aa78253ca48279cdebdc52 100644 (file)
@@ -119,7 +119,11 @@ public class Platform {
     level4Container = level3Container.createChild();
     serverComponents.startLevel4Components(level4Container);
     currentContainer = level4Container;
+    executeStartupTasks();
+  }
 
+  public void executeStartupTasks() {
+    serverComponents.executeStartupTasks(level4Container);
   }
 
   public void restart() {
index f01552be46a914ca269ec0ed8ccf806938ecdc21..ba51176ec2c0c90492bab11b585a53363cca978f 100644 (file)
@@ -653,14 +653,13 @@ class ServerComponents {
     extensionInstaller.installExtensions(pico);
 
     pico.startComponents();
-    executeStartupTaks(pico);
   }
 
   void addComponents(Collection components) {
     this.level4AddedComponents.addAll(components);
   }
 
-  private void executeStartupTaks(ComponentContainer pico) {
+  public void executeStartupTasks(ComponentContainer pico) {
     final ComponentContainer startupContainer = pico.createChild();
     startupContainer.addSingleton(GwtPublisher.class);
     startupContainer.addSingleton(RegisterMetrics.class);
index cfc7de00cea0e381cce864ed1228044b94e04719..df115467d1c7b434f1d45b90b67655f1aec25a11 100644 (file)
@@ -87,7 +87,7 @@ public class RegisterPermissionTemplates {
   }
 
   private void addGroupPermission(PermissionTemplateDto template, String permission, String groupName) {
-    Long groupId;
+    Long groupId = null;
     if (DefaultGroups.isAnyone(groupName)) {
       groupId = null;
     } else {
@@ -95,7 +95,7 @@ public class RegisterPermissionTemplates {
       if (groupDto != null) {
         groupId = groupDto.getId();
       } else {
-        throw new IllegalArgumentException("Cannot setup default permission for group: " + groupName);
+        LOG.error("Cannot setup default permission for group: " + groupName);
       }
     }
     permissionTemplateDao.addGroupPermission(template.getId(), groupId, permission);
index 34797df5b82ac16f353ce9d7d89ec68af39018c1..7fbf4d129aa1bf6a3acab597dbb88db71b5f67b1 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.rule;
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -52,26 +53,28 @@ import static org.fest.assertions.Assertions.assertThat;
 
 public class RegisterRulesMediumTest {
 
-  // Hack to restart server without x2
-  static boolean includeX1 = true, includeX2 = true;
+  static XooRulesDefinition rulesDefinition = new XooRulesDefinition();
 
-  @org.junit.Rule
-  public ServerTester tester = new ServerTester().addComponents(XooRulesDefinition.class);
+  @ClassRule
+  public static ServerTester tester = new ServerTester().addComponents(rulesDefinition);
 
   DbClient db;
   DbSession dbSession;
 
   @Before
   public void before() {
+    tester.clearDbAndIndexes();
+    rulesDefinition.includeX1 = true;
+    rulesDefinition.includeX2 = true;
+    tester.get(Platform.class).executeStartupTasks();
     db = tester.get(DbClient.class);
     dbSession = tester.get(DbClient.class).openSession(false);
+    dbSession.clearCache();
   }
 
   @After
   public void after() {
     dbSession.close();
-    includeX1 = true;
-    includeX2 = true;
   }
 
   @Test
@@ -106,7 +109,7 @@ public class RegisterRulesMediumTest {
     assertThat(searchResult.getHits()).hasSize(0);
 
     // db is not updated (same rules) but es must be reindexed
-    tester.get(Platform.class).restart();
+    tester.get(Platform.class).executeStartupTasks();
 
     index = tester.get(RuleIndex.class);
 
@@ -121,8 +124,8 @@ public class RegisterRulesMediumTest {
   public void mark_rule_as_removed() throws Exception {
     verifyTwoRulesInDb();
 
-    includeX2 = false;
-    tester.get(Platform.class).restart();
+    rulesDefinition.includeX2 = false;
+    tester.get(Platform.class).executeStartupTasks();
 
     verifyTwoRulesInDb();
     RuleDto rule = db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2"));
@@ -144,9 +147,9 @@ public class RegisterRulesMediumTest {
     dbSession.clearCache();
 
     // restart, x2 still exists -> deactivate x1
-    includeX1 = false;
-    includeX2 = true;
-    tester.get(Platform.class).restart();
+    rulesDefinition.includeX1 = false;
+    rulesDefinition.includeX2 = true;
+    tester.get(Platform.class).executeStartupTasks();
     dbSession.clearCache();
     assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x1")).getStatus()).isEqualTo(RuleStatus.REMOVED);
     assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2")).getStatus()).isEqualTo(RuleStatus.READY);
@@ -168,9 +171,9 @@ public class RegisterRulesMediumTest {
     dbSession.clearCache();
 
     // restart without x1 and x2 -> keep active rule of x1
-    includeX1 = false;
-    includeX2 = false;
-    tester.get(Platform.class).restart();
+    rulesDefinition.includeX1 = false;
+    rulesDefinition.includeX2 = false;
+    tester.get(Platform.class).executeStartupTasks();
     dbSession.clearCache();
     assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x1")).getStatus()).isEqualTo(RuleStatus.REMOVED);
     assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2")).getStatus()).isEqualTo(RuleStatus.REMOVED);
@@ -186,6 +189,8 @@ public class RegisterRulesMediumTest {
 
   public static class XooRulesDefinition implements RulesDefinition {
 
+    boolean includeX1 = true, includeX2 = true;
+
     @Override
     public void define(Context context) {
       if (includeX1 || includeX2) {