]> source.dussan.org Git - sonarqube.git/commitdiff
Fix NPE in CacheRuleFinder
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 21 Sep 2011 13:45:47 +0000 (15:45 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 21 Sep 2011 13:46:20 +0000 (15:46 +0200)
sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java
sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java

index f44461f5003c09341bee77c8009d5755ed39a5b2..ab1477ec8f6c5098e4c037b32540b74866bbac0a 100644 (file)
@@ -40,22 +40,24 @@ public final class CacheRuleFinder extends DefaultRuleFinder {
   @Override
   public Rule findById(int ruleId) {
     Rule rule = rulesById.get(ruleId);
-    if (rule==null) {
+    if (rule == null) {
       rule = doFindById(ruleId);
-      loadRepository(rule.getRepositoryKey());
+      if (rule != null) {
+        loadRepository(rule.getRepositoryKey());
+      }
     }
     return rule;
   }
 
   @Override
   public Rule findByKey(String repositoryKey, String ruleKey) {
-    Map<String,Rule> repository = loadRepository(repositoryKey);
+    Map<String, Rule> repository = loadRepository(repositoryKey);
     return repository.get(ruleKey);
   }
 
-  private Map<String,Rule> loadRepository(String repositoryKey) {
-    Map<String,Rule> repository = rulesByKey.get(repositoryKey);
-    if (repository==null) {
+  private Map<String, Rule> loadRepository(String repositoryKey) {
+    Map<String, Rule> repository = rulesByKey.get(repositoryKey);
+    if (repository == null) {
       repository = Maps.newHashMap();
       rulesByKey.put(repositoryKey, repository);
 
index 33d76615ad078829fd1984fc0ab14c307d27025f..bbf0201ed4dd2f61c60a992e11e943205d2c1e1a 100644 (file)
@@ -24,6 +24,7 @@ import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.notNullValue;
 import static org.junit.Assert.assertThat;
@@ -41,6 +42,13 @@ public class CacheRuleFinderTest extends AbstractDbUnitTestCase {
     assertThat(finder.findById(3), notNullValue());
   }
 
+  @Test
+  public void shouldNotFailIfUnknownRuleId() {
+    setupData("shared");
+    RuleFinder finder = new CacheRuleFinder(getSessionFactory());
+    assertThat(finder.findById(33456816), nullValue());
+  }
+
   @Test
   public void shouldCacheFindByKey() {
     setupData("shared");