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();
}
Aggregations