### Question:
J2EEConnectionPool implements JDBCConnectionPool { @Override public Connection reserveConnection() throws SQLException { Connection conn = getDataSource().getConnection(); conn.setAutoCommit(false); return conn; } J2EEConnectionPool(DataSource dataSource);  J2EEConnectionPool(String dataSourceJndiName); @Override Connection reserveConnection(); @Override void releaseConnection(Connection conn); @Override void destroy();  }### Answer:
@Test public void reserveConnection_dataSourceSpecified_shouldReturnValidConnection() throws SQLException { Connection connection = EasyMock.createMock(Connection.class); connection.setAutoCommit(false); EasyMock.expectLastCall(); DataSource ds = EasyMock.createMock(DataSource.class); ds.getConnection(); EasyMock.expectLastCall().andReturn(connection); EasyMock.replay(connection, ds); J2EEConnectionPool pool = new J2EEConnectionPool(ds); Connection c = pool.reserveConnection(); Assert.assertEquals(connection, c); EasyMock.verify(connection, ds); }
@Test public void reserveConnection_dataSourceSpecified_shouldReturnValidConnection() throws SQLException { Connection connection = EasyMock.createMock(Connection.class); connection.setAutoCommit(false); EasyMock.expectLastCall(); DataSource ds = EasyMock.createMock(DataSource.class); ds.getConnection(); EasyMock.expectLastCall().andReturn(connection); EasyMock.replay(connection, ds); J2EEConnectionPool pool = new J2EEConnectionPool(ds); Connection c = pool.reserveConnection(); assertEquals(connection, c); EasyMock.verify(connection, ds); }
@Test(expected = SQLException.class) public void reserveConnection_nonExistantDataSourceLookedUp_shouldFail() throws SQLException, NamingException { System.setProperty("java.naming.factory.initial", "com.vaadin.addon.sqlcontainer.connection.MockInitialContextFactory"); Context context = EasyMock.createMock(Context.class); context.lookup("foo"); EasyMock.expectLastCall().andThrow(new NamingException("fail")); MockInitialContextFactory.setMockContext(context); EasyMock.replay(context); J2EEConnectionPool pool = new J2EEConnectionPool("foo"); pool.reserveConnection(); EasyMock.verify(context); }