Search in sources :

Example 6 with SimpleTransactionStatus

use of org.springframework.transaction.support.SimpleTransactionStatus in project opennms by OpenNMS.

the class CollectdTest method setupTransactionManager.

/**
 * Add a dummy transaction manager that has mock calls to commit() and rollback()
 */
private void setupTransactionManager() {
    PlatformTransactionManager m_transactionManager = m_easyMockUtils.createMock(PlatformTransactionManager.class);
    TransactionTemplate transactionTemplate = new TransactionTemplate(m_transactionManager);
    m_collectd.setTransactionTemplate(transactionTemplate);
    expect(m_transactionManager.getTransaction(isA(TransactionDefinition.class))).andReturn(new SimpleTransactionStatus()).anyTimes();
    m_transactionManager.rollback(isA(TransactionStatus.class));
    expectLastCall().anyTimes();
    m_transactionManager.commit(isA(TransactionStatus.class));
    expectLastCall().anyTimes();
}
Also used : MockTransactionTemplate(org.opennms.netmgt.dao.mock.MockTransactionTemplate) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 7 with SimpleTransactionStatus

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

the class MockCallbackPreferringTransactionManager method execute.

@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
    this.definition = definition;
    this.status = new SimpleTransactionStatus();
    return callback.doInTransaction(this.status);
}
Also used : SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus)

Example 8 with SimpleTransactionStatus

use of org.springframework.transaction.support.SimpleTransactionStatus in project gocd by gocd.

the class PipelineSqlMapDaoCachingTest method shouldInvalidateCacheOfPipelineInstancesTriggeredWithMaterialRevision.

@Test
void shouldInvalidateCacheOfPipelineInstancesTriggeredWithMaterialRevision() {
    GitMaterialInstance materialInstance = new GitMaterialInstance("url", null, "branch", "submodule", "flyweight");
    String cacheKey = pipelineDao.cacheKeyForPipelineInstancesTriggeredWithDependencyMaterial("p1", materialInstance.getFingerprint(), "r1");
    List<PipelineIdentifier> result = Arrays.asList(new PipelineIdentifier("p1", 1, "1"));
    when(mockTemplate.queryForList(eq("pipelineInstancesTriggeredOffOfMaterialRevision"), any())).thenReturn((List) result);
    List<PipelineIdentifier> actual = pipelineDao.getPipelineInstancesTriggeredWithDependencyMaterial("p1", materialInstance, "r1");
    assertThat(actual).hasSize(1);
    assertThat((List<PipelineIdentifier>) goCache.get(cacheKey)).hasSize(1);
    MaterialRevisions materialRevisions = new MaterialRevisions(new MaterialRevision(new GitMaterial("url", "branch"), new Modification("user", "comment", "email", new Date(), "r1")));
    Pipeline pipeline = new Pipeline("p1", BuildCause.createWithModifications(materialRevisions, ""));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ((TransactionSynchronizationAdapter) invocation.getArguments()[0]).afterCommit();
            return null;
        }
    }).when(transactionSynchronizationManager).registerSynchronization(any(TransactionSynchronization.class));
    when(transactionTemplate.execute(any(TransactionCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ((TransactionCallback) invocation.getArguments()[0]).doInTransaction(new SimpleTransactionStatus());
            return null;
        }
    });
    pipelineDao.save(pipeline);
    assertThat(goCache.get(cacheKey)).isNull();
}
Also used : GitMaterialInstance(com.thoughtworks.go.domain.materials.git.GitMaterialInstance) Modification(com.thoughtworks.go.domain.materials.Modification) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallback(org.springframework.transaction.support.TransactionCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.jupiter.api.Test)

Example 9 with SimpleTransactionStatus

use of org.springframework.transaction.support.SimpleTransactionStatus in project gocd by gocd.

the class PipelineStateDaoTest method shouldNotCorruptCacheIfSaveFailsWhileLocking.

@Test
void shouldNotCorruptCacheIfSaveFailsWhileLocking() {
    String pipelineName = UUID.randomUUID().toString();
    Pipeline pipeline = PipelineMother.pipeline(pipelineName, new Stage());
    PipelineState pipelineState = new PipelineState(pipelineName);
    goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipelineName), pipelineState);
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
            callback.doInTransaction(new SimpleTransactionStatus());
            return null;
        }
    });
    doThrow(new RuntimeException("could not save!")).when(session).saveOrUpdate(any(PipelineState.class));
    try {
        pipelineStateDao.lockPipeline(pipeline);
        fail("save should have thrown an exception!");
    } catch (Exception e) {
        PipelineState stateFromCache = (PipelineState) goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName));
        assertThat(stateFromCache.isLocked(), is(false));
        assertThat(stateFromCache.getLockedByPipelineId(), is(0L));
        assertThat(stateFromCache.getLockedBy(), is(nullValue()));
    }
}
Also used : Pipeline(com.thoughtworks.go.domain.Pipeline) PipelineState(com.thoughtworks.go.domain.PipelineState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Stage(com.thoughtworks.go.domain.Stage) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)9 Test (org.junit.jupiter.api.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)2 PipelineState (com.thoughtworks.go.domain.PipelineState)2 Modification (com.thoughtworks.go.domain.materials.Modification)2 Test (org.junit.Test)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2 TransactionCallback (org.springframework.transaction.support.TransactionCallback)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)1 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)1 Pipeline (com.thoughtworks.go.domain.Pipeline)1 Stage (com.thoughtworks.go.domain.Stage)1 GitMaterialInstance (com.thoughtworks.go.domain.materials.git.GitMaterialInstance)1 MockTransactionTemplate (org.opennms.netmgt.dao.mock.MockTransactionTemplate)1 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1