HAWQDataSource { Connection getConnection(String host, String port, String user, String password) throws SystemException, SQLException { LOG.debug("Getting connection for = {}:{}:{}", host, port, user); String key = composeKey(host, port, user, password); if (!key.equals(dataSourceKey)) { resetDataSource(host, port, user, password); } Connection conn = dataSource.getConnection(); LOG.debug("Acquired connection = {}", conn); return conn; }    }
@Test public void testGetConnection() throws Exception { ComboPooledDataSource cpds1 = createMock(ComboPooledDataSource.class); expectNew(ComboPooledDataSource.class).andReturn(cpds1); cpds1.setDriverClass("org.postgresql.Driver"); expectLastCall().once(); cpds1.setJdbcUrl("jdbc:postgresql: expectLastCall().once(); cpds1.setUser("user"); expectLastCall().once(); cpds1.setPassword("password"); expectLastCall().once(); cpds1.setInitialPoolSize(0); expectLastCall().once(); cpds1.setMinPoolSize(0); expectLastCall().once(); cpds1.setMaxPoolSize(1); expectLastCall().once(); cpds1.setMaxIdleTime(600); expectLastCall().once(); cpds1.setAcquireRetryAttempts(5); expectLastCall().once(); cpds1.setAcquireRetryDelay(1000); expectLastCall().once(); Connection conn1 = createMock(Connection.class); expect(cpds1.getConnection()).andReturn(conn1); cpds1.close(); expectLastCall().once(); ComboPooledDataSource cpds2 = createStrictMock(ComboPooledDataSource.class); expectNew(ComboPooledDataSource.class).andReturn(cpds2); cpds2.setDriverClass("org.postgresql.Driver"); expectLastCall().once(); cpds2.setJdbcUrl("jdbc:postgresql: expectLastCall().once(); cpds2.setUser("user2"); expectLastCall().once(); cpds2.setPassword("password"); expectLastCall().once(); cpds2.setInitialPoolSize(0); expectLastCall().once(); cpds2.setMinPoolSize(0); expectLastCall().once(); cpds2.setMaxPoolSize(1); expectLastCall().once(); cpds2.setMaxIdleTime(600); expectLastCall().once(); cpds2.setAcquireRetryAttempts(5); expectLastCall().once(); cpds2.setAcquireRetryDelay(1000); expectLastCall().once(); Connection conn2 = createMock(Connection.class); expect(cpds2.getConnection()).andReturn(conn2); expect(cpds2.getConnection()).andReturn(conn2); replay(cpds1, ComboPooledDataSource.class); replay(cpds2, ComboPooledDataSource.class); replay(conn1); replay(conn2); Connection connection1 = datasource.getConnection("host", "port", "user", "password"); Connection connection2 = datasource.getConnection("host", "port", "user2", "password"); Connection connection3 = datasource.getConnection("host", "port", "user2", "password"); assertNotEquals(connection1, connection2); assertEquals(connection2, connection3); }
@Test(expected = SystemException.class) public void testGetConnection_FailToLoadDriver() throws Exception { ComboPooledDataSource cpds = createMock(ComboPooledDataSource.class); expectNew(ComboPooledDataSource.class).andReturn(cpds); cpds.setDriverClass("org.postgresql.Driver"); expectLastCall().andThrow(new RuntimeException("driver not found")); replay(cpds, ComboPooledDataSource.class); datasource.getConnection("host", "port", "user", "password"); }
@Test(expected = SQLException.class) public void testGetConnection_FailToGetConnection() throws Exception { ComboPooledDataSource cpds1 = createMock(ComboPooledDataSource.class); expectNew(ComboPooledDataSource.class).andReturn(cpds1); cpds1.setDriverClass("org.postgresql.Driver"); expectLastCall().once(); cpds1.setJdbcUrl("jdbc:postgresql: expectLastCall().once(); cpds1.setUser("user"); expectLastCall().once(); cpds1.setPassword("password"); expectLastCall().once(); cpds1.setInitialPoolSize(0); expectLastCall().once(); cpds1.setMinPoolSize(0); expectLastCall().once(); cpds1.setMaxPoolSize(1); expectLastCall().once(); cpds1.setMaxIdleTime(600); expectLastCall().once(); cpds1.setAcquireRetryAttempts(5); expectLastCall().once(); cpds1.setAcquireRetryDelay(1000); expectLastCall().once(); Connection conn1 = createMock(Connection.class); expect(cpds1.getConnection()).andThrow(new SQLException("fail")); replay(cpds1, ComboPooledDataSource.class); replay(conn1); datasource.getConnection("host", "port", "user", "password"); }