]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 ability to get list of available issue statuses
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 16 May 2013 14:01:12 +0000 (16:01 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 16 May 2013 14:09:12 +0000 (16:09 +0200)
sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java
sonar-core/src/main/java/org/sonar/core/issue/workflow/StateMachine.java
sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java
sonar-core/src/test/java/org/sonar/core/issue/workflow/StateMachineTest.java [new file with mode: 0644]

index dea050cb4928ebab373090d1d0e213f8c5951029..3b2f2943ad6e1a2d3498a34bcebf1af8af7c56c3 100644 (file)
@@ -44,7 +44,10 @@ public class IssueWorkflow implements BatchComponent, ServerComponent, Startable
   @Override
   public void start() {
     machine = StateMachine.builder()
+
+      // order is important for UI
       .states(Issue.STATUS_OPEN, Issue.STATUS_REOPENED, Issue.STATUS_RESOLVED, Issue.STATUS_CLOSED)
+
       .transition(Transition.builder(DefaultTransitions.RESOLVE)
         .from(Issue.STATUS_OPEN).to(Issue.STATUS_RESOLVED)
         .functions(new SetResolution(Issue.RESOLUTION_FIXED))
@@ -131,6 +134,10 @@ public class IssueWorkflow implements BatchComponent, ServerComponent, Startable
     }
   }
 
+  public List<String> statusKeys() {
+    return machine.stateKeys();
+  }
+
   private State stateOf(DefaultIssue issue) {
     State state = machine.state(issue.status());
     if (state==null) {
index 463e462330e995055bbb7f833397f761a3140145..70260dfe758f3326a966566eff5d5159d76b1075 100644 (file)
 package org.sonar.core.issue.workflow;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import com.google.common.collect.*;
 
 import javax.annotation.CheckForNull;
 import java.util.Arrays;
@@ -33,19 +30,27 @@ import java.util.Set;
 
 public class StateMachine {
 
-  private Map<String, State> states = Maps.newHashMap();
+  private final List<String> keys;
+  private final Map<String, State> byKey;
 
   private StateMachine(Builder builder) {
+    this.keys = ImmutableList.copyOf(builder.states);
+    ImmutableMap.Builder<String, State> mapBuilder = ImmutableMap.builder();
     for (String stateKey : builder.states) {
       List<Transition> outTransitions = builder.outTransitions.get(stateKey);
       State state = new State(stateKey, outTransitions.toArray(new Transition[outTransitions.size()]));
-      states.put(stateKey, state);
+      mapBuilder.put(stateKey, state);
     }
+    byKey = mapBuilder.build();
   }
 
   @CheckForNull
   public State state(String stateKey) {
-    return states.get(stateKey);
+    return byKey.get(stateKey);
+  }
+
+  public List<String> stateKeys() {
+    return keys;
   }
 
   public static Builder builder() {
@@ -53,10 +58,13 @@ public class StateMachine {
   }
 
   public static class Builder {
-    private final Set<String> states = Sets.newTreeSet();
+    private final Set<String> states = Sets.newLinkedHashSet();
     // transitions per originating state
     private final ListMultimap<String, Transition> outTransitions = ArrayListMultimap.create();
 
+    private Builder() {
+    }
+
     public Builder states(String... keys) {
       states.addAll(Arrays.asList(keys));
       return this;
index 2d63c8a08b59ea3a3a4c4fe6463094f275d6121c..e4a24c24668f7a3971f1c1f91b52dbdc0d5cff2e 100644 (file)
@@ -53,6 +53,13 @@ public class IssueWorkflowTest {
     workflow.stop();
   }
 
+  @Test
+  public void should_list_statuses() throws Exception {
+    workflow.start();
+    // order is important for UI
+    assertThat(workflow.statusKeys()).containsSequence(Issue.STATUS_OPEN, Issue.STATUS_REOPENED, Issue.STATUS_RESOLVED, Issue.STATUS_CLOSED);
+  }
+
   @Test
   public void should_list_out_manual_transitions() throws Exception {
     workflow.start();
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/workflow/StateMachineTest.java b/sonar-core/src/test/java/org/sonar/core/issue/workflow/StateMachineTest.java
new file mode 100644 (file)
index 0000000..e28f38c
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.issue.workflow;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class StateMachineTest {
+  @Test
+  public void keep_order_of_state_keys() throws Exception {
+    StateMachine machine = StateMachine.builder().states("OPEN", "RESOLVED", "CLOSED").build();
+
+    assertThat(machine.stateKeys()).containsSequence("OPEN", "RESOLVED", "CLOSED");
+  }
+
+  @Test
+  public void stateKey() throws Exception {
+    StateMachine machine = StateMachine.builder()
+      .states("OPEN", "RESOLVED", "CLOSED")
+      .transition(Transition.builder("resolve").from("OPEN").to("RESOLVED").build())
+      .build();
+
+    assertThat(machine.state("OPEN")).isNotNull();
+    assertThat(machine.state("OPEN").transition("resolve")).isNotNull();
+  }
+}