### Question:
SimpleJDBCConnectionPool implements JDBCConnectionPool { @Override public synchronized Connection reserveConnection() throws SQLException { if (!initialized) { initializeConnections(); } if (availableConnections.isEmpty()) { if (reservedConnections.size() < maxConnections) { availableConnections.add(createConnection()); } else { throw new SQLException("Connection limit has been reached."); } } Connection c = availableConnections.iterator().next(); availableConnections.remove(c); reservedConnections.add(c); return c; } SimpleJDBCConnectionPool(String driverName, String connectionUri,
            String userName, String password);  SimpleJDBCConnectionPool(String driverName, String connectionUri,
            String userName, String password, int initialConnections,
            int maxConnections); @Override synchronized Connection reserveConnection(); @Override synchronized void releaseConnection(Connection conn); @Override void destroy();  }### Answer:
@Test public void reserveConnection_reserveNewConnection_returnsConnection() throws SQLException { Connection conn = connectionPool.reserveConnection(); Assert.assertNotNull(conn); }
@Test(expected = SQLException.class) public void reserveConnection_noConnectionsLeft_shouldFail() throws SQLException { try { connectionPool.reserveConnection(); connectionPool.reserveConnection(); } catch (SQLException e) { e.printStackTrace(); Assert.fail("Exception before all connections used! " + e.getMessage()); } connectionPool.reserveConnection(); Assert.fail("Reserving connection didn't fail even though no connections are available!"); }
@Test public void reserveConnection_oneConnectionLeft_returnsConnection() throws SQLException { try { connectionPool.reserveConnection(); } catch (SQLException e) { e.printStackTrace(); Assert.fail("Exception before all connections used! " + e.getMessage()); } Connection conn = connectionPool.reserveConnection(); Assert.assertNotNull(conn); }
@Test public void reserveConnection_newConnectionOpened_shouldSucceed() throws SQLException { connectionPool = new SimpleJDBCConnectionPool( SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL, SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 0, 2); Connection c = connectionPool.reserveConnection(); Assert.assertNotNull(c); }
@Test public void reserveConnection_reserveNewConnection_returnsConnection() throws SQLException { Connection conn = connectionPool.reserveConnection(); assertNotNull(conn); }
@Test(expected = SQLException.class) public void reserveConnection_noConnectionsLeft_shouldFail() throws SQLException { try { connectionPool.reserveConnection(); connectionPool.reserveConnection(); } catch (SQLException e) { e.printStackTrace(); fail("Exception before all connections used! " + e.getMessage()); } connectionPool.reserveConnection(); fail("Reserving connection didn't fail even though no connections are available!"); }
@Test public void reserveConnection_oneConnectionLeft_returnsConnection() throws SQLException { try { connectionPool.reserveConnection(); } catch (SQLException e) { e.printStackTrace(); fail("Exception before all connections used! " + e.getMessage()); } Connection conn = connectionPool.reserveConnection(); assertNotNull(conn); }
@Test public void reserveConnection_newConnectionOpened_shouldSucceed() throws SQLException { connectionPool = new SimpleJDBCConnectionPool( SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL, SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 0, 2); Connection c = connectionPool.reserveConnection(); assertNotNull(c); }