MessagesToHtmlWriter implements AutoCloseable { @Override public void close() throws IOException { if(streamClosed){ return; } if (!preMessageWritten) { writePreMessage(); preMessageWritten = true; } if (!postMessageWritten) { writePostMessage(); postMessageWritten = true; } writer.close(); streamClosed = true; } MessagesToHtmlWriter(Writer writer); void write(Messages.Envelope envelope); @Override void close();  }
@Test void it_can_be_closed_twice() throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(bytes, UTF_8); BufferedWriter bw = new BufferedWriter(osw); MessagesToHtmlWriter messagesToHtmlWriter = new MessagesToHtmlWriter(bw); messagesToHtmlWriter.close(); assertDoesNotThrow(messagesToHtmlWriter::close); }