From a4061d9dc28fd34f376636dfa35d805052430a64 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 23 Apr 2013 14:35:22 +0200 Subject: SONAR-3755 Issue finder optimization --- .../org/sonar/server/issue/ServerIssueFinder.java | 12 +++++++++- .../sonar/server/issue/ServerIssueFinderTest.java | 26 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) (limited to 'sonar-server') diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueFinder.java b/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueFinder.java index f406f4867b7..b5e52df61ff 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueFinder.java @@ -86,7 +86,11 @@ public class ServerIssueFinder implements IssueFinder { ruleIds.add(dto.getRuleId()); } } - return new DefaultResults(issues, getRulesByIssue(issues, ruleIds), getComponentsByIssue(issues, componentIds)); + if (!issues.isEmpty()) { + return new DefaultResults(issues, getRulesByIssue(issues, ruleIds), getComponentsByIssue(issues, componentIds)); + } else { + return new DefaultResults(issues); + } } finally { MyBatis.closeQuietly(sqlSession); } @@ -144,6 +148,12 @@ public class ServerIssueFinder implements IssueFinder { this.componentsByIssue = componentsByIssue; } + DefaultResults(List issues) { + this.issues = issues; + this.rulesByIssue = newHashMap(); + this.componentsByIssue = newHashMap(); + } + @Override public List issues() { return issues; diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueFinderTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueFinderTest.java index d3bf37c085e..7d71aa1e8f2 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueFinderTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueFinderTest.java @@ -38,13 +38,18 @@ import org.sonar.core.resource.ResourceDao; import org.sonar.core.rule.DefaultRuleFinder; import org.sonar.core.user.AuthorizationDao; +import java.util.Collections; import java.util.List; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyCollection; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anySet; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; public class ServerIssueFinderTest { @@ -148,6 +153,21 @@ public class ServerIssueFinderTest { assertThat(results.components()).hasSize(1); } + @Test + public void should_get_empty_rule_and_component_from_result_when_no_issue() { + grantAccessRights(); + IssueQuery issueQuery = mock(IssueQuery.class); + when(issueDao.select(eq(issueQuery), any(SqlSession.class))).thenReturn(Collections.emptyList()); + + IssueFinder.Results results = finder.find(issueQuery, null, UserRole.USER); + assertThat(results.issues()).isEmpty(); + assertThat(results.rules()).isEmpty(); + assertThat(results.components()).isEmpty(); + + verifyZeroInteractions(ruleFinder); + verifyZeroInteractions(resourceDao); + } + private void grantAccessRights() { when(authorizationDao.keepAuthorizedComponentIds(anySet(), anyInt(), anyString(), any(SqlSession.class))) .thenAnswer(new Answer() { -- cgit v1.2.3