### Question:
RetryingConnectionFactory implements ConnectionFactory { @Override public Connection openConnection(JdbcIdentity identity) throws SQLException { try { return Failsafe.with(RETRY_POLICY) .get(() -> delegate.openConnection(identity)); } catch (FailsafeException ex) { if (ex.getCause() instanceof SQLException) { throw (SQLException) ex.getCause(); } throw ex; } } RetryingConnectionFactory(ConnectionFactory delegate); @Override Connection openConnection(JdbcIdentity identity); @Override void close();  }### Answer:
@Test public void testSimplyReturnConnection() throws Exception { MockConnectorFactory mock = new MockConnectorFactory(RETURN); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertNotNull(factory.openConnection(IDENTITY)); assertEquals(mock.getCallCount(), 1); }
@Test public void testRetryAndStopOnPrestoException() { MockConnectorFactory mock = new MockConnectorFactory(THROW_SQL_RECOVERABLE_EXCEPTION, THROW_PRESTO_EXCEPTION); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertThatThrownBy(() -> factory.openConnection(IDENTITY)) .isInstanceOf(PrestoException.class) .hasMessage("Testing presto exception"); assertEquals(mock.getCallCount(), 2); }
@Test public void testRetryAndStopOnSqlException() { MockConnectorFactory mock = new MockConnectorFactory(THROW_SQL_RECOVERABLE_EXCEPTION, THROW_SQL_EXCEPTION); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertThatThrownBy(() -> factory.openConnection(IDENTITY)) .isInstanceOf(SQLException.class) .hasMessage("Testing sql exception"); assertEquals(mock.getCallCount(), 2); }
@Test public void testNullPointerException() { MockConnectorFactory mock = new MockConnectorFactory(THROW_NPE); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertThatThrownBy(() -> factory.openConnection(IDENTITY)) .isInstanceOf(NullPointerException.class) .hasMessage("Testing NPE"); assertEquals(mock.getCallCount(), 1); }
@Test public void testRetryAndReturn() throws Exception { MockConnectorFactory mock = new MockConnectorFactory(THROW_SQL_RECOVERABLE_EXCEPTION, RETURN); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertNotNull(factory.openConnection(IDENTITY)); assertEquals(mock.getCallCount(), 2); }
@Test public void testRetryOnWrappedAndReturn() throws Exception { MockConnectorFactory mock = new MockConnectorFactory(THROW_WRAPPED_SQL_RECOVERABLE_EXCEPTION, RETURN); ConnectionFactory factory = new RetryingConnectionFactory(mock); assertNotNull(factory.openConnection(IDENTITY)); assertEquals(mock.getCallCount(), 2); }