aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch-protocol
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-batch-protocol')
-rw-r--r--sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelper.java32
-rw-r--r--sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelperTest.java25
2 files changed, 36 insertions, 21 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelper.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelper.java
index 576cb9105a3..14e4e4a3785 100644
--- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelper.java
+++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelper.java
@@ -26,39 +26,47 @@ import org.sonar.batch.protocol.GsonHelper;
import javax.annotation.Nullable;
+import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
import java.util.NoSuchElementException;
-public class PreviousIssueHelper {
+public class PreviousIssueHelper implements Closeable {
private final Gson gson = GsonHelper.create();
+ JsonWriter writer;
- private PreviousIssueHelper() {
+ private PreviousIssueHelper(Writer out) {
+ try {
+ this.writer = new JsonWriter(out);
+ writer.setIndent(" ");
+ writer.beginArray();
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to open writer", e);
+ }
}
- public static PreviousIssueHelper create() {
- return new PreviousIssueHelper();
+ public static PreviousIssueHelper create(Writer out) {
+ return new PreviousIssueHelper(out);
}
public static interface Function<F, T> {
T apply(@Nullable F from);
}
- public <G> void streamIssues(Writer out, Iterable<G> issues, Function<G, PreviousIssue> converter) {
+ public <G> void addIssue(G issue, Function<G, PreviousIssue> converter) {
+ gson.toJson(converter.apply(issue), PreviousIssue.class, writer);
+ }
+
+ @Override
+ public void close() {
try {
- JsonWriter writer = new JsonWriter(out);
- writer.setIndent(" ");
- writer.beginArray();
- for (G issue : issues) {
- gson.toJson(converter.apply(issue), PreviousIssue.class, writer);
- }
writer.endArray();
writer.close();
} catch (IOException e) {
- throw new IllegalStateException("Unable to stream issues", e);
+ throw new IllegalStateException("Unable to close write", e);
}
}
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelperTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelperTest.java
index 8719db821f8..1e5264b4ad2 100644
--- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelperTest.java
+++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/issues/PreviousIssueHelperTest.java
@@ -25,7 +25,6 @@ import org.skyscreamer.jsonassert.JSONAssert;
import java.io.StringReader;
import java.io.StringWriter;
-import java.util.Arrays;
import java.util.Iterator;
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,8 +33,8 @@ public class PreviousIssueHelperTest {
@Test
public void writeIssues() throws JSONException {
- PreviousIssueHelper helper = PreviousIssueHelper.create();
StringWriter out = new StringWriter();
+ PreviousIssueHelper helper = PreviousIssueHelper.create(out);
PreviousIssue issue1 = new PreviousIssue();
issue1.setKey("key1");
@@ -52,12 +51,10 @@ public class PreviousIssueHelperTest {
PreviousIssue issue2 = new PreviousIssue();
issue2.setKey("key2");
- helper.streamIssues(out, Arrays.asList(issue1, issue2), new PreviousIssueHelper.Function<PreviousIssue, PreviousIssue>() {
- @Override
- public PreviousIssue apply(PreviousIssue from) {
- return from;
- }
- });
+ PreviousIssueFunction previousIssueFunction = new PreviousIssueFunction();
+ helper.addIssue(issue1, previousIssueFunction);
+ helper.addIssue(issue2, previousIssueFunction);
+ helper.close();
JSONAssert
.assertEquals(
@@ -65,11 +62,20 @@ public class PreviousIssueHelperTest {
+
"{\"key\": \"key2\"}]",
out.getBuffer().toString(), true);
+
+ }
+
+ private static class PreviousIssueFunction implements PreviousIssueHelper.Function<PreviousIssue, PreviousIssue> {
+ @Override
+ public PreviousIssue apply(PreviousIssue from) {
+ return from;
+ }
}
@Test
public void readIssues() {
- PreviousIssueHelper helper = PreviousIssueHelper.create();
+ StringWriter out = new StringWriter();
+ PreviousIssueHelper helper = PreviousIssueHelper.create(out);
StringReader reader = new StringReader(
"[{\"key\": \"key1\", \"componentPath\": \"path\", \"ruleKey\": \"rulekey\", \"ruleRepo\": \"repokey\", \"line\": 2,\"message\": \"message\", \"severity\": \"severity\", \"resolution\": \"resolution\", \"status\": \"status\", \"checksum\": \"checksum\",\"assigneeLogin\": \"login\", \"assigneeFullname\": \"fullname\"},"
+
@@ -95,6 +101,7 @@ public class PreviousIssueHelperTest {
assertThat(issue1.assigneeFullname()).isEqualTo("fullname");
assertThat(issue2.key()).isEqualTo("key2");
+ helper.close();
}
}