@Test
 public void testStaleWhenClosed() throws Exception {
   conn.bind(socket);
   conn.ensureOpen();
   conn.close();
   Assert.assertTrue(conn.isStale());
 }
  @Test
  public void testConnectionShutdown() throws Exception {
    final InputStream instream = Mockito.mock(InputStream.class);
    final OutputStream outstream = Mockito.mock(OutputStream.class);
    Mockito.when(socket.getInputStream()).thenReturn(instream);
    Mockito.when(socket.getOutputStream()).thenReturn(outstream);

    conn.bind(socket);
    conn.ensureOpen();
    conn.getSessionOutputBuffer().write(0);

    Assert.assertTrue(conn.isOpen());

    conn.shutdown();

    Assert.assertFalse(conn.isOpen());

    Mockito.verify(outstream, Mockito.never())
        .write(Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt());
    Mockito.verify(socket, Mockito.never()).shutdownInput();
    Mockito.verify(socket, Mockito.never()).shutdownOutput();
    Mockito.verify(socket, Mockito.times(1)).close();

    conn.close();
    Mockito.verify(socket, Mockito.times(1)).close();

    conn.shutdown();
    Mockito.verify(socket, Mockito.times(2)).close();
  }
  @Test
  public void testNotStaleWhenHasData() throws Exception {
    final ByteArrayInputStream instream =
        Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5}));
    Mockito.when(socket.getInputStream()).thenReturn(instream);

    conn.bind(socket);
    conn.ensureOpen();

    Assert.assertFalse(conn.isStale());
  }
  @Test
  public void testStaleWhenIOError() throws Exception {
    final InputStream instream = Mockito.mock(InputStream.class);
    Mockito.when(socket.getInputStream()).thenReturn(instream);
    Mockito.when(instream.read(Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt()))
        .thenThrow(new SocketException());

    conn.bind(socket);
    conn.ensureOpen();

    Assert.assertTrue(conn.isStale());
  }
  @Test
  public void testAwaitInputNoData() throws Exception {
    final InputStream instream = Mockito.mock(InputStream.class);
    Mockito.when(socket.getInputStream()).thenReturn(instream);
    Mockito.when(instream.read(Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt()))
        .thenReturn(-1);

    conn.bind(socket);
    conn.ensureOpen();

    Assert.assertFalse(conn.awaitInput(432));
  }
  @Test
  public void testAwaitInputInBuffer() throws Exception {
    final ByteArrayInputStream instream =
        Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5}));
    Mockito.when(socket.getInputStream()).thenReturn(instream);

    conn.bind(socket);
    conn.ensureOpen();
    conn.getSessionInputBuffer().read();

    Assert.assertTrue(conn.awaitInput(432));

    Mockito.verify(socket, Mockito.never()).setSoTimeout(Mockito.anyInt());
    Mockito.verify(instream, Mockito.times(1))
        .read(Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt());
  }