ReleasableOutputStream extends OutputStream { @Override public void flush() throws IOException { try { delegate.flush(); } catch (IOException e) { safeClose(); isClosed = true; } } ReleasableOutputStream(OutputStream delegate, Runnable onClose); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void flush(); @Override void close();  }
@Test public void flush() throws Exception { releasableOutputStream.flush(); assertFalse(wasCalled.get()); }
@Test public void failedFlush() throws Exception { failedReleasableOutputStream.flush(); assertTrue(wasCalled.get()); }