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.

BatchReportReaderImplTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.collect.ImmutableList;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.impl.utils.JUnitTempFolder;
  29. import org.sonar.core.util.CloseableIterator;
  30. import org.sonar.scanner.protocol.output.ScannerReport;
  31. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  32. import static com.google.common.collect.ImmutableList.of;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  35. public class BatchReportReaderImplTest {
  36. private static final int COMPONENT_REF = 1;
  37. private static final ScannerReport.Changesets CHANGESETS = ScannerReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
  38. private static final ScannerReport.Measure MEASURE = ScannerReport.Measure.newBuilder().build();
  39. private static final ScannerReport.Component COMPONENT = ScannerReport.Component.newBuilder().setRef(COMPONENT_REF).build();
  40. private static final ScannerReport.Issue ISSUE = ScannerReport.Issue.newBuilder().build();
  41. private static final ScannerReport.Duplication DUPLICATION = ScannerReport.Duplication.newBuilder().build();
  42. private static final ScannerReport.CpdTextBlock DUPLICATION_BLOCK = ScannerReport.CpdTextBlock.newBuilder().build();
  43. private static final ScannerReport.Symbol SYMBOL = ScannerReport.Symbol.newBuilder().build();
  44. private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_1 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
  45. private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_2 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
  46. private static final ScannerReport.LineCoverage COVERAGE_1 = ScannerReport.LineCoverage.newBuilder().build();
  47. private static final ScannerReport.LineCoverage COVERAGE_2 = ScannerReport.LineCoverage.newBuilder().build();
  48. @Rule
  49. public JUnitTempFolder tempFolder = new JUnitTempFolder();
  50. private ScannerReportWriter writer;
  51. private BatchReportReaderImpl underTest;
  52. @Before
  53. public void setUp() {
  54. BatchReportDirectoryHolder holder = new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
  55. underTest = new BatchReportReaderImpl(holder);
  56. writer = new ScannerReportWriter(holder.getDirectory());
  57. }
  58. @Test
  59. public void readMetadata_throws_ISE_if_no_metadata() {
  60. assertThatThrownBy(() -> underTest.readMetadata())
  61. .isInstanceOf(IllegalStateException.class);
  62. }
  63. @Test
  64. public void readMetadata_result_is_cached() {
  65. ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().build();
  66. writer.writeMetadata(metadata);
  67. ScannerReport.Metadata res = underTest.readMetadata();
  68. assertThat(res).isEqualTo(metadata);
  69. assertThat(underTest.readMetadata()).isSameAs(res);
  70. }
  71. @Test
  72. public void readScannerLogs() throws IOException {
  73. File scannerLogFile = writer.getFileStructure().analysisLog();
  74. FileUtils.write(scannerLogFile, "log1\nlog2");
  75. CloseableIterator<String> logs = underTest.readScannerLogs();
  76. assertThat(logs).toIterable().containsExactly("log1", "log2");
  77. }
  78. @Test
  79. public void readScannerLogs_no_logs() {
  80. CloseableIterator<String> logs = underTest.readScannerLogs();
  81. assertThat(logs.hasNext()).isFalse();
  82. }
  83. @Test
  84. public void readComponentMeasures_returns_empty_list_if_there_is_no_measure() {
  85. assertThat(underTest.readComponentMeasures(COMPONENT_REF)).isExhausted();
  86. }
  87. @Test
  88. public void verify_readComponentMeasures_returns_measures() {
  89. writer.appendComponentMeasure(COMPONENT_REF, MEASURE);
  90. try (CloseableIterator<ScannerReport.Measure> measures = underTest.readComponentMeasures(COMPONENT_REF)) {
  91. assertThat(measures.next()).isEqualTo(MEASURE);
  92. assertThat(measures.hasNext()).isFalse();
  93. }
  94. }
  95. @Test
  96. public void readComponentMeasures_is_not_cached() {
  97. writer.appendComponentMeasure(COMPONENT_REF, MEASURE);
  98. assertThat(underTest.readComponentMeasures(COMPONENT_REF)).isNotSameAs(underTest.readComponentMeasures(COMPONENT_REF));
  99. }
  100. @Test
  101. public void readChangesets_returns_null_if_no_changeset() {
  102. assertThat(underTest.readChangesets(COMPONENT_REF)).isNull();
  103. }
  104. @Test
  105. public void verify_readChangesets_returns_changesets() {
  106. writer.writeComponentChangesets(CHANGESETS);
  107. ScannerReport.Changesets res = underTest.readChangesets(COMPONENT_REF);
  108. assertThat(res).isEqualTo(CHANGESETS);
  109. }
  110. @Test
  111. public void readChangesets_is_not_cached() {
  112. writer.writeComponentChangesets(CHANGESETS);
  113. assertThat(underTest.readChangesets(COMPONENT_REF)).isNotSameAs(underTest.readChangesets(COMPONENT_REF));
  114. }
  115. @Test
  116. public void readComponent_throws_ISE_if_file_does_not_exist() {
  117. assertThatThrownBy(() -> underTest.readComponent(COMPONENT_REF))
  118. .isInstanceOf(IllegalStateException.class);
  119. }
  120. @Test
  121. public void verify_readComponent_returns_Component() {
  122. writer.writeComponent(COMPONENT);
  123. assertThat(underTest.readComponent(COMPONENT_REF)).isEqualTo(COMPONENT);
  124. }
  125. @Test
  126. public void readComponent_is_not_cached() {
  127. writer.writeComponent(COMPONENT);
  128. assertThat(underTest.readComponent(COMPONENT_REF)).isNotSameAs(underTest.readComponent(COMPONENT_REF));
  129. }
  130. @Test
  131. public void readComponentIssues_returns_empty_list_if_file_does_not_exist() {
  132. assertThat(underTest.readComponentIssues(COMPONENT_REF)).isExhausted();
  133. }
  134. @Test
  135. public void verify_readComponentIssues_returns_Issues() {
  136. writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
  137. try (CloseableIterator<ScannerReport.Issue> res = underTest.readComponentIssues(COMPONENT_REF)) {
  138. assertThat(res.next()).isEqualTo(ISSUE);
  139. assertThat(res.hasNext()).isFalse();
  140. }
  141. }
  142. @Test
  143. public void readComponentIssues_it_not_cached() {
  144. writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
  145. assertThat(underTest.readComponentIssues(COMPONENT_REF)).isNotSameAs(underTest.readComponentIssues(COMPONENT_REF));
  146. }
  147. @Test
  148. public void readComponentDuplications_returns_empty_list_if_file_does_not_exist() {
  149. assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isExhausted();
  150. }
  151. @Test
  152. public void verify_readComponentDuplications_returns_Issues() {
  153. writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
  154. try (CloseableIterator<ScannerReport.Duplication> res = underTest.readComponentDuplications(COMPONENT_REF)) {
  155. assertThat(res.next()).isEqualTo(DUPLICATION);
  156. assertThat(res.hasNext()).isFalse();
  157. }
  158. }
  159. @Test
  160. public void readComponentDuplications_it_not_cached() {
  161. writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
  162. assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isNotSameAs(underTest.readComponentDuplications(COMPONENT_REF));
  163. }
  164. @Test
  165. public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() {
  166. assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isExhausted();
  167. }
  168. @Test
  169. public void verify_readComponentDuplicationBlocks_returns_Issues() {
  170. writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
  171. try (CloseableIterator<ScannerReport.CpdTextBlock> res = underTest.readCpdTextBlocks(COMPONENT_REF)) {
  172. assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK);
  173. assertThat(res.hasNext()).isFalse();
  174. }
  175. }
  176. @Test
  177. public void readComponentDuplicationBlocks_is_not_cached() {
  178. writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
  179. assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isNotSameAs(underTest.readCpdTextBlocks(COMPONENT_REF));
  180. }
  181. @Test
  182. public void readComponentSymbols_returns_empty_list_if_file_does_not_exist() {
  183. assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isExhausted();
  184. }
  185. @Test
  186. public void verify_readComponentSymbols_returns_Issues() {
  187. writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
  188. try (CloseableIterator<ScannerReport.Symbol> res = underTest.readComponentSymbols(COMPONENT_REF)) {
  189. assertThat(res.next()).isEqualTo(SYMBOL);
  190. assertThat(res.hasNext()).isFalse();
  191. }
  192. }
  193. @Test
  194. public void readComponentSymbols_it_not_cached() {
  195. writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
  196. assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isNotSameAs(underTest.readComponentSymbols(COMPONENT_REF));
  197. }
  198. @Test
  199. public void readComponentSyntaxHighlighting_returns_empty_CloseableIterator_when_file_does_not_exist() {
  200. assertThat(underTest.readComponentSyntaxHighlighting(COMPONENT_REF)).isExhausted();
  201. }
  202. @Test
  203. public void verify_readComponentSyntaxHighlighting() {
  204. writer.writeComponentSyntaxHighlighting(COMPONENT_REF, of(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2));
  205. CloseableIterator<ScannerReport.SyntaxHighlightingRule> res = underTest.readComponentSyntaxHighlighting(COMPONENT_REF);
  206. assertThat(res).toIterable().containsExactly(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2);
  207. res.close();
  208. }
  209. @Test
  210. public void readComponentCoverage_returns_empty_CloseableIterator_when_file_does_not_exist() {
  211. assertThat(underTest.readComponentCoverage(COMPONENT_REF)).isExhausted();
  212. }
  213. @Test
  214. public void verify_readComponentCoverage() {
  215. writer.writeComponentCoverage(COMPONENT_REF, of(COVERAGE_1, COVERAGE_2));
  216. CloseableIterator<ScannerReport.LineCoverage> res = underTest.readComponentCoverage(COMPONENT_REF);
  217. assertThat(res).toIterable().containsExactly(COVERAGE_1, COVERAGE_2);
  218. res.close();
  219. }
  220. @Test
  221. public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
  222. assertThat(underTest.readFileSource(COMPONENT_REF)).isEmpty();
  223. }
  224. @Test
  225. public void verify_readFileSource() throws IOException {
  226. File file = writer.getSourceFile(COMPONENT_REF);
  227. FileUtils.writeLines(file, of("1", "2", "3"));
  228. CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
  229. assertThat(res).toIterable().containsExactly("1", "2", "3");
  230. res.close();
  231. }
  232. @Test
  233. public void verify_readAnalysisWarnings() {
  234. ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
  235. ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
  236. ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
  237. writer.writeAnalysisWarnings(warnings);
  238. CloseableIterator<ScannerReport.AnalysisWarning> res = underTest.readAnalysisWarnings();
  239. assertThat(res).toIterable().containsExactlyElementsOf(warnings);
  240. res.close();
  241. }
  242. }