diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-09-21 15:45:47 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-09-21 15:46:20 +0200 |
commit | a02d4600fe04f3a70e2d9cf0fa42b9b9ce5378f3 (patch) | |
tree | 650c286663e2915ad22ed6190568513f10186ee4 | |
parent | 01e1b2bd891bad89771cd0b6c83e21c0ba9e83d3 (diff) | |
download | sonarqube-a02d4600fe04f3a70e2d9cf0fa42b9b9ce5378f3.tar.gz sonarqube-a02d4600fe04f3a70e2d9cf0fa42b9b9ce5378f3.zip |
Fix NPE in CacheRuleFinder
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java | 14 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java | 8 |
2 files changed, 16 insertions, 6 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java index f44461f5003..ab1477ec8f6 100644 --- a/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java +++ b/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java @@ -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); diff --git a/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java index 33d76615ad0..bbf0201ed4d 100644 --- a/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java @@ -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; @@ -42,6 +43,13 @@ public class CacheRuleFinderTest extends AbstractDbUnitTestCase { } @Test + public void shouldNotFailIfUnknownRuleId() { + setupData("shared"); + RuleFinder finder = new CacheRuleFinder(getSessionFactory()); + assertThat(finder.findById(33456816), nullValue()); + } + + @Test public void shouldCacheFindByKey() { setupData("shared"); RuleFinder finder = new CacheRuleFinder(getSessionFactory()); |