You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalysisResult.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.scanner.mediumtest;
  21. import com.google.common.collect.Iterators;
  22. import com.google.common.collect.Lists;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import javax.annotation.CheckForNull;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.sonar.api.batch.fs.InputComponent;
  33. import org.sonar.api.batch.fs.InputFile;
  34. import org.sonar.api.batch.fs.TextPointer;
  35. import org.sonar.api.batch.fs.TextRange;
  36. import org.sonar.api.batch.fs.internal.DefaultInputComponent;
  37. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  38. import org.sonar.api.batch.sensor.highlighting.TypeOfText;
  39. import org.sonar.api.scanner.fs.InputProject;
  40. import org.sonar.core.util.CloseableIterator;
  41. import org.sonar.scanner.issue.IssueCache;
  42. import org.sonar.scanner.issue.tracking.TrackedIssue;
  43. import org.sonar.scanner.protocol.output.ScannerReport;
  44. import org.sonar.scanner.protocol.output.ScannerReport.Component;
  45. import org.sonar.scanner.protocol.output.ScannerReport.Symbol;
  46. import org.sonar.scanner.protocol.output.ScannerReportReader;
  47. import org.sonar.scanner.report.ReportPublisher;
  48. import org.sonar.scanner.report.ScannerReportUtils;
  49. import org.sonar.scanner.scan.ProjectScanContainer;
  50. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  51. public class AnalysisResult implements AnalysisObserver {
  52. private static final Logger LOG = LoggerFactory.getLogger(AnalysisResult.class);
  53. private List<TrackedIssue> issues = new ArrayList<>();
  54. private Map<String, InputFile> inputFilesByKeys = new HashMap<>();
  55. private InputProject project;
  56. private ScannerReportReader reader;
  57. @Override
  58. public void analysisCompleted(ProjectScanContainer container) {
  59. LOG.info("Store analysis results in memory for later assertions in medium test");
  60. for (TrackedIssue issue : container.getComponentByType(IssueCache.class).all()) {
  61. issues.add(issue);
  62. }
  63. ReportPublisher reportPublisher = container.getComponentByType(ReportPublisher.class);
  64. reader = new ScannerReportReader(reportPublisher.getReportDir().toFile());
  65. project = container.getComponentByType(InputProject.class);
  66. storeFs(container);
  67. }
  68. public ScannerReportReader getReportReader() {
  69. return reader;
  70. }
  71. private void storeFs(ProjectScanContainer container) {
  72. InputComponentStore inputFileCache = container.getComponentByType(InputComponentStore.class);
  73. for (InputFile inputPath : inputFileCache.inputFiles()) {
  74. inputFilesByKeys.put(((DefaultInputFile) inputPath).getProjectRelativePath(), inputPath);
  75. }
  76. }
  77. public List<TrackedIssue> trackedIssues() {
  78. return issues;
  79. }
  80. public Component getReportComponent(InputComponent inputComponent) {
  81. return getReportReader().readComponent(((DefaultInputComponent) inputComponent).scannerId());
  82. }
  83. public Component getReportComponent(int scannerId) {
  84. return getReportReader().readComponent(scannerId);
  85. }
  86. public List<ScannerReport.Issue> issuesFor(InputComponent inputComponent) {
  87. return issuesFor(((DefaultInputComponent) inputComponent).scannerId());
  88. }
  89. public List<ScannerReport.ExternalIssue> externalIssuesFor(InputComponent inputComponent) {
  90. return externalIssuesFor(((DefaultInputComponent) inputComponent).scannerId());
  91. }
  92. public List<ScannerReport.Issue> issuesFor(Component reportComponent) {
  93. int ref = reportComponent.getRef();
  94. return issuesFor(ref);
  95. }
  96. private List<ScannerReport.Issue> issuesFor(int ref) {
  97. List<ScannerReport.Issue> result = Lists.newArrayList();
  98. try (CloseableIterator<ScannerReport.Issue> it = reader.readComponentIssues(ref)) {
  99. while (it.hasNext()) {
  100. result.add(it.next());
  101. }
  102. }
  103. return result;
  104. }
  105. private List<ScannerReport.ExternalIssue> externalIssuesFor(int ref) {
  106. List<ScannerReport.ExternalIssue> result = Lists.newArrayList();
  107. try (CloseableIterator<ScannerReport.ExternalIssue> it = reader.readComponentExternalIssues(ref)) {
  108. while (it.hasNext()) {
  109. result.add(it.next());
  110. }
  111. }
  112. return result;
  113. }
  114. public InputProject project() {
  115. return project;
  116. }
  117. public Collection<InputFile> inputFiles() {
  118. return inputFilesByKeys.values();
  119. }
  120. @CheckForNull
  121. public InputFile inputFile(String relativePath) {
  122. return inputFilesByKeys.get(relativePath);
  123. }
  124. public Map<String, List<ScannerReport.Measure>> allMeasures() {
  125. Map<String, List<ScannerReport.Measure>> result = new HashMap<>();
  126. List<ScannerReport.Measure> projectMeasures = new ArrayList<>();
  127. try (CloseableIterator<ScannerReport.Measure> it = reader.readComponentMeasures(((DefaultInputComponent) project).scannerId())) {
  128. Iterators.addAll(projectMeasures, it);
  129. }
  130. result.put(project.key(), projectMeasures);
  131. for (InputFile inputFile : inputFilesByKeys.values()) {
  132. List<ScannerReport.Measure> measures = new ArrayList<>();
  133. try (CloseableIterator<ScannerReport.Measure> it = reader.readComponentMeasures(((DefaultInputComponent) inputFile).scannerId())) {
  134. Iterators.addAll(measures, it);
  135. }
  136. result.put(inputFile.key(), measures);
  137. }
  138. return result;
  139. }
  140. /**
  141. * Get highlighting types at a given position in an inputfile
  142. *
  143. * @param lineOffset 0-based offset in file
  144. */
  145. public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) {
  146. int ref = ((DefaultInputComponent) file).scannerId();
  147. if (!reader.hasSyntaxHighlighting(ref)) {
  148. return Collections.emptyList();
  149. }
  150. TextPointer pointer = file.newPointer(line, lineOffset);
  151. List<TypeOfText> result = new ArrayList<>();
  152. try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) {
  153. while (it.hasNext()) {
  154. ScannerReport.SyntaxHighlightingRule rule = it.next();
  155. TextRange ruleRange = toRange(file, rule.getRange());
  156. if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) {
  157. result.add(ScannerReportUtils.toBatchType(rule.getType()));
  158. }
  159. }
  160. } catch (Exception e) {
  161. throw new IllegalStateException("Can't read syntax highlighting for " + file, e);
  162. }
  163. return result;
  164. }
  165. private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) {
  166. return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset()));
  167. }
  168. /**
  169. * Get list of all start positions of a symbol in an inputfile
  170. *
  171. * @param symbolStartLine 0-based start offset for the symbol in file
  172. * @param symbolStartLineOffset 0-based end offset for the symbol in file
  173. */
  174. @CheckForNull
  175. public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) {
  176. int ref = ((DefaultInputComponent) file).scannerId();
  177. try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) {
  178. while (symbols.hasNext()) {
  179. Symbol symbol = symbols.next();
  180. if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) {
  181. return symbol.getReferenceList();
  182. }
  183. }
  184. }
  185. return Collections.emptyList();
  186. }
  187. public List<ScannerReport.Duplication> duplicationsFor(InputFile file) {
  188. List<ScannerReport.Duplication> result = new ArrayList<>();
  189. int ref = ((DefaultInputComponent) file).scannerId();
  190. try (CloseableIterator<ScannerReport.Duplication> it = getReportReader().readComponentDuplications(ref)) {
  191. while (it.hasNext()) {
  192. result.add(it.next());
  193. }
  194. } catch (Exception e) {
  195. throw new IllegalStateException(e);
  196. }
  197. return result;
  198. }
  199. public List<ScannerReport.CpdTextBlock> duplicationBlocksFor(InputFile file) {
  200. List<ScannerReport.CpdTextBlock> result = new ArrayList<>();
  201. int ref = ((DefaultInputComponent) file).scannerId();
  202. try (CloseableIterator<ScannerReport.CpdTextBlock> it = getReportReader().readCpdTextBlocks(ref)) {
  203. while (it.hasNext()) {
  204. result.add(it.next());
  205. }
  206. } catch (Exception e) {
  207. throw new IllegalStateException(e);
  208. }
  209. return result;
  210. }
  211. @CheckForNull
  212. public ScannerReport.LineCoverage coverageFor(InputFile file, int line) {
  213. int ref = ((DefaultInputComponent) file).scannerId();
  214. try (CloseableIterator<ScannerReport.LineCoverage> it = getReportReader().readComponentCoverage(ref)) {
  215. while (it.hasNext()) {
  216. ScannerReport.LineCoverage coverage = it.next();
  217. if (coverage.getLine() == line) {
  218. return coverage;
  219. }
  220. }
  221. } catch (Exception e) {
  222. throw new IllegalStateException(e);
  223. }
  224. return null;
  225. }
  226. public List<ScannerReport.AdHocRule> adHocRules() {
  227. List<ScannerReport.AdHocRule> result = new ArrayList<>();
  228. try (CloseableIterator<ScannerReport.AdHocRule> it = getReportReader().readAdHocRules()) {
  229. while (it.hasNext()) {
  230. result.add(it.next());
  231. }
  232. } catch (Exception e) {
  233. throw new IllegalStateException(e);
  234. }
  235. return result;
  236. }
  237. }