@Test
public void testGetConnection() throws Exception {

    // first invocation, creates new CPDS
    ComboPooledDataSource cpds1 = createMock(ComboPooledDataSource.class);
    expectNew(ComboPooledDataSource.class).andReturn(cpds1);
    cpds1.setDriverClass("org.postgresql.Driver");
    expectLastCall().once();
    cpds1.setJdbcUrl("jdbc:postgresql://host:port/template1");
    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);
    // second invocation, closes old CPDS, creates a new one
    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://host:port/template1");
    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);
    // third invocation, only return the same connection
    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);
}