Search in sources :

Example 1 with TransactionListener

use of org.apache.cayenne.tx.TransactionListener in project cayenne by apache.

the class ServerRuntimeIT method testPerformInTransaction_Local_Callback.

@Test
public void testPerformInTransaction_Local_Callback() {
    TransactionListener callback = mock(TransactionListener.class);
    Artist a = runtime.performInTransaction(new TransactionalOperation<Artist>() {

        @Override
        public Artist perform() {
            Artist localArtist = runtime.newContext().newObject(Artist.class);
            localArtist.setArtistName("A1");
            localArtist.getObjectContext().commitChanges();
            return localArtist;
        }
    }, callback);
    assertEquals("A1", a.getArtistName());
    assertEquals(PersistenceState.COMMITTED, a.getPersistenceState());
    verify(callback).willCommit(any(Transaction.class));
    verify(callback).willAddConnection(any(Transaction.class), any(String.class), any(Connection.class));
    verify(callback, times(0)).willRollback(any(Transaction.class));
}
Also used : TransactionListener(org.apache.cayenne.tx.TransactionListener) Artist(org.apache.cayenne.testdo.testmap.Artist) Transaction(org.apache.cayenne.tx.Transaction) Connection(java.sql.Connection) Test(org.junit.Test)

Example 2 with TransactionListener

use of org.apache.cayenne.tx.TransactionListener in project cayenne by apache.

the class ServerRuntimeIT method testPerformInTransaction_Local_Callback_Rollback.

@Test
public void testPerformInTransaction_Local_Callback_Rollback() {
    TransactionListener callback = mock(TransactionListener.class);
    try {
        runtime.performInTransaction(new TransactionalOperation<Artist>() {

            @Override
            public Artist perform() {
                Artist localArtist = runtime.newContext().newObject(Artist.class);
                localArtist.getObjectContext().commitChanges();
                return localArtist;
            }
        }, callback);
        fail("Exception expected");
    } catch (ValidationException v) {
        verify(callback).willRollback(any(Transaction.class));
        verify(callback, times(0)).willAddConnection(any(Transaction.class), any(String.class), any(Connection.class));
        verify(callback, times(0)).willCommit(any(Transaction.class));
    }
}
Also used : TransactionListener(org.apache.cayenne.tx.TransactionListener) Artist(org.apache.cayenne.testdo.testmap.Artist) ValidationException(org.apache.cayenne.validation.ValidationException) Test(org.junit.Test)

Aggregations

Artist (org.apache.cayenne.testdo.testmap.Artist)2 TransactionListener (org.apache.cayenne.tx.TransactionListener)2 Test (org.junit.Test)2 Connection (java.sql.Connection)1 Transaction (org.apache.cayenne.tx.Transaction)1 ValidationException (org.apache.cayenne.validation.ValidationException)1