From a02d4600fe04f3a70e2d9cf0fa42b9b9ce5378f3 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 21 Sep 2011 15:45:47 +0200 Subject: Fix NPE in CacheRuleFinder --- .../java/org/sonar/core/components/CacheRuleFinder.java | 14 ++++++++------ .../org/sonar/core/components/CacheRuleFinderTest.java | 8 ++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'sonar-core') 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 repository = loadRepository(repositoryKey); + Map repository = loadRepository(repositoryKey); return repository.get(ruleKey); } - private Map loadRepository(String repositoryKey) { - Map repository = rulesByKey.get(repositoryKey); - if (repository==null) { + private Map loadRepository(String repositoryKey) { + Map 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; @@ -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"); -- cgit v1.2.3