@Test
public void testRead() throws IOException {

  DataSourceInputStream inputStream = buildTestInputStream();
  // Read bytes.
  byte[] readBytes = new byte[TEST_DATA.length];
  int totalBytesRead = 0;
  while (totalBytesRead < TEST_DATA.length) {
    int bytesRead = inputStream.read(readBytes, totalBytesRead,
        TEST_DATA.length - totalBytesRead);
    assertThat(bytesRead).isGreaterThan(0);
    totalBytesRead += bytesRead;
    assertThat(inputStream.bytesRead()).isEqualTo(totalBytesRead);
  }
  // Check the read data.
  assertThat(readBytes).isEqualTo(TEST_DATA);
  // Check end of stream.
  assertThat(inputStream.bytesRead()).isEqualTo(TEST_DATA.length);
  assertThat(totalBytesRead).isEqualTo(TEST_DATA.length);
  assertThat(inputStream.read()).isEqualTo(-1);
  // Check close succeeds.
  inputStream.close();
}