From da8cc3299f6c6e4f999a6dd04f9efdd070798dd7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 3 Jun 2013 06:51:08 +0200 Subject: [PATCH] Fix some quality flaws --- .../batch/index/ComponentDataPersister.java | 5 +++-- .../sonar/core/issue/workflow/IssueWorkflow.java | 6 +++++- .../org/sonar/core/issue/workflow/State.java | 1 - .../core/issue/workflow/IssueWorkflowTest.java | 16 +++++++++++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java index 7d7532b2503..c40701f150e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java @@ -48,12 +48,13 @@ public class ComponentDataPersister implements ScanPersister { String componentKey = componentEntry.getKey(); Snapshot snapshot = componentEntry.getValue(); for (Cache.Entry dataEntry : data.entries(componentKey)) { - if (dataEntry.value() != null) { + Data value = dataEntry.value(); + if (value != null) { SnapshotDataDto dto = new SnapshotDataDto(); dto.setSnapshotId(snapshot.getId()); dto.setResourceId(snapshot.getResourceId()); dto.setDataType(dataEntry.key()); - dto.setData(dataEntry.value().writeString()); + dto.setData(value.writeString()); dao.insert(session, dto); } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java b/sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java index bbd752f5e21..12a8318fadf 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java @@ -160,7 +160,11 @@ public class IssueWorkflow implements BatchComponent, ServerComponent, Startable } public List outTransitions(Issue issue) { - return machine.state(issue.status()).outManualTransitions(issue); + State state = machine.state(issue.status()); + if (state == null) { + throw new IllegalArgumentException("Unknown status: " + issue.status()); + } + return state.outManualTransitions(issue); } public void doAutomaticTransition(DefaultIssue issue, IssueChangeContext issueChangeContext) { diff --git a/sonar-core/src/main/java/org/sonar/core/issue/workflow/State.java b/sonar-core/src/main/java/org/sonar/core/issue/workflow/State.java index 98388f22c53..5660152d5ad 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/workflow/State.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/workflow/State.java @@ -27,7 +27,6 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.issue.Issue; import javax.annotation.CheckForNull; - import java.util.List; import java.util.Set; diff --git a/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java b/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java index 7ee3d8f29b0..eda0b11523a 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java @@ -104,6 +104,20 @@ public class IssueWorkflowTest { assertThat(keys(transitions)).containsOnly("confirm", "resolve", "falsepositive"); } + @Test + public void should_fail_if_unknown_status_when_listing_transitions() throws Exception { + workflow.start(); + + DefaultIssue issue = new DefaultIssue().setStatus("xxx"); + try { + workflow.outTransitions(issue); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Unknown status: xxx"); + } + } + + @Test public void should_do_automatic_transition() throws Exception { workflow.start(); @@ -178,7 +192,7 @@ public class IssueWorkflowTest { @Test - public void should_fail_if_unknown_status() throws Exception { + public void should_fail_if_unknown_status_on_automatic_trans() throws Exception { workflow.start(); DefaultIssue issue = new DefaultIssue() -- 2.39.5