Search in sources :

Example 1 with LineReader

use of org.sonar.ce.task.projectanalysis.source.linereader.LineReader in project sonarqube by SonarSource.

the class LineReadersImplTest method close_closes_all_closeables.

@Test
public void close_closes_all_closeables() {
    LineReader r1 = mock(LineReader.class);
    LineReader r2 = mock(LineReader.class);
    CloseableIterator<Object> c1 = newCloseableIteratorMock();
    CloseableIterator<Object> c2 = newCloseableIteratorMock();
    SourceLineReadersFactory.LineReaders lineReaders = new SourceLineReadersFactory.LineReadersImpl(asList(r1, r2), null, asList(c1, c2));
    lineReaders.close();
    verify(c1).close();
    verify(c2).close();
    verifyNoMoreInteractions(c1, c2);
    verifyZeroInteractions(r1, r2);
}
Also used : ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) Test(org.junit.Test)

Example 2 with LineReader

use of org.sonar.ce.task.projectanalysis.source.linereader.LineReader in project sonarqube by SonarSource.

the class LineReadersImplTest method read_calls_all_readers.

@Test
public void read_calls_all_readers() {
    LineReader r1 = mock(LineReader.class);
    LineReader r2 = mock(LineReader.class);
    CloseableIterator<Object> c1 = newCloseableIteratorMock();
    CloseableIterator<Object> c2 = newCloseableIteratorMock();
    SourceLineReadersFactory.LineReadersImpl lineReaders = new SourceLineReadersFactory.LineReadersImpl(asList(r1, r2), null, asList(c1, c2));
    DbFileSources.Line.Builder builder = DbFileSources.Line.newBuilder();
    lineReaders.read(builder, NOOP_READ_ERROR_CONSUMER);
    verify(r1).read(builder);
    verify(r2).read(builder);
    verifyNoMoreInteractions(r1, r2);
    verifyZeroInteractions(c1, c2);
}
Also used : ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) Test(org.junit.Test)

Example 3 with LineReader

use of org.sonar.ce.task.projectanalysis.source.linereader.LineReader in project sonarqube by SonarSource.

the class LineReadersImplTest method read_calls_ReaderError_consumer_with_each_read_error_returned_by_all_readers.

@Test
public void read_calls_ReaderError_consumer_with_each_read_error_returned_by_all_readers() {
    int readErrorCount = 2 + 2 * new Random().nextInt(10);
    int halfCount = readErrorCount / 2;
    ReadError[] expectedReadErrors = IntStream.range(0, readErrorCount).mapToObj(i -> new ReadError(LineReader.Data.SYMBOLS, i)).toArray(ReadError[]::new);
    LineReader[] lineReaders = IntStream.range(0, halfCount).mapToObj(i -> {
        LineReader lineReader = mock(LineReader.class);
        when(lineReader.read(any())).thenReturn(Optional.of(expectedReadErrors[i])).thenReturn(Optional.of(expectedReadErrors[i + halfCount])).thenReturn(Optional.empty());
        return lineReader;
    }).toArray(LineReader[]::new);
    DbFileSources.Line.Builder builder = DbFileSources.Line.newBuilder();
    SourceLineReadersFactory.LineReadersImpl underTest = new SourceLineReadersFactory.LineReadersImpl(stream(lineReaders).collect(toList()), null, Collections.emptyList());
    List<ReadError> readErrors = new ArrayList<>();
    // calls first mocked result of each Reader mock => we get first half read errors
    underTest.read(builder, readErrors::add);
    assertThat(readErrors).contains(stream(expectedReadErrors).limit(halfCount).toArray(ReadError[]::new));
    // calls first mocked result of each Reader mock => we get second half read errors
    readErrors.clear();
    underTest.read(builder, readErrors::add);
    assertThat(readErrors).contains(stream(expectedReadErrors).skip(halfCount).toArray(ReadError[]::new));
    // calls third mocked result of each Reader mock (empty) => consumer is never called => empty list
    readErrors.clear();
    underTest.read(builder, readErrors::add);
    assertThat(readErrors).isEmpty();
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) CloseableIterator(org.sonar.core.util.CloseableIterator) DbFileSources(org.sonar.db.protobuf.DbFileSources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Optional(java.util.Optional) Collections(java.util.Collections) ReadError(org.sonar.ce.task.projectanalysis.source.linereader.LineReader.ReadError) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) ArrayList(java.util.ArrayList) Random(java.util.Random) ReadError(org.sonar.ce.task.projectanalysis.source.linereader.LineReader.ReadError) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) Test(org.junit.Test)

Example 4 with LineReader

use of org.sonar.ce.task.projectanalysis.source.linereader.LineReader in project sonarqube by SonarSource.

the class SourceLineReadersFactory method getLineReaders.

public LineReaders getLineReaders(Component component) {
    List<LineReader> readers = new ArrayList<>();
    List<CloseableIterator<?>> closeables = new ArrayList<>();
    ScmLineReader scmLineReader = null;
    int componentRef = component.getReportAttributes().getRef();
    CloseableIterator<ScannerReport.LineCoverage> coverageIt = reportReader.readComponentCoverage(componentRef);
    closeables.add(coverageIt);
    readers.add(new CoverageLineReader(coverageIt));
    Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
    if (scmInfoOptional.isPresent()) {
        scmLineReader = new ScmLineReader(scmInfoOptional.get());
        readers.add(scmLineReader);
    }
    RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
    CloseableIterator<ScannerReport.SyntaxHighlightingRule> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
    closeables.add(highlightingIt);
    readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));
    CloseableIterator<ScannerReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
    closeables.add(symbolsIt);
    readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));
    readers.add(new DuplicationLineReader(duplicationRepository.getDuplications(component)));
    readers.add(new IsNewLineReader(newLinesRepository, component));
    return new LineReadersImpl(readers, scmLineReader, closeables);
}
Also used : CoverageLineReader(org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader) CloseableIterator(org.sonar.core.util.CloseableIterator) SymbolsLineReader(org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader) ArrayList(java.util.ArrayList) IsNewLineReader(org.sonar.ce.task.projectanalysis.source.linereader.IsNewLineReader) HighlightingLineReader(org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader) DuplicationLineReader(org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader) DuplicationLineReader(org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) SymbolsLineReader(org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader) IsNewLineReader(org.sonar.ce.task.projectanalysis.source.linereader.IsNewLineReader) CoverageLineReader(org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader) HighlightingLineReader(org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) RangeOffsetConverter(org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter) ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo)

Aggregations

LineReader (org.sonar.ce.task.projectanalysis.source.linereader.LineReader)4 ScmLineReader (org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 CloseableIterator (org.sonar.core.util.CloseableIterator)2 Arrays.asList (java.util.Arrays.asList)1 Arrays.stream (java.util.Arrays.stream)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Consumer (java.util.function.Consumer)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)1 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)1