@SuppressWarnings("unchecked")
@Test
public void testGetNitfSegmentsFlowTFBOSAsBytesourceThrows()
    throws IOException, NitfFormatException {

  ByteSource byteSource = mock(ByteSource.class);
  when(byteSource.openBufferedStream()).thenThrow(IOException.class);
  TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
  when(tfbos.asByteSource()).thenReturn(byteSource);
  catalogOutputAdapter =
      new CatalogOutputAdapter(new Semaphore(3, true)) {
        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
          return tfbos;
        }
      };
  try {
    catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] {(byte) 0}));
    fail("expected an exception, shouldn't reach this line");
  } catch (IOException | NitfFormatException e) {
    assertThat(e, notNullValue());
  }
  verify(tfbos).close();
}