UnsyncBufferedWriter extends Writer { @Override public void close() throws IOException { if (_writer == null) { return; } flush(); _writer.close(); _writer = null; _buffer = null; } UnsyncBufferedWriter(Writer writer);  UnsyncBufferedWriter(Writer writer, int size); @Override void close(); @Override void flush(); void newLine(); @Override void write(char[] chars, int offset, int length); @Override void write(int c); @Override void write(String string, int offset, int length);  }
@Test public void testClose() throws Exception { StringWriter stringWriter = new StringWriter(); UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter( stringWriter, 10); Assert.assertNotNull(_bufferField.get(unsyncBufferedWriter)); Assert.assertSame(stringWriter, _writerField.get(unsyncBufferedWriter)); unsyncBufferedWriter.write("test"); unsyncBufferedWriter.close(); Assert.assertNull(_bufferField.get(unsyncBufferedWriter)); Assert.assertNull(_writerField.get(unsyncBufferedWriter)); Assert.assertEquals("test", stringWriter.toString()); try { unsyncBufferedWriter.flush(); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals("Writer is null", ioException.getMessage()); } try { unsyncBufferedWriter.write("abc".toCharArray(), 0, 3); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals("Writer is null", ioException.getMessage()); } try { unsyncBufferedWriter.write(1); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals("Writer is null", ioException.getMessage()); } try { unsyncBufferedWriter.write("abc", 0, 3); Assert.fail(); } catch (IOException ioException) { Assert.assertEquals("Writer is null", ioException.getMessage()); } unsyncBufferedWriter.close(); }