Search in sources :

Example 6 with BaseTransaction

use of org.apache.cayenne.tx.BaseTransaction 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

BaseTransaction (org.apache.cayenne.tx.BaseTransaction)6 ExternalTransaction (org.apache.cayenne.tx.ExternalTransaction)5 Test (org.junit.Test)3 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 Painting (org.apache.cayenne.testdo.testmap.Painting)2 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