Search in sources :

Example 1 with TransactionalOperation

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

the class ServerRuntimeTest method testPerformInTransaction.

@Test
public void testPerformInTransaction() {
    final BaseTransaction tx = mock(BaseTransaction.class);
    final TransactionFactory txFactory = mock(TransactionFactory.class);
    when(txFactory.createTransaction()).thenReturn(tx);
    Module module = binder -> binder.bind(TransactionFactory.class).toInstance(txFactory);
    ServerRuntime runtime = ServerRuntime.builder().addConfig("xxxx").addModule(module).build();
    try {
        final Object expectedResult = new Object();
        Object result = runtime.performInTransaction(new TransactionalOperation<Object>() {

            public Object perform() {
                assertSame(tx, BaseTransaction.getThreadTransaction());
                return expectedResult;
            }
        });
        assertSame(expectedResult, result);
    } finally {
        runtime.shutdown();
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) EventManager(org.apache.cayenne.event.EventManager) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Key(org.apache.cayenne.di.Key) Mockito.mock(org.mockito.Mockito.mock) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Aggregations

Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 DataChannel (org.apache.cayenne.DataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 QueryResponse (org.apache.cayenne.QueryResponse)1 DataContext (org.apache.cayenne.access.DataContext)1 Constants (org.apache.cayenne.configuration.Constants)1 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)1 Key (org.apache.cayenne.di.Key)1 Module (org.apache.cayenne.di.Module)1 EventManager (org.apache.cayenne.event.EventManager)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 EntityResolver (org.apache.cayenne.map.EntityResolver)1 Query (org.apache.cayenne.query.Query)1 BaseTransaction (org.apache.cayenne.tx.BaseTransaction)1 TransactionFactory (org.apache.cayenne.tx.TransactionFactory)1 TransactionalOperation (org.apache.cayenne.tx.TransactionalOperation)1