From: Simon Brandhof Date: Thu, 13 Jun 2013 09:32:42 +0000 (+0200) Subject: Fix null collections in org.sonar.api.issue.IssueQuery X-Git-Tag: 3.7~494 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=849d0e9ef5ff9bd0049af2f9bcc18f2104579da9;p=sonarqube.git Fix null collections in org.sonar.api.issue.IssueQuery --- diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 61246d6c239..1495d7bf359 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -230,32 +230,32 @@ inner join () components on components.project_id=i.component_id - + inner join projects project_component on project_component.id=i.component_id and project_component.enabled=${_true} and project_component.kee in #{component} - + inner join rules r on r.id=i.rule_id and ( r.plugin_name=#{rule.repository} and r.plugin_rule_key=#{rule.rule}) - + and i.kee in #{key} - + and i.severity in #{severity} - + and i.status in #{status} - + and i.resolution in #{resolution} @@ -268,12 +268,12 @@ and i.resolution is null - + and i.reporter in #{reporter} - + and i.assignee in #{assignee} @@ -294,7 +294,7 @@ and i.action_plan_key is null - + and i.action_plan_key in #{action_plan} @@ -306,9 +306,6 @@ and i.issue_creation_date < #{query.createdBefore} - - and i.issue_creation_date < #{query.createdBefore} - order by i.id desc diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java index 5daf278f46a..79e5aace148 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java @@ -27,10 +27,7 @@ import org.sonar.api.web.UserRole; import javax.annotation.CheckForNull; import javax.annotation.Nullable; - -import java.util.Collection; -import java.util.Date; -import java.util.Set; +import java.util.*; /** * @since 3.6 @@ -77,16 +74,16 @@ public class IssueQuery { private final int pageIndex; private IssueQuery(Builder builder) { - this.issueKeys = builder.issueKeys; - this.severities = builder.severities; - this.statuses = builder.statuses; - this.resolutions = builder.resolutions; - this.components = builder.components; - this.componentRoots = builder.componentRoots; - this.rules = builder.rules; - this.actionPlans = builder.actionPlans; - this.reporters = builder.reporters; - this.assignees = builder.assignees; + this.issueKeys = defaultCollection(builder.issueKeys); + this.severities = defaultCollection(builder.severities); + this.statuses = defaultCollection(builder.statuses); + this.resolutions = defaultCollection(builder.resolutions); + this.components = defaultCollection(builder.components); + this.componentRoots = defaultCollection(builder.componentRoots); + this.rules = defaultCollection(builder.rules); + this.actionPlans = defaultCollection(builder.actionPlans); + this.reporters = defaultCollection(builder.reporters); + this.assignees = defaultCollection(builder.assignees); this.assigned = builder.assigned; this.planned = builder.planned; this.resolved = builder.resolved; @@ -224,52 +221,52 @@ public class IssueQuery { private Builder() { } - public Builder issueKeys(Collection l) { + public Builder issueKeys(@Nullable Collection l) { this.issueKeys = l; return this; } - public Builder severities(Collection l) { + public Builder severities(@Nullable Collection l) { this.severities = l; return this; } - public Builder statuses(Collection l) { + public Builder statuses(@Nullable Collection l) { this.statuses = l; return this; } - public Builder resolutions(Collection l) { + public Builder resolutions(@Nullable Collection l) { this.resolutions = l; return this; } - public Builder components(Collection l) { + public Builder components(@Nullable Collection l) { this.components = l; return this; } - public Builder componentRoots(Collection l) { + public Builder componentRoots(@Nullable Collection l) { this.componentRoots = l; return this; } - public Builder rules(Collection rules) { + public Builder rules(@Nullable Collection rules) { this.rules = rules; return this; } - public Builder actionPlans(Collection l) { + public Builder actionPlans(@Nullable Collection l) { this.actionPlans = l; return this; } - public Builder reporters(Collection l) { + public Builder reporters(@Nullable Collection l) { this.reporters = l; return this; } - public Builder assignees(Collection l) { + public Builder assignees(@Nullable Collection l) { this.assignees = l; return this; } @@ -302,12 +299,12 @@ public class IssueQuery { } public Builder createdAfter(@Nullable Date d) { - this.createdAfter = d; + this.createdAfter = (d == null ? null : new Date(d.getTime())); return this; } public Builder createdBefore(@Nullable Date d) { - this.createdBefore = d; + this.createdBefore = (d == null ? null : new Date(d.getTime())); return this; } @@ -367,4 +364,8 @@ public class IssueQuery { Preconditions.checkArgument(pageIndex > 0, "Page index must be greater than 0 (got " + pageIndex + ")"); } } + + private static Collection defaultCollection(@Nullable Collection c) { + return c == null ? Collections.emptyList() : Collections.unmodifiableCollection(c); + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java index 3ee45572a6c..27ee1da2ee8 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java @@ -49,6 +49,8 @@ public class IssueQueryTest { .assigned(true) .createdAfter(new Date()) .createdBefore(new Date()) + .planned(true) + .resolved(true) .sort(IssueQuery.SORT_BY_ASSIGNEE) .pageSize(10) .pageIndex(2) @@ -67,12 +69,66 @@ public class IssueQueryTest { assertThat(query.actionPlans()).containsOnly("AP1", "AP2"); assertThat(query.createdAfter()).isNotNull(); assertThat(query.createdBefore()).isNotNull(); + assertThat(query.planned()).isTrue(); + assertThat(query.resolved()).isTrue(); assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_ASSIGNEE); assertThat(query.pageSize()).isEqualTo(10); assertThat(query.pageIndex()).isEqualTo(2); assertThat(query.requiredRole()).isEqualTo(UserRole.USER); } + @Test + public void collection_params_should_not_be_null_but_empty() throws Exception { + IssueQuery query = IssueQuery.builder() + .issueKeys(null) + .components(null) + .componentRoots(null) + .statuses(null) + .actionPlans(null) + .assignees(null) + .reporters(null) + .resolutions(null) + .rules(null) + .severities(null) + .build(); + assertThat(query.issueKeys()).isEmpty(); + assertThat(query.components()).isEmpty(); + assertThat(query.componentRoots()).isEmpty(); + assertThat(query.statuses()).isEmpty(); + assertThat(query.actionPlans()).isEmpty(); + assertThat(query.assignees()).isEmpty(); + assertThat(query.reporters()).isEmpty(); + assertThat(query.resolutions()).isEmpty(); + assertThat(query.rules()).isEmpty(); + assertThat(query.severities()).isEmpty(); + } + + @Test + public void test_default_query() throws Exception { + IssueQuery query = IssueQuery.builder().build(); + assertThat(query.issueKeys()).isEmpty(); + assertThat(query.components()).isEmpty(); + assertThat(query.componentRoots()).isEmpty(); + assertThat(query.statuses()).isEmpty(); + assertThat(query.actionPlans()).isEmpty(); + assertThat(query.assignees()).isEmpty(); + assertThat(query.reporters()).isEmpty(); + assertThat(query.resolutions()).isEmpty(); + assertThat(query.rules()).isEmpty(); + assertThat(query.severities()).isEmpty(); + assertThat(query.assigned()).isNull(); + assertThat(query.createdAfter()).isNull(); + assertThat(query.createdBefore()).isNull(); + assertThat(query.planned()).isNull(); + assertThat(query.resolved()).isNull(); + assertThat(query.sort()).isNull(); + assertThat(query.pageSize()).isEqualTo(100); + assertThat(query.pageIndex()).isEqualTo(1); + assertThat(query.requiredRole()).isEqualTo(UserRole.USER); + assertThat(query.maxResults()).isEqualTo(IssueQuery.MAX_RESULTS); + + } + @Test public void should_use_max_page_size_if_negative() throws Exception { IssueQuery query = IssueQuery.builder().pageSize(0).build();