@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);
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;
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");