Search in sources :

Example 1 with LineRange

use of org.sonar.core.hash.LineRange in project sonarqube by SonarSource.

the class SignificantCodeRepository method toArray.

private static LineRange[] toArray(CloseableIterator<LineSgnificantCode> lineRanges, int numLines) {
    LineRange[] ranges = new LineRange[numLines];
    LineSgnificantCode currentLine = null;
    for (int i = 0; i < numLines; i++) {
        if (currentLine == null) {
            if (!lineRanges.hasNext()) {
                break;
            }
            currentLine = lineRanges.next();
        }
        if (currentLine.getLine() == i + 1) {
            ranges[i] = new LineRange(currentLine.getStartOffset(), currentLine.getEndOffset());
            currentLine = null;
        }
    }
    return ranges;
}
Also used : LineRange(org.sonar.core.hash.LineRange) LineSgnificantCode(org.sonar.scanner.protocol.output.ScannerReport.LineSgnificantCode)

Example 2 with LineRange

use of org.sonar.core.hash.LineRange in project sonarqube by SonarSource.

the class SourceLinesHashRepositoryImplTest method SignificantCodeLineHashesComputer_delegates_after_taking_ranges_into_account.

@Test
public void SignificantCodeLineHashesComputer_delegates_after_taking_ranges_into_account() {
    LineRange[] lineRanges = { new LineRange(0, 1), null, new LineRange(1, 5), new LineRange(2, 7), new LineRange(4, 5) };
    SourceLineHashesComputer lineHashComputer = mock(SourceLineHashesComputer.class);
    SignificantCodeLineHashesComputer computer = new SignificantCodeLineHashesComputer(lineHashComputer, lineRanges);
    computer.addLine("testline");
    computer.addLine("testline");
    computer.addLine("testline");
    computer.addLine("testline");
    computer.addLine("testline");
    computer.addLine("testline");
    verify(lineHashComputer).addLine("t");
    // there is an extra line at the end which will be ignored since there is no range for it
    verify(lineHashComputer, times(2)).addLine("");
    verify(lineHashComputer).addLine("estl");
    verify(lineHashComputer).addLine("stlin");
    verify(lineHashComputer).addLine("l");
    verifyNoMoreInteractions(lineHashComputer);
}
Also used : SourceLineHashesComputer(org.sonar.core.hash.SourceLineHashesComputer) LineRange(org.sonar.core.hash.LineRange) SignificantCodeLineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.SignificantCodeLineHashesComputer) Test(org.junit.Test)

Example 3 with LineRange

use of org.sonar.core.hash.LineRange in project sonarqube by SonarSource.

the class SourceLinesHashRepositoryImplTest method should_return_version_of_line_hashes_with_significant_code_in_the_report.

@Test
public void should_return_version_of_line_hashes_with_significant_code_in_the_report() {
    LineRange[] lineRanges = { new LineRange(0, 1), null, new LineRange(1, 5) };
    when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.of(lineRanges));
    assertThat(underTest.getLineHashesVersion(file)).isEqualTo(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue());
    verify(significantCodeRepository).getRangesPerLine(file);
    verifyNoMoreInteractions(significantCodeRepository);
    verifyZeroInteractions(dbLineHashVersion);
}
Also used : LineRange(org.sonar.core.hash.LineRange) Test(org.junit.Test)

Example 4 with LineRange

use of org.sonar.core.hash.LineRange in project sonarqube by SonarSource.

the class SourceLinesHashRepositoryImplTest method should_persist_with_significant_code_from_cache_if_possible.

@Test
public void should_persist_with_significant_code_from_cache_if_possible() {
    List<String> lineHashes = Lists.newArrayList("line1", "line2", "line3");
    LineRange[] lineRanges = { new LineRange(0, 1), null, new LineRange(1, 5) };
    sourceLinesHashCache.computeIfAbsent(file, c -> lineHashes);
    when(dbLineHashVersion.hasLineHashesWithoutSignificantCode(file)).thenReturn(false);
    when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.of(lineRanges));
    LineHashesComputer hashesComputer = underTest.getLineHashesComputerToPersist(file);
    assertThat(hashesComputer).isInstanceOf(CachedLineHashesComputer.class);
    assertThat(hashesComputer.getResult()).isEqualTo(lineHashes);
}
Also used : LineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.LineHashesComputer) SourceLineHashesComputer(org.sonar.core.hash.SourceLineHashesComputer) SignificantCodeLineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.SignificantCodeLineHashesComputer) CachedLineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.CachedLineHashesComputer) LineRange(org.sonar.core.hash.LineRange) Test(org.junit.Test)

Example 5 with LineRange

use of org.sonar.core.hash.LineRange in project sonarqube by SonarSource.

the class SourceLinesHashRepositoryImplTest method should_create_hash_with_significant_code.

@Test
public void should_create_hash_with_significant_code() {
    LineRange[] lineRanges = { new LineRange(0, 1), null, new LineRange(1, 5) };
    when(dbLineHashVersion.hasLineHashesWithSignificantCode(file)).thenReturn(true);
    when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.of(lineRanges));
    List<String> lineHashes = underTest.getLineHashesMatchingDBVersion(file);
    assertLineHashes(lineHashes, "l", "", "ine3");
    verify(dbLineHashVersion).hasLineHashesWithoutSignificantCode(file);
    verifyNoMoreInteractions(dbLineHashVersion);
    verify(significantCodeRepository).getRangesPerLine(file);
    verifyNoMoreInteractions(significantCodeRepository);
}
Also used : LineRange(org.sonar.core.hash.LineRange) Test(org.junit.Test)

Aggregations

LineRange (org.sonar.core.hash.LineRange)8 Test (org.junit.Test)7 SignificantCodeLineHashesComputer (org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.SignificantCodeLineHashesComputer)3 SourceLineHashesComputer (org.sonar.core.hash.SourceLineHashesComputer)3 LineSgnificantCode (org.sonar.scanner.protocol.output.ScannerReport.LineSgnificantCode)3 ArrayList (java.util.ArrayList)2 Component (org.sonar.ce.task.projectanalysis.component.Component)2 CachedLineHashesComputer (org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.CachedLineHashesComputer)2 LineHashesComputer (org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.LineHashesComputer)2