Search in sources :

Example 41 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project uPortal by Jasig.

the class PortalRawEventsAggregatorImplTest method aggregateRawEventsIncompleteByReturn.

@Test
public void aggregateRawEventsIncompleteByReturn() throws Exception {
    when(transactionOperations.execute(any(TransactionCallback.class))).then(new Answer<EventProcessingResult>() {

        @Override
        public EventProcessingResult answer(InvocationOnMock invocation) throws Throwable {
            final TransactionStatus status = mock(TransactionStatus.class);
            return ((TransactionCallback<EventProcessingResult>) invocation.getArguments()[0]).doInTransaction(status);
        }
    });
    when(clusterLockService.isLockOwner(PortalRawEventsAggregator.AGGREGATION_LOCK_NAME)).thenReturn(true);
    when(portalEventDimensionPopulator.isCheckedDimensions()).thenReturn(true);
    when(eventAggregationManagementDao.getEventAggregatorStatus(ProcessingType.AGGREGATION, true)).thenReturn(eventAggregatorStatus);
    when(portalInfoProvider.getUniqueServerName()).thenReturn("serverName_abcd");
    when(eventAggregatorStatus.getLastEventDate()).thenReturn(new DateTime(1325881376117l));
    when(portalEventDao.aggregatePortalEvents(any(DateTime.class), any(DateTime.class), (int) any(Integer.TYPE), (Function<PortalEvent, Boolean>) any(Function.class))).then(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return false;
        }
    });
    final EventProcessingResult result = portalEventAggregator.doAggregateRawEvents();
    assertNotNull(result);
    assertEquals(0, result.getProcessed());
    assertEquals(false, result.isComplete());
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) PortalEvent(org.apereo.portal.events.PortalEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransactionStatus(org.springframework.transaction.TransactionStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 42 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project uPortal by Jasig.

the class PortalRawEventsAggregatorImplTest method aggregateRawEventsIncompleteByProcessCount.

@Test
public void aggregateRawEventsIncompleteByProcessCount() throws Exception {
    when(transactionOperations.execute(any(TransactionCallback.class))).then(new Answer<EventProcessingResult>() {

        @Override
        public EventProcessingResult answer(InvocationOnMock invocation) throws Throwable {
            final TransactionStatus status = mock(TransactionStatus.class);
            return ((TransactionCallback<EventProcessingResult>) invocation.getArguments()[0]).doInTransaction(status);
        }
    });
    when(clusterLockService.isLockOwner(PortalRawEventsAggregator.AGGREGATION_LOCK_NAME)).thenReturn(true);
    when(portalEventDimensionPopulator.isCheckedDimensions()).thenReturn(true);
    when(eventAggregationManagementDao.getEventAggregatorStatus(ProcessingType.AGGREGATION, true)).thenReturn(eventAggregatorStatus);
    when(portalInfoProvider.getUniqueServerName()).thenReturn("serverName_abcd");
    when(eventAggregatorStatus.getLastEventDate()).thenReturn(new DateTime(1325881376117l));
    when(portalEventDao.aggregatePortalEvents(any(DateTime.class), any(DateTime.class), (int) any(Integer.TYPE), (Function<PortalEvent, Boolean>) any(Function.class))).then(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            ((Function<PortalEvent, Boolean>) invocation.getArguments()[3]).apply(new MockPortalEvent(this, "serverName", "eventSessionId", person));
            return false;
        }
    });
    when(eventSessionDao.getEventSession(any(PortalEvent.class))).thenReturn(eventSession);
    this.portalEventAggregator.setEventAggregationBatchSize(1);
    final EventProcessingResult result = portalEventAggregator.doAggregateRawEvents();
    assertNotNull(result);
    assertEquals(1, result.getProcessed());
    assertEquals(false, result.isComplete());
    this.portalEventAggregator.setEventAggregationBatchSize(1000);
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) PortalEvent(org.apereo.portal.events.PortalEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransactionStatus(org.springframework.transaction.TransactionStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 43 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testTransactionRollbackWithPropagationSupports.

@Test
public void testTransactionRollbackWithPropagationSupports() {
    given(manager.isOpen()).willReturn(true);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            assertTrue(!TransactionSynchronizationManager.hasResource(factory));
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue(!status.isNewTransaction());
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            status.setRollbackOnly();
            return null;
        }
    });
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 44 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testTransactionCommitWithPrebound.

@Test
public void testTransactionCommitWithPrebound() {
    given(manager.getTransaction()).willReturn(tx);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
    try {
        Object result = tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
                return l;
            }
        });
        assertSame(l, result);
        assertTrue(TransactionSynchronizationManager.hasResource(factory));
        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    } finally {
        TransactionSynchronizationManager.unbindResource(factory);
    }
    verify(tx).begin();
    verify(tx).commit();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 45 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.

the class JpaTransactionManagerTests method testTransactionCommitWithDataSource.

@Test
public void testTransactionCommitWithDataSource() throws SQLException {
    DataSource ds = mock(DataSource.class);
    tm.setDataSource(ds);
    given(manager.getTransaction()).willReturn(tx);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    Object result = tt.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.hasResource(factory));
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            return l;
        }
    });
    assertTrue(result == l);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(tx).commit();
    verify(manager).flush();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)359 Test (org.junit.Test)186 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)154 TransactionCallback (org.springframework.transaction.support.TransactionCallback)91 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)72 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 List (java.util.List)28 ArrayList (java.util.ArrayList)27 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)21 Date (java.util.Date)18 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11