DbSetup { public void launch() { try { Connection connection = destination.getConnection(); try { connection.setAutoCommit(false); operation.execute(connection, binderConfiguration); connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } catch (RuntimeException e) { connection.rollback(); throw e; } finally { connection.close(); } } catch (SQLException e) { throw new DbSetupRuntimeException(e); } } DbSetup(@Nonnull Destination destination, @Nonnull Operation operation);  DbSetup(@Nonnull Destination destination,
                   @Nonnull Operation operation,
                   @Nonnull BinderConfiguration binderConfiguration); void launch(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void launchWorks() throws SQLException { Destination destination = mock(Destination.class); Connection connection = mock(Connection.class); when(destination.getConnection()).thenReturn(connection); Operation operation = mock(Operation.class); DbSetup setup = new DbSetup(destination, operation); setup.launch(); InOrder inOrder = inOrder(destination, connection, operation); inOrder.verify(destination).getConnection(); inOrder.verify(operation).execute(connection, DefaultBinderConfiguration.INSTANCE); inOrder.verify(connection).commit(); }
@Test public void launchWorksWithCustomConfiguration() throws SQLException { Destination destination = mock(Destination.class); Connection connection = mock(Connection.class); when(destination.getConnection()).thenReturn(connection); Operation operation = mock(Operation.class); BinderConfiguration config = mock(BinderConfiguration.class); DbSetup setup = new DbSetup(destination, operation, config); setup.launch(); InOrder inOrder = inOrder(destination, connection, operation); inOrder.verify(destination).getConnection(); inOrder.verify(operation).execute(connection, config); inOrder.verify(connection).commit(); }
@Test public void launchRollbacksIfSQLException() throws SQLException { Destination destination = mock(Destination.class); Connection connection = mock(Connection.class); when(destination.getConnection()).thenReturn(connection); Operation operation = mock(Operation.class); doThrow(new SQLException()).when(operation).execute(connection, DefaultBinderConfiguration.INSTANCE); DbSetup setup = new DbSetup(destination, operation); try { setup.launch(); fail("Expected a DbSetupRuntimeException"); } catch (DbSetupRuntimeException e) { } InOrder inOrder = inOrder(destination, connection, operation); inOrder.verify(destination).getConnection(); inOrder.verify(operation).execute(connection, DefaultBinderConfiguration.INSTANCE); inOrder.verify(connection).rollback(); }
@Test public void launchRollbacksIfOtherException() throws SQLException { Destination destination = mock(Destination.class); Connection connection = mock(Connection.class); when(destination.getConnection()).thenReturn(connection); Operation operation = mock(Operation.class); doThrow(new NullPointerException()).when(operation).execute(connection, DefaultBinderConfiguration.INSTANCE); DbSetup setup = new DbSetup(destination, operation); try { setup.launch(); fail("Expected a DbSetupRuntimeException"); } catch (NullPointerException e) { } InOrder inOrder = inOrder(destination, connection, operation); inOrder.verify(destination).getConnection(); inOrder.verify(operation).execute(connection, DefaultBinderConfiguration.INSTANCE); inOrder.verify(connection).rollback(); }