CompositeBalanceFileReader implements BalanceFileReader { @Override public Stream<AccountBalance> read(File file) { return getReader(file).read(file); }  @Override Stream<AccountBalance> read(File file);  }
@Test void defaultsToVersion1Reader() throws IOException { String firstLine = "null"; FileUtils.writeStringToFile(balanceFile, firstLine, "utf-8"); when(readerImplV2.isFirstLineFromFileVersion(firstLine)).thenReturn(false); compositeBalanceFileReader.read(balanceFile); verify(readerImplV1, times(1)).read(balanceFile); verify(readerImplV2, times(0)).read(balanceFile); }
@Test void usesVersion2Reader() throws IOException { String firstLine = "# version:2"; FileUtils.writeStringToFile(balanceFile, firstLine, "utf-8"); when(readerImplV2.isFirstLineFromFileVersion(firstLine)).thenReturn(true); compositeBalanceFileReader.read(balanceFile); verify(readerImplV2, times(1)).read(balanceFile); verify(readerImplV1, times(0)).read(balanceFile); }
@Test void verifyLongFirstLineStopsAtBuffer() throws IOException { String firstLine = "A".repeat(32); String bufferedFirstLine = "A".repeat(CompositeBalanceFileReader.BUFFER_SIZE); FileUtils.writeStringToFile(balanceFile, firstLine, "utf-8"); when(readerImplV2.isFirstLineFromFileVersion(bufferedFirstLine)).thenReturn(false); compositeBalanceFileReader.read(balanceFile); verify(readerImplV1, times(1)).read(balanceFile); verify(readerImplV2, times(0)).read(balanceFile); }
@Test void missingFile() throws IOException { assertThrows(InvalidDatasetException.class, () -> { compositeBalanceFileReader.read(new File("")); }); }
@Test void nullFile() throws IOException { assertThrows(InvalidDatasetException.class, () -> { compositeBalanceFileReader.read(null); }); }
@Test void emptyFile() throws IOException { assertThrows(InvalidDatasetException.class, () -> { compositeBalanceFileReader.read(balanceFile); }); }