]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4832 RE-enable indexation of rules at startup
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 8 Nov 2013 10:25:28 +0000 (11:25 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 8 Nov 2013 17:21:45 +0000 (18:21 +0100)
sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java
sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java

index de7ffa48d7f629a24a491d998beee23fbc36b7bd..e7c917bbdd565bb2236b7acaac53565d64d4762d 100644 (file)
@@ -39,7 +39,7 @@ import java.util.Locale;
  * Fill search index with rules
  * @since 4.1
  */
-public final class RuleRegistry {
+public class RuleRegistry {
 
   private static final String INDEX_RULES = "rules";
   private static final String TYPE_RULE = "rule";
index f90b690862408b2b894c8ceb9dad49ade20bf108..2a7aaa4e9837c7e72d350fad08c8ad1d5297b15c 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.api.utils.TimeProfiler;
 import org.sonar.core.i18n.RuleI18nManager;
 import org.sonar.jpa.session.DatabaseSessionFactory;
 import org.sonar.server.configuration.ProfilesManager;
+import org.sonar.server.rule.RuleRegistry;
 
 import java.util.*;
 
@@ -52,18 +53,20 @@ public final class RegisterRules {
   private final ProfilesManager profilesManager;
   private final List<RuleRepository> repositories;
   private final RuleI18nManager ruleI18nManager;
+  private final RuleRegistry ruleRegistry;
 
   private DatabaseSession session;
 
-  public RegisterRules(DatabaseSessionFactory sessionFactory, RuleRepository[] repos, RuleI18nManager ruleI18nManager, ProfilesManager profilesManager) {
+  public RegisterRules(DatabaseSessionFactory sessionFactory, RuleRepository[] repos, RuleI18nManager ruleI18nManager, ProfilesManager profilesManager, RuleRegistry ruleRegistry) {
     this.sessionFactory = sessionFactory;
     this.profilesManager = profilesManager;
     this.repositories = newArrayList(repos);
     this.ruleI18nManager = ruleI18nManager;
+    this.ruleRegistry = ruleRegistry;
   }
 
-  public RegisterRules(DatabaseSessionFactory sessionFactory, RuleI18nManager ruleI18nManager, ProfilesManager profilesManager) {
-    this(sessionFactory, new RuleRepository[0], ruleI18nManager, profilesManager);
+  public RegisterRules(DatabaseSessionFactory sessionFactory, RuleI18nManager ruleI18nManager, ProfilesManager profilesManager, RuleRegistry ruleRegistry) {
+    this(sessionFactory, new RuleRepository[0], ruleI18nManager, profilesManager, ruleRegistry);
   }
 
   public void start() {
@@ -77,6 +80,8 @@ public final class RegisterRules {
     disableDeprecatedRepositories(existingRules);
 
     session.commit();
+
+    ruleRegistry.bulkRegisterRules();
   }
 
   private List<Rule> findAllRules() {
index a3c309ccdc977e1f80f7a71755a52aa666bf60fc..7254329440972ca9cc6620724bf90e7852dd28e3 100644 (file)
@@ -27,29 +27,39 @@ import org.sonar.api.utils.SonarException;
 import org.sonar.core.i18n.RuleI18nManager;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 import org.sonar.server.configuration.ProfilesManager;
+import org.sonar.server.rule.RuleRegistry;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Locale;
 
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
 import static org.hamcrest.number.OrderingComparisons.greaterThan;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class RegisterRulesTest extends AbstractDbUnitTestCase {
 
   private RegisterRules task;
   private ProfilesManager profilesManager;
+  private RuleRegistry ruleRegistry;
 
   @Before
   public void init() {
     profilesManager = mock(ProfilesManager.class);
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new FakeRepository()}, null, profilesManager);
+    ruleRegistry = mock(RuleRegistry.class);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new FakeRepository()}, null, profilesManager, ruleRegistry);
   }
 
   @Test
@@ -57,6 +67,8 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
     setupData("shared");
     task.start();
 
+    verify(ruleRegistry).bulkRegisterRules();
+
     List<Rule> result = getSession().getResults(Rule.class, "pluginName", "fake");
     assertThat(result.size(), is(2));
 
@@ -255,7 +267,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
 
   @Test
   public void volume_testing() {
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new VolumeRepository()}, null, profilesManager);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new VolumeRepository()}, null, profilesManager, ruleRegistry);
     setupData("shared");
     task.start();
 
@@ -267,7 +279,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
   @Test
   public void should_fail_with_rule_without_name() throws Exception {
     RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class);
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutNameRepository()}, ruleI18nManager, profilesManager);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutNameRepository()}, ruleI18nManager, profilesManager, ruleRegistry);
     setupData("shared");
 
     // the rule has no name, it should fail
@@ -289,7 +301,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
   public void should_fail_with_rule_with_blank_name() throws Exception {
     RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class);
     when(ruleI18nManager.getName(anyString(), anyString(), any(Locale.class))).thenReturn("");
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutNameRepository()}, ruleI18nManager, profilesManager);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutNameRepository()}, ruleI18nManager, profilesManager, ruleRegistry);
     setupData("shared");
 
     // the rule has no name, it should fail
@@ -306,7 +318,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
   public void should_fail_with_rule_without_description() throws Exception {
     RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class);
     when(ruleI18nManager.getName(anyString(), anyString(), any(Locale.class))).thenReturn("Name");
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager, profilesManager);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager, profilesManager, ruleRegistry);
     setupData("shared");
 
     // the rule has no name, it should fail
@@ -327,7 +339,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
   @Test
   public void should_fail_with_rule_without_name_in_bundle() throws Exception {
     RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class);
-    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager, profilesManager);
+    task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager, profilesManager, ruleRegistry);
     setupData("shared");
 
     // the rule has no name, it should fail