From: Jean-Baptiste Lievremont Date: Fri, 13 Dec 2013 15:02:29 +0000 (+0100) Subject: Fix startup when no rule registered X-Git-Tag: 4.2~985 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f9eb8abfdfe567b602f388fc414a4814068ff513;p=sonarqube.git Fix startup when no rule registered --- diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java index 773e2d5025c..cd639f1beb7 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java @@ -170,9 +170,12 @@ public class RuleRegistry { index ++; } profiler.stop(); - profiler.start("Index rules"); - searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs); - profiler.stop(); + + if (! rules.isEmpty()) { + profiler.start("Index rules"); + searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs); + profiler.stop(); + } List indexIds = searchIndex.findDocumentIds(SearchQuery.create().index(INDEX_RULES).type(TYPE_RULE)); indexIds.removeAll(Arrays.asList(ids)); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistryTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistryTest.java index 6689af02771..66ee278bf1a 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistryTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistryTest.java @@ -24,6 +24,7 @@ import com.github.tlrx.elasticsearch.test.EsSetup; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.apache.commons.io.IOUtils; +import org.elasticsearch.common.collect.Lists; import org.elasticsearch.search.SearchHit; import org.junit.After; import org.junit.Before; @@ -149,6 +150,14 @@ public class RuleRegistryTest { registry.findIds(ImmutableMap.of("nameOrKey", "\"'")); } + @Test + public void should_remove_all_rules_when_ro_rule_registered() { + List rules = Lists.newArrayList(); + when(ruleDao.selectNonManual()).thenReturn(rules); + registry.bulkRegisterRules(); + assertThat(registry.findIds(new HashMap())).hasSize(0); + } + @Test public void should_index_all_rules() { long ruleId1 = 3L;