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

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