### Question:
AbstractPoolAdapter implements PoolAdapter<T> { @Override public Connection getConnection(ConnectionRequestContext requestContext) throws SQLException { long startNanos = System.nanoTime(); try { Credentials credentials = requestContext.getCredentials(); return (credentials == null) ? targetDataSource.getConnection() : targetDataSource.getConnection(credentials.getUsername(), credentials.getPassword()); } catch (SQLException e) { throw translateException(e); } catch (RuntimeException e) { throw translateException(e); } finally { long endNanos = System.nanoTime(); connectionAcquireTimer.update(TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos), TimeUnit.MILLISECONDS); } } AbstractPoolAdapter(ConfigurationProperties<T, Metrics, PoolAdapter<T>> configurationProperties); @Override T getTargetDataSource(); @Override Connection getConnection(ConnectionRequestContext requestContext); static final String CONNECTION_ACQUIRE_MILLIS; }### Answer:
@Test public void testGetConnectionWithoutCredentials() throws SQLException { ConnectionRequestContext connectionRequestContext = new ConnectionRequestContext.Builder().build(); when(dataSource.getConnection()).thenReturn(connection); assertSame(connection, poolAdapter.getConnection(connectionRequestContext)); verify(timer, times(1)).update(anyLong(), eq(TimeUnit.MILLISECONDS)); }
@Test public void testGetConnectionWithCredentials() throws SQLException { ConnectionRequestContext connectionRequestContext = new ConnectionRequestContext.Builder() .setCredentials(new Credentials("username", "password")).build(); when(dataSource.getConnection(eq("username"), eq("password"))).thenReturn(connection); assertSame(connection, poolAdapter.getConnection(connectionRequestContext)); verify(timer, times(1)).update(anyLong(), eq(TimeUnit.MILLISECONDS)); }
@Test public void testGetConnectionWithoutCredentialsThrowsSQLException() throws SQLException { ConnectionRequestContext connectionRequestContext = new ConnectionRequestContext.Builder().build(); when(dataSource.getConnection()).thenThrow(new SQLException()); try { poolAdapter.getConnection(connectionRequestContext); fail("Should have thrown SQLException"); } catch (SQLException e) { verify(timer, times(1)).update(anyLong(), eq(TimeUnit.MILLISECONDS)); } }
@Test public void testGetConnectionWithoutCredentialsThrowsRuntimeException() throws SQLException { ConnectionRequestContext connectionRequestContext = new ConnectionRequestContext.Builder().build(); when(dataSource.getConnection()).thenThrow(new RuntimeException()); try { poolAdapter.getConnection(connectionRequestContext); fail("Should have thrown SQLException"); } catch (SQLException e) { verify(timer, times(1)).update(anyLong(), eq(TimeUnit.MILLISECONDS)); } }
@Test public void testGetConnectionThrowsTimeoutException() throws SQLException { ConnectionRequestContext connectionRequestContext = new ConnectionRequestContext.Builder().build(); when(dataSource.getConnection()).thenThrow(new ConnectionTimedOutException()); try { poolAdapter.getConnection(connectionRequestContext); fail("Should have thrown SQLException"); } catch (SQLException e) { verify(timer, times(1)).update(anyLong(), eq(TimeUnit.MILLISECONDS)); if (supportsTimeoutExceptionTranslation()) { ConnectionAcquireTimeoutEvent connectionAcquireTimeoutEvent = eventListener.event; assertNotNull(connectionAcquireTimeoutEvent); assertEquals(getClass().getName(), connectionAcquireTimeoutEvent.getUniqueName()); } } }