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 4.0KB

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