Skip to content

Commit

Permalink
LITS-20,LITS-21 Dump valid JSON
Browse files Browse the repository at this point in the history
Co-authored-by: Evgeny Mandrikov <[email protected]>
  • Loading branch information
Swalkyn and Godin authored Jun 12, 2023
1 parent 564f911 commit a4a828c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
20 changes: 11 additions & 9 deletions sonar-lits-plugin/src/main/java/com/sonarsource/lits/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.nio.file.Files;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -92,7 +91,7 @@ static void save(List<IssueKey> issues, File dir) {
throw Throwables.propagate(e);
}

Collections.sort(issues, new IssueKeyComparator());
issues.sort(new IssueKeyComparator());

PrintStream out = null;
String prevRuleKey = null;
Expand All @@ -103,17 +102,20 @@ static void save(List<IssueKey> issues, File dir) {
endRule(out);
}
try {
out = new PrintStream(new FileOutputStream(new File(dir, ruleKeyToFileName(issueKey.ruleKey))), /* autoFlush: */ true, StandardCharsets.UTF_8.name());
out = new PrintStream(Files.newOutputStream(dir.toPath().resolve(ruleKeyToFileName(issueKey.ruleKey))), /* autoFlush: */ true, StandardCharsets.UTF_8.name());
} catch (IOException e) {
throw Throwables.propagate(e);
}
out.print("{\n");
out.print("{");
startComponent(out, issueKey.componentKey);
} else if (!issueKey.componentKey.equals(prevComponentKey)) {
endComponent(out);
out.print(",");
startComponent(out, issueKey.componentKey);
} else {
out.print(",");
}
out.print(issueKey.line + ",\n");
out.print("\n" + issueKey.line);
prevComponentKey = issueKey.componentKey;
prevRuleKey = issueKey.ruleKey;
}
Expand All @@ -131,16 +133,16 @@ private static String ruleKeyFromFileName(String fileName) {
}

private static void startComponent(PrintStream out, String componentKey) {
out.print("'" + componentKey + "':[\n");
out.print("\n\"" + componentKey + "\": [");
}

private static void endComponent(PrintStream out) {
out.print("],\n");
out.print("\n]");
}

private static void endRule(PrintStream out) {
endComponent(out);
out.print("}\n");
out.print("\n}\n");
Closeables.closeQuietly(out);
}

Expand Down
22 changes: 11 additions & 11 deletions sonar-lits-plugin/src/test/java/com/sonarsource/lits/DumpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,29 @@ public void save_load() throws Exception {
assertThat(dir.listFiles()).hasSize(3);
String expected = new StringBuilder()
.append("{\n")
.append("'componentKey1':[\n")
.append("1,\n")
.append("],\n")
.append("'componentKey2':[\n")
.append("1,\n")
.append("\"componentKey1\": [\n")
.append("1\n")
.append("],\n")
.append("\"componentKey2\": [\n")
.append("1\n")
.append("]\n")
.append("}\n")
.toString();
assertThat(Files.toString(new File(dir, "repoKey-ruleKey1.json"), StandardCharsets.UTF_8)).isEqualTo(expected);
expected = new StringBuilder()
.append("{\n")
.append("'componentKey1':[\n")
.append("\"componentKey1\": [\n")
.append("1,\n")
.append("2,\n")
.append("],\n")
.append("2\n")
.append("]\n")
.append("}\n")
.toString();
assertThat(Files.toString(new File(dir, "repoKey-ruleKey2.json"), StandardCharsets.UTF_8)).isEqualTo(expected);
expected = new StringBuilder()
.append("{\n")
.append("'componentKey1':[\n")
.append("1,\n")
.append("],\n")
.append("\"componentKey1\": [\n")
.append("1\n")
.append("]\n")
.append("}\n")
.toString();
assertThat(Files.toString(new File(dir, "repoKey-rule-key3.json"), StandardCharsets.UTF_8)).isEqualTo(expected);
Expand Down

0 comments on commit a4a828c

Please sign in to comment.