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.

SignificantCodeRepositoryTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.source;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  26. import org.sonar.ce.task.projectanalysis.component.Component;
  27. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  28. import org.sonar.core.hash.LineRange;
  29. import org.sonar.scanner.protocol.output.ScannerReport;
  30. import org.sonar.scanner.protocol.output.ScannerReport.LineSgnificantCode;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  33. public class SignificantCodeRepositoryTest {
  34. private static final String FILE_UUID = "FILE_UUID";
  35. private static final String FILE_KEY = "FILE_KEY";
  36. private static final int FILE_REF = 2;
  37. @Rule
  38. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  39. private SignificantCodeRepository underTest = new SignificantCodeRepository(reportReader);
  40. @Test
  41. public void return_empty_if_information_not_available() {
  42. assertThat(underTest.getRangesPerLine(createComponent(3))).isEmpty();
  43. }
  44. @Test
  45. public void return_null_for_lines_without_information() {
  46. Component component = createComponent(5);
  47. List<ScannerReport.LineSgnificantCode> significantCode = new ArrayList<>();
  48. // line 3 and 5 missing
  49. significantCode.add(createLineSignificantCode(1, 1, 2));
  50. significantCode.add(createLineSignificantCode(2, 1, 2));
  51. significantCode.add(createLineSignificantCode(4, 1, 2));
  52. reportReader.putSignificantCode(component.getReportAttributes().getRef(), significantCode);
  53. assertThat(underTest.getRangesPerLine(component)).isNotEmpty();
  54. LineRange[] lines = underTest.getRangesPerLine(component).get();
  55. assertThat(lines).hasSize(5);
  56. assertThat(lines[0]).isNotNull();
  57. assertThat(lines[1]).isNotNull();
  58. assertThat(lines[2]).isNull();
  59. assertThat(lines[3]).isNotNull();
  60. assertThat(lines[4]).isNull();
  61. }
  62. @Test
  63. public void translate_offset_for_each_line() {
  64. Component component = createComponent(1);
  65. List<ScannerReport.LineSgnificantCode> significantCode = new ArrayList<>();
  66. significantCode.add(createLineSignificantCode(1, 1, 2));
  67. reportReader.putSignificantCode(component.getReportAttributes().getRef(), significantCode);
  68. assertThat(underTest.getRangesPerLine(component)).isNotEmpty();
  69. LineRange[] lines = underTest.getRangesPerLine(component).get();
  70. assertThat(lines).hasSize(1);
  71. assertThat(lines[0].startOffset()).isOne();
  72. assertThat(lines[0].endOffset()).isEqualTo(2);
  73. }
  74. private static LineSgnificantCode createLineSignificantCode(int line, int start, int end) {
  75. return LineSgnificantCode.newBuilder()
  76. .setLine(line)
  77. .setStartOffset(start)
  78. .setEndOffset(end)
  79. .build();
  80. }
  81. private static Component createComponent(int lineCount) {
  82. return builder(Component.Type.FILE, FILE_REF)
  83. .setKey(FILE_KEY)
  84. .setUuid(FILE_UUID)
  85. .setFileAttributes(new FileAttributes(false, null, lineCount))
  86. .build();
  87. }
  88. }