### Question:
CachedConnection implements Connection { void open() { if (session == null || !session.isConnected()) { throw new IllegalStateException("Connection is either unavailable or physically closed."); } closed = false; } CachedConnection(final String username, final String password, final URI serverAddress,
            final ConnectionLibrarian<CachedConnection> librarian);  CachedConnection(final String username, final String salt, final URI privateKeyLocation,
            final URI serverAddress, final ConnectionLibrarian<CachedConnection> librarian); @Override void close(); void closePhysical(); @Override boolean equals(final Object obj); @Override ChannelSftp getSFtp(); @Override ChannelShell getShell(); @Override int hashCode(); @Override boolean isClosed();  }### Answer:
@Test public void testOpen() { prepareUsernamePasswordSession(); final CachedConnection connection = new CachedConnection(USERNAME, PASSWORD, SERVER_ADDRESS, librarian); when(usernamePasswordSession.isConnected()).thenReturn(Boolean.TRUE); connection.close(); assertThat(connection.isClosed()).isTrue(); connection.open(); assertThat(connection.isClosed()).isFalse(); }
@Test public void testOpenClosedSession() { prepareUsernamePasswordSession(); final CachedConnection connection = new CachedConnection(USERNAME, PASSWORD, SERVER_ADDRESS, librarian); when(usernamePasswordSession.isConnected()).thenReturn(Boolean.FALSE); try { connection.open(); fail("No exception thrown."); } catch (final IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Connection is either unavailable or physically closed."); } }