Search in sources :

Example 1 with SimpleTransactionStatus

use of org.springframework.transaction.support.SimpleTransactionStatus in project cucumber-jvm by cucumber.

the class SpringTransactionHooksTest method shouldObtainOrStartTransactionInBeforeHook.

@Test
public void shouldObtainOrStartTransactionInBeforeHook() {
    final SimpleTransactionStatus dummyTxStatus = new SimpleTransactionStatus();
    when(mockedPlatformTransactionManager.getTransaction(isA(TransactionDefinition.class))).thenReturn(dummyTxStatus);
    target.startTransaction();
    assertSame(target.getTransactionStatus(), dummyTxStatus);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.Test)

Example 2 with SimpleTransactionStatus

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

the class TransactionalTestExecutionListenerTests method assertAfterTestMethodWithTransactionalTestMethod.

private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
    BDDMockito.<Class<?>>given(testContext.getTestClass()).willReturn(clazz);
    Invocable instance = BeanUtils.instantiateClass(clazz);
    given(testContext.getTestInstance()).willReturn(instance);
    given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
    given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus());
    assertThat(instance.invoked()).as("callback should not have been invoked").isFalse();
    TransactionContextHolder.removeCurrentTransactionContext();
    listener.beforeTestMethod(testContext);
    assertThat(instance.invoked()).as("callback should not have been invoked").isFalse();
    listener.afterTestMethod(testContext);
    assertThat(instance.invoked()).as("callback should have been invoked").isTrue();
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus)

Example 3 with SimpleTransactionStatus

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

the class PipelineStateDaoTest method shouldNotCorruptCacheIfSaveFailsWhileUnLocking.

@Test
void shouldNotCorruptCacheIfSaveFailsWhileUnLocking() {
    String pipelineName = UUID.randomUUID().toString();
    PipelineState pipelineState = new PipelineState(pipelineName);
    long lockedByPipelineId = 1;
    pipelineState.lock(lockedByPipelineId);
    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.unlockPipeline(pipelineName);
        fail("save should have thrown an exception!");
    } catch (Exception e) {
        PipelineState stateFromCache = (PipelineState) goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName));
        assertThat(stateFromCache.isLocked(), is(true));
        assertThat(stateFromCache.getLockedByPipelineId(), is(lockedByPipelineId));
    }
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleTransactionStatus

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

the class PipelineSqlMapDaoCachingTest method shouldInvalidateCacheOfPipelineInstancesTriggeredWithDependencyMaterial.

@Test
void shouldInvalidateCacheOfPipelineInstancesTriggeredWithDependencyMaterial() {
    String cacheKey = pipelineDao.cacheKeyForPipelineInstancesTriggeredWithDependencyMaterial("p1", "p", 1);
    when(mockTemplate.queryForList(eq("pipelineInstancesTriggeredOutOfDependencyMaterial"), anyString())).thenReturn(new ArrayList());
    List<PipelineIdentifier> actual = pipelineDao.getPipelineInstancesTriggeredWithDependencyMaterial("p1", new PipelineIdentifier("p", 1));
    assertThat(actual).hasSize(0);
    assertThat((List<PipelineIdentifier>) goCache.get(cacheKey)).hasSize(0);
    MaterialRevisions materialRevisions = new MaterialRevisions(new MaterialRevision(new DependencyMaterial(new CaseInsensitiveString("p"), new CaseInsensitiveString("s")), new Modification("u", "comment", "email", new Date(), "p/1/s/1")));
    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 : Modification(com.thoughtworks.go.domain.materials.Modification) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallback(org.springframework.transaction.support.TransactionCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleTransactionStatus

use of org.springframework.transaction.support.SimpleTransactionStatus in project cucumber-jvm by cucumber.

the class SpringTransactionHooksTest method shouldTriggerTransactionRollbackInAfterHook.

@Test
public void shouldTriggerTransactionRollbackInAfterHook() {
    final SimpleTransactionStatus dummyTxStatus = new SimpleTransactionStatus();
    target.setTransactionStatus(dummyTxStatus);
    target.rollBackTransaction();
    verify(mockedPlatformTransactionManager).rollback(dummyTxStatus);
}
Also used : SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.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