Search in sources :

Example 1 with ReadError

use of org.sonar.ce.task.projectanalysis.source.linereader.LineReader.ReadError 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)

Aggregations

ArrayList (java.util.ArrayList)1 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 Test (org.junit.Test)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 Mockito.when (org.mockito.Mockito.when)1 Changeset (org.sonar.ce.task.projectanalysis.scm.Changeset)1 LineReader (org.sonar.ce.task.projectanalysis.source.linereader.LineReader)1