diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-04-11 16:42:24 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-04-11 16:42:35 +0200 |
commit | 7d243175adfa986d8f3b64adc0eba1e289101448 (patch) | |
tree | f7881237b1fd9663658ae7d1f0fa9b4890484eaf | |
parent | fa79db824d284f92ff2cb65c9dc7d3029f3a36fa (diff) | |
download | sonarqube-7d243175adfa986d8f3b64adc0eba1e289101448.tar.gz sonarqube-7d243175adfa986d8f3b64adc0eba1e289101448.zip |
SONAR-3755 complete IssueActions
8 files changed, 247 insertions, 21 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedViolations.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedViolations.java index ed5d695ae2e..b803e6dc327 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedViolations.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedViolations.java @@ -50,11 +50,7 @@ public class DeprecatedViolations implements BatchComponent { Issue toIssue(Violation violation) { DefaultIssue issue = new DefaultIssue(); issue.setComponentKey(violation.getResource().getEffectiveKey()); - if (violation.getPermanentId() != null) { - issue.setKey(violation.getPermanentId().toString()); - } else { - issue.setKey(UUID.randomUUID().toString()); - } + issue.setKey(UUID.randomUUID().toString()); issue.setRuleRepositoryKey(violation.getRule().getRepositoryKey()); issue.setRuleKey(violation.getRule().getKey()); issue.setCost(violation.getCost()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueActions.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueActions.java new file mode 100644 index 00000000000..14bbd3b1ce5 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueActions.java @@ -0,0 +1,84 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.issue; + +import org.sonar.api.issue.Issue; +import org.sonar.api.issue.IssueActions; +import org.sonar.core.issue.DefaultIssue; + +import javax.annotation.Nullable; + +public class ScanIssueActions implements IssueActions { + + @Override + public Issue comment(Issue issue, String userLogin, String comment) { + throw new UnsupportedOperationException("TODO"); + } + + @Override + public Issue setSeverity(Issue issue, String severity) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setSeverity(severity); + return impl; + } + + @Override + public Issue setManualSeverity(Issue issue, String severity) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setSeverity(severity); + impl.setManualSeverity(true); + return impl; + } + + @Override + public Issue setMessage(Issue issue, String message) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setMessage(message); + return impl; + } + + @Override + public Issue setCost(Issue issue, @Nullable Double cost) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setCost(cost); + return impl; + } + + @Override + public Issue setResolution(Issue issue, String resolution) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setResolution(resolution); + return impl; + } + + @Override + public Issue assign(Issue issue, String userLogin) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setUserLogin(userLogin); + return impl; + } + + @Override + public Issue setAttribute(Issue issue, String key, @Nullable String value) { + DefaultIssue impl = (DefaultIssue) issue; + impl.setAttribute(key, value); + return impl; + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index d26352c878f..50e19dc336e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -35,6 +35,7 @@ import org.sonar.batch.index.*; import org.sonar.batch.issue.DeprecatedViolations; import org.sonar.batch.issue.IssueCache; import org.sonar.batch.issue.IssuePersister; +import org.sonar.batch.issue.ScanIssueActions; import org.sonar.batch.phases.GraphPersister; import org.sonar.batch.scan.maven.FakeMavenPluginExecutor; import org.sonar.batch.scan.maven.MavenPluginExecutor; @@ -79,6 +80,7 @@ public class ProjectScanContainer extends ComponentContainer { LastSnapshots.class, SnapshotCache.class, + ScanIssueActions.class, DeprecatedViolations.class, IssueCache.class, IssuePersister.class, diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueActionsTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueActionsTest.java new file mode 100644 index 00000000000..1851b33e235 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueActionsTest.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.issue; + +import org.junit.Test; +import org.sonar.api.issue.Issue; +import org.sonar.core.issue.DefaultIssue; + +import static org.fest.assertions.Assertions.assertThat; + +public class ScanIssueActionsTest { + + ScanIssueActions actions = new ScanIssueActions(); + + @Test + public void should_set_severity() throws Exception { + Issue issue = new DefaultIssue(); + issue = actions.setSeverity(issue, Issue.SEVERITY_INFO); + assertThat(issue.severity()).isEqualTo(Issue.SEVERITY_INFO); + } + + @Test + public void should_set_cost() throws Exception { + Issue issue = new DefaultIssue(); + issue = actions.setCost(issue, 123.0); + assertThat(issue.cost()).isEqualTo(123.0); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java index 92dd3d0b475..7629e715e23 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java @@ -32,12 +32,15 @@ import java.util.Set; public class DefaultIssue implements Issue { private static final Set<String> SEVERITIES = ImmutableSet.of(SEVERITY_BLOCKER, SEVERITY_CRITICAL, SEVERITY_MAJOR, SEVERITY_MINOR, SEVERITY_INFO); + private static final Set<String> RESOLUTIONS = ImmutableSet.of(RESOLUTION_FALSE_POSITIVE, RESOLUTION_FIXED); + private static final Set<String> STATUSES = ImmutableSet.of(STATUS_OPEN, STATUS_CLOSED, STATUS_REOPENED, STATUS_RESOLVED); private String key; private String componentKey; private String ruleKey; private String ruleRepositoryKey; private String severity; + private boolean manualSeverity = false; private String title; private String message; private Integer line; @@ -67,8 +70,8 @@ public class DefaultIssue implements Issue { return componentKey; } - public DefaultIssue setComponentKey(String componentKey) { - this.componentKey = componentKey; + public DefaultIssue setComponentKey(String s) { + this.componentKey = s; return this; } @@ -100,6 +103,15 @@ public class DefaultIssue implements Issue { return this; } + public boolean isManualSeverity() { + return manualSeverity; + } + + public DefaultIssue setManualSeverity(boolean b) { + this.manualSeverity = b; + return this; + } + public String title() { return title; } @@ -142,8 +154,9 @@ public class DefaultIssue implements Issue { return status; } - public DefaultIssue setStatus(@Nullable String status) { - this.status = status; + public DefaultIssue setStatus(@Nullable String s) { + Preconditions.checkArgument(s == null || STATUSES.contains(s), "Not a valid status: " + s); + this.status = s; return this; } @@ -151,8 +164,9 @@ public class DefaultIssue implements Issue { return resolution; } - public DefaultIssue setResolution(@Nullable String resolution) { - this.resolution = resolution; + public DefaultIssue setResolution(@Nullable String s) { + Preconditions.checkArgument(s == null || RESOLUTIONS.contains(s), "Not a valid resolution: " + s); + this.resolution = s; return this; } @@ -232,11 +246,15 @@ public class DefaultIssue implements Issue { return attributes == null ? null : attributes.get(key); } - public DefaultIssue setAttribute(String key, String value) { + public DefaultIssue setAttribute(String key, @Nullable String value) { if (attributes == null) { attributes = Maps.newHashMap(); } - attributes.put(key, value); + if (value == null) { + attributes.remove(key); + } else { + attributes.put(key, value); + } return this; } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java new file mode 100644 index 00000000000..20c1011f85f --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java @@ -0,0 +1,71 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.issue; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; + +public class DefaultIssueTest { + + DefaultIssue issue = new DefaultIssue(); + + @Test + public void test_attributes() throws Exception { + assertThat(issue.attribute("foo")).isNull(); + issue.setAttribute("foo", "bar"); + assertThat(issue.attribute("foo")).isEqualTo("bar"); + issue.setAttribute("foo", "newbar"); + assertThat(issue.attribute("foo")).isEqualTo("newbar"); + issue.setAttribute("foo", null); + assertThat(issue.attribute("foo")).isNull(); + } + + @Test + public void should_fail_on_bad_status() { + try { + issue.setStatus("FOO"); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Not a valid status: FOO"); + } + } + + @Test + public void should_fail_on_bad_resolution() { + try { + issue.setResolution("FOO"); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Not a valid resolution: FOO"); + } + } + + @Test + public void should_fail_on_bad_severity() { + try { + issue.setSeverity("FOO"); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Not a valid severity: FOO"); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java index ad582ebb4c2..652ed9cd9bd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java @@ -30,8 +30,10 @@ public interface Issue { String STATUS_REOPENED = "REOPENED"; String STATUS_RESOLVED = "RESOLVED"; String STATUS_CLOSED = "CLOSED"; + String RESOLUTION_FALSE_POSITIVE = "FALSE-POSITIVE"; String RESOLUTION_FIXED = "FIXED"; + String SEVERITY_INFO = "INFO"; String SEVERITY_MINOR = "MINOR"; String SEVERITY_MAJOR = "MAJOR"; @@ -39,7 +41,7 @@ public interface Issue { String SEVERITY_BLOCKER = "BLOCKER"; /** - * Unique key + * Unique generated key */ String key(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java index d7f2dbcd925..91dd34b5a02 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java @@ -26,12 +26,20 @@ import javax.annotation.Nullable; public interface IssueActions extends BatchComponent, ServerComponent { - IssueActions comment(Issue issue, String userLogin, String comment); - IssueActions setSeverity(Issue issue, String severity); - IssueActions setMessage(Issue issue, String message); - IssueActions setCost(Issue issue, @Nullable Double cost); - IssueActions setResolution(Issue issue, String resolution); - IssueActions assign(Issue issue, String userLogin); - IssueActions setAttribute(Issue issue, String key, @Nullable String value); + Issue comment(Issue issue, String userLogin, String comment); + + Issue setSeverity(Issue issue, String severity); + + Issue setManualSeverity(Issue issue, String severity); + + Issue setMessage(Issue issue, String message); + + Issue setCost(Issue issue, @Nullable Double cost); + + Issue setResolution(Issue issue, String resolution); + + Issue assign(Issue issue, String userLogin); + + Issue setAttribute(Issue issue, String key, @Nullable String value); } |