@Test public void closeStream() throws IOException { final ConnectedStreamChannelMock channelMock = new ConnectedStreamChannelMock(); final ChannelOutputStream delegateStream = new ChannelOutputStream(channelMock); final LimitedOutputStream stream = new LimitedOutputStream(delegateStream, 5); stream.write('a'); stream.write('b'); stream.write('c'); // flush assertFalse(channelMock.isFlushed()); stream.flush(); assertTrue(channelMock.isFlushed()); // close stream.close(); assertTrue(channelMock.isShutdownWrites()); IOException expected = null; try { stream.write('d'); } catch (IOException e) { expected = e; } assertNotNull(expected); expected = null; try { stream.write("efg".getBytes("UTF-8")); } catch (IOException e) { expected = e; } assertNotNull(expected); stream.flush(); assertWrittenMessage(channelMock, "abc"); // idempotent stream.close(); expected = null; try { stream.write('h'); } catch (IOException e) { expected = e; } assertNotNull(expected); expected = null; try { stream.write("ijk".getBytes("UTF-8")); } catch (IOException e) { expected = e; } assertNotNull(expected); stream.flush(); assertWrittenMessage(channelMock, "abc"); }
@Test public void closeEmptyStream() throws IOException { final ConnectedStreamChannelMock channelMock = new ConnectedStreamChannelMock(); final ChannelOutputStream delegateStream = new ChannelOutputStream(channelMock); final LimitedOutputStream stream = new LimitedOutputStream(delegateStream, 5); stream.close(); assertTrue(channelMock.isShutdownWrites()); IOException expected = null; try { stream.write('a'); } catch (IOException e) { expected = e; } assertNotNull(expected); expected = null; try { stream.write("bcd".getBytes("UTF-8")); } catch (IOException e) { expected = e; } assertNotNull(expected); stream.flush(); assertWrittenMessage(channelMock); // idempotent stream.close(); expected = null; try { stream.write('e'); } catch (IOException e) { expected = e; } assertNotNull(expected); expected = null; try { stream.write("fgh".getBytes("UTF-8")); } catch (IOException e) { expected = e; } assertNotNull(expected); stream.flush(); assertWrittenMessage(channelMock); }