Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BatchReportReaderRule.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.batch;
  21. import com.google.common.base.Preconditions;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Objects;
  29. import java.util.Optional;
  30. import javax.annotation.CheckForNull;
  31. import javax.annotation.Nullable;
  32. import org.junit.rules.TestRule;
  33. import org.junit.runner.Description;
  34. import org.junit.runners.model.Statement;
  35. import org.sonar.core.util.CloseableIterator;
  36. import org.sonar.scanner.protocol.output.ScannerReport;
  37. import org.sonar.scanner.protocol.output.ScannerReport.LineSgnificantCode;
  38. public class BatchReportReaderRule implements TestRule, BatchReportReader {
  39. private ScannerReport.Metadata metadata;
  40. private List<String> scannerLogs;
  41. private List<ScannerReport.ActiveRule> activeRules = new ArrayList<>();
  42. private List<ScannerReport.ContextProperty> contextProperties = new ArrayList<>();
  43. private Map<Integer, List<ScannerReport.Measure>> measures = new HashMap<>();
  44. private Map<Integer, ScannerReport.Changesets> changesets = new HashMap<>();
  45. private Map<Integer, ScannerReport.Component> components = new HashMap<>();
  46. private Map<Integer, List<ScannerReport.Issue>> issues = new HashMap<>();
  47. private Map<Integer, List<ScannerReport.ExternalIssue>> externalIssues = new HashMap<>();
  48. private List<ScannerReport.AdHocRule> adHocRules = new ArrayList<>();
  49. private Map<Integer, List<ScannerReport.Duplication>> duplications = new HashMap<>();
  50. private Map<Integer, List<ScannerReport.CpdTextBlock>> duplicationBlocks = new HashMap<>();
  51. private Map<Integer, List<ScannerReport.Symbol>> symbols = new HashMap<>();
  52. private Map<Integer, List<ScannerReport.SyntaxHighlightingRule>> syntaxHighlightings = new HashMap<>();
  53. private Map<Integer, List<ScannerReport.LineCoverage>> coverages = new HashMap<>();
  54. private Map<Integer, List<String>> fileSources = new HashMap<>();
  55. private Map<Integer, List<ScannerReport.LineSgnificantCode>> significantCode = new HashMap<>();
  56. private Map<Integer, ScannerReport.ChangedLines> changedLines = new HashMap<>();
  57. private List<ScannerReport.AnalysisWarning> analysisWarnings = Collections.emptyList();
  58. @Override
  59. public Statement apply(final Statement statement, Description description) {
  60. return new Statement() {
  61. @Override
  62. public void evaluate() throws Throwable {
  63. try {
  64. statement.evaluate();
  65. } finally {
  66. clear();
  67. }
  68. }
  69. };
  70. }
  71. private void clear() {
  72. this.metadata = null;
  73. this.scannerLogs = null;
  74. this.measures.clear();
  75. this.changesets.clear();
  76. this.components.clear();
  77. this.issues.clear();
  78. this.duplications.clear();
  79. this.duplicationBlocks.clear();
  80. this.symbols.clear();
  81. this.syntaxHighlightings.clear();
  82. this.coverages.clear();
  83. this.fileSources.clear();
  84. this.significantCode.clear();
  85. }
  86. @Override
  87. public CloseableIterator<ScannerReport.ContextProperty> readContextProperties() {
  88. return CloseableIterator.from(contextProperties.iterator());
  89. }
  90. public BatchReportReaderRule putContextProperties(List<ScannerReport.ContextProperty> contextProperties) {
  91. this.contextProperties = Objects.requireNonNull(contextProperties);
  92. return this;
  93. }
  94. @Override
  95. public ScannerReport.Metadata readMetadata() {
  96. if (metadata == null) {
  97. throw new IllegalStateException("Metadata is missing");
  98. }
  99. return metadata;
  100. }
  101. public BatchReportReaderRule setMetadata(ScannerReport.Metadata metadata) {
  102. this.metadata = metadata;
  103. return this;
  104. }
  105. @Override
  106. public CloseableIterator<String> readScannerLogs() {
  107. if (scannerLogs == null) {
  108. throw new IllegalStateException("Scanner logs are missing");
  109. }
  110. return CloseableIterator.from(scannerLogs.iterator());
  111. }
  112. public BatchReportReaderRule setScannerLogs(@Nullable List<String> logs) {
  113. this.scannerLogs = logs;
  114. return this;
  115. }
  116. @Override
  117. public CloseableIterator<ScannerReport.ActiveRule> readActiveRules() {
  118. if (activeRules == null) {
  119. throw new IllegalStateException("Active rules are not set");
  120. }
  121. return CloseableIterator.from(activeRules.iterator());
  122. }
  123. public BatchReportReaderRule putActiveRules(List<ScannerReport.ActiveRule> activeRules) {
  124. this.activeRules = activeRules;
  125. return this;
  126. }
  127. @Override
  128. public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
  129. return closeableIterator(this.measures.get(componentRef));
  130. }
  131. public BatchReportReaderRule putMeasures(int componentRef, List<ScannerReport.Measure> measures) {
  132. this.measures.put(componentRef, measures);
  133. return this;
  134. }
  135. @Override
  136. @CheckForNull
  137. public ScannerReport.Changesets readChangesets(int componentRef) {
  138. return changesets.get(componentRef);
  139. }
  140. public BatchReportReaderRule putChangesets(ScannerReport.Changesets changesets) {
  141. this.changesets.put(changesets.getComponentRef(), changesets);
  142. return this;
  143. }
  144. @Override
  145. public ScannerReport.Component readComponent(int componentRef) {
  146. return components.get(componentRef);
  147. }
  148. public BatchReportReaderRule putComponent(ScannerReport.Component component) {
  149. this.components.put(component.getRef(), component);
  150. return this;
  151. }
  152. @Override
  153. public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) {
  154. return closeableIterator(issues.get(componentRef));
  155. }
  156. @Override
  157. public CloseableIterator<ScannerReport.ExternalIssue> readComponentExternalIssues(int componentRef) {
  158. return closeableIterator(externalIssues.get(componentRef));
  159. }
  160. @Override
  161. public CloseableIterator<ScannerReport.AdHocRule> readAdHocRules() {
  162. return closeableIterator(adHocRules);
  163. }
  164. public BatchReportReaderRule putAdHocRules(List<ScannerReport.AdHocRule> adHocRules) {
  165. this.adHocRules = adHocRules;
  166. return this;
  167. }
  168. public BatchReportReaderRule putIssues(int componentRef, List<ScannerReport.Issue> issues) {
  169. this.issues.put(componentRef, issues);
  170. return this;
  171. }
  172. public BatchReportReaderRule putExternalIssues(int componentRef, List<ScannerReport.ExternalIssue> externalIssues) {
  173. this.externalIssues.put(componentRef, externalIssues);
  174. return this;
  175. }
  176. @Override
  177. public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) {
  178. return closeableIterator(this.duplications.get(componentRef));
  179. }
  180. public BatchReportReaderRule putDuplications(int componentRef, ScannerReport.Duplication... duplications) {
  181. this.duplications.put(componentRef, Arrays.asList(duplications));
  182. return this;
  183. }
  184. @Override
  185. public CloseableIterator<ScannerReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
  186. return closeableIterator(this.duplicationBlocks.get(componentRef));
  187. }
  188. public BatchReportReaderRule putDuplicationBlocks(int componentRef, List<ScannerReport.CpdTextBlock> duplicationBlocks) {
  189. this.duplicationBlocks.put(componentRef, duplicationBlocks);
  190. return this;
  191. }
  192. @Override
  193. public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) {
  194. return closeableIterator(this.symbols.get(componentRef));
  195. }
  196. private static <T> CloseableIterator<T> closeableIterator(@Nullable List<T> list) {
  197. return list == null ? CloseableIterator.emptyCloseableIterator() : CloseableIterator.from(list.iterator());
  198. }
  199. public BatchReportReaderRule putSymbols(int componentRef, List<ScannerReport.Symbol> symbols) {
  200. this.symbols.put(componentRef, symbols);
  201. return this;
  202. }
  203. public BatchReportReaderRule putSignificantCode(int fileRef, List<ScannerReport.LineSgnificantCode> significantCode) {
  204. this.significantCode.put(fileRef, significantCode);
  205. return this;
  206. }
  207. @Override
  208. public Optional<CloseableIterator<LineSgnificantCode>> readComponentSignificantCode(int fileRef) {
  209. List<LineSgnificantCode> list = significantCode.get(fileRef);
  210. return list == null ? Optional.empty() : Optional.of(CloseableIterator.from(list.iterator()));
  211. }
  212. public BatchReportReaderRule putChangedLines(int fileRef, ScannerReport.ChangedLines fileChangedLines) {
  213. changedLines.put(fileRef, fileChangedLines);
  214. return this;
  215. }
  216. @Override
  217. public Optional<ScannerReport.ChangedLines> readComponentChangedLines(int fileRef) {
  218. return Optional.ofNullable(changedLines.get(fileRef));
  219. }
  220. @Override
  221. public CloseableIterator<ScannerReport.AnalysisWarning> readAnalysisWarnings() {
  222. return closeableIterator(analysisWarnings);
  223. }
  224. public BatchReportReaderRule setAnalysisWarnings(List<ScannerReport.AnalysisWarning> analysisWarnings) {
  225. this.analysisWarnings = new ArrayList<>(analysisWarnings);
  226. return this;
  227. }
  228. @Override
  229. public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) {
  230. return closeableIterator(this.syntaxHighlightings.get(fileRef));
  231. }
  232. public BatchReportReaderRule putSyntaxHighlighting(int fileRef, List<ScannerReport.SyntaxHighlightingRule> syntaxHighlightings) {
  233. this.syntaxHighlightings.put(fileRef, syntaxHighlightings);
  234. return this;
  235. }
  236. @Override
  237. public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) {
  238. return closeableIterator(this.coverages.get(fileRef));
  239. }
  240. public BatchReportReaderRule putCoverage(int fileRef, List<ScannerReport.LineCoverage> coverages) {
  241. this.coverages.put(fileRef, coverages);
  242. return this;
  243. }
  244. @Override
  245. public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
  246. List<String> lines = fileSources.get(fileRef);
  247. if (lines == null) {
  248. return Optional.empty();
  249. }
  250. return Optional.of(CloseableIterator.from(lines.iterator()));
  251. }
  252. public BatchReportReaderRule putFileSourceLines(int fileRef, @Nullable String... lines) {
  253. Preconditions.checkNotNull(lines);
  254. this.fileSources.put(fileRef, Arrays.asList(lines));
  255. return this;
  256. }
  257. public BatchReportReaderRule putFileSourceLines(int fileRef, List<String> lines) {
  258. this.fileSources.put(fileRef, lines);
  259. return this;
  260. }
  261. }