]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 complete IssueActions
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 11 Apr 2013 14:42:24 +0000 (16:42 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 11 Apr 2013 14:42:35 +0000 (16:42 +0200)
sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedViolations.java
sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueActions.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueActionsTest.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java
sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/issue/Issue.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java

index ed5d695ae2e364a79a44e746e3e7e78be94f18ce..b803e6dc327c514d7ba60c31a62d3211e3cfff9d 100644 (file)
@@ -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 (file)
index 0000000..14bbd3b
--- /dev/null
@@ -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;
+  }
+}
index d26352c878fd68223c1fc40cbd085738e50f2526..50e19dc336e9098ae009bfffb38c003028a1fe4e 100644 (file)
@@ -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 (file)
index 0000000..1851b33
--- /dev/null
@@ -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);
+  }
+}
index 92dd3d0b47507718b83a78485cc76ad4d194af8b..7629e715e231af573e2114074be1e6e6cc4414cd 100644 (file)
@@ -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 (file)
index 0000000..20c1011
--- /dev/null
@@ -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");
+    }
+  }
+}
index ad582ebb4c266c58c9b2501da221655eae51ec99..652ed9cd9bd39272a3a9a29e51ecf41fe9ab2262 100644 (file)
@@ -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();
 
index d7f2dbcd92504cb77853ad36a6123d9f057e8874..91dd34b5a02b2257f51b147299f2cf212b3e030b 100644 (file)
@@ -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);
 
 }