@Test
  public void shouldThrowNotInTransactionExceptionWhenNotInTransaction() throws Exception {
    // Given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);
    when(txManager.getKernelTransaction()).thenReturn(null);
    ThreadToStatementContextBridge bridge = new ThreadToStatementContextBridge(null, txManager);

    // When
    try {
      bridge.statement();
      fail("Should throw");
    } catch (NotInTransactionException e) { // Good
    }
  }
示例#2
0
  @Test
  public void shouldRollbackFailingStatement() throws Exception {
    // given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);

    javax.transaction.Transaction existingTransaction = mock(javax.transaction.Transaction.class);
    when(txManager.suspend()).thenReturn(existingTransaction);

    StatementOperationParts operations = mock(StatementOperationParts.class);
    KernelStatement statement = mock(KernelStatement.class);
    StubKernelTransaction kernelTransaction = spy(new StubKernelTransaction(operations, statement));
    when(txManager.getKernelTransaction()).thenReturn(kernelTransaction);

    @SuppressWarnings("unchecked")
    Transactor.Work<Object, KernelException> work = mock(Transactor.Work.class);
    SpecificKernelException exception = new SpecificKernelException();
    when(work.perform(any(StatementOperationParts.class), any(KernelStatement.class)))
        .thenThrow(exception);

    Transactor transactor = new Transactor(txManager);

    // when
    try {
      transactor.execute(work);

      fail("expected exception");
    }
    // then
    catch (SpecificKernelException e) {
      assertSame(exception, e);
    }
    InOrder order = inOrder(txManager, kernelTransaction, operations, statement, work);
    order.verify(txManager).suspend();
    order.verify(txManager).begin();
    order.verify(txManager).getKernelTransaction();
    order.verify(kernelTransaction).newStatement();
    order.verify(work).perform(operations, statement);
    order.verify(statement).close();
    order.verify(kernelTransaction).rollback();
    order.verify(txManager).resume(existingTransaction);
    order.verifyNoMoreInteractions();
  }
示例#3
0
  @Test
  public void shouldCommitSuccessfulStatement() throws Exception {
    // given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);

    javax.transaction.Transaction existingTransaction = mock(javax.transaction.Transaction.class);
    when(txManager.suspend()).thenReturn(existingTransaction);

    StatementOperationParts operations = mock(StatementOperationParts.class);
    KernelStatement statement = mock(KernelStatement.class);
    StubKernelTransaction kernelTransaction = spy(new StubKernelTransaction(operations, statement));
    when(txManager.getKernelTransaction()).thenReturn(kernelTransaction);

    @SuppressWarnings("unchecked")
    Transactor.Work<Object, KernelException> work = mock(Transactor.Work.class);
    Object expectedResult = new Object();
    when(work.perform(eq(operations), any(KernelStatement.class))).thenReturn(expectedResult);

    Transactor transactor = new Transactor(txManager);

    // when
    Object result = transactor.execute(work);

    // then
    assertEquals(expectedResult, result);
    InOrder order = inOrder(txManager, kernelTransaction, statement, work);
    order.verify(txManager).suspend();
    order.verify(txManager).begin();
    order.verify(txManager).getKernelTransaction();
    order.verify(kernelTransaction).newStatement();
    order.verify(work).perform(operations, statement);
    order.verify(statement).close();
    order.verify(kernelTransaction).commit();
    order.verify(txManager).resume(existingTransaction);
    order.verifyNoMoreInteractions();
  }