Java Code Examples for org.springframework.transaction.support.SimpleTransactionStatus

The following examples show how to use org.springframework.transaction.support.SimpleTransactionStatus. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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());

	assertFalse("callback should not have been invoked", instance.invoked());
	TransactionContextHolder.removeCurrentTransactionContext();
	listener.beforeTestMethod(testContext);
	assertFalse("callback should not have been invoked", instance.invoked());
	listener.afterTestMethod(testContext);
	assertTrue("callback should have been invoked", instance.invoked());
}
 
Example 2
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());

	assertFalse("callback should not have been invoked", instance.invoked());
	TransactionContextHolder.removeCurrentTransactionContext();
	listener.beforeTestMethod(testContext);
	assertFalse("callback should not have been invoked", instance.invoked());
	listener.afterTestMethod(testContext);
	assertTrue("callback should have been invoked", instance.invoked());
}
 
Example 3
private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
	BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
	Invocable instance = clazz.newInstance();
	given(testContext.getTestInstance()).willReturn(instance);
	given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));

	given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus());

	assertFalse(instance.invoked);
	TransactionContextHolder.removeCurrentTransactionContext();
	listener.beforeTestMethod(testContext);
	listener.afterTestMethod(testContext);
	assertTrue(instance.invoked);
}
 
Example 4
Source Project: gocd   Source File: PipelineStateDaoCachingTest.java    License: Apache License 2.0 5 votes vote down vote up
private void setupTransactionTemplate(List<TransactionSynchronizationAdapter> transactionSynchronizationAdapters) {
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
            callback.doInTransaction(new SimpleTransactionStatus());
            for (TransactionSynchronizationAdapter synchronizationAdapter : transactionSynchronizationAdapters) {
                synchronizationAdapter.afterCommit();
            }
            return null;
        }
    });
}
 
Example 5
Source Project: gocd   Source File: PipelineStateDaoTest.java    License: Apache License 2.0 5 votes vote down vote up
@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()));
    }
}
 
Example 6
Source Project: gocd   Source File: PipelineStateDaoTest.java    License: Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 7
Source Project: gocd   Source File: DataSharingSettingsServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    initMocks(this);
    sharingSettingsService = new DataSharingSettingsService(dataSharingSettingsSqlMapDao, transactionTemplate,
            transactionSynchronizationManager, entityHashingService);

    when(transactionTemplate.execute(any(TransactionCallback.class))).then(invocation -> {
        ((TransactionCallback) invocation.getArguments()[0]).doInTransaction(new SimpleTransactionStatus());
        return null;
    });
}
 
Example 8
@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
	this.definition = definition;
	this.status = new SimpleTransactionStatus();
	return callback.doInTransaction(this.status);
}
 
Example 9
@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
	this.definition = definition;
	this.status = new SimpleTransactionStatus();
	return callback.doInTransaction(this.status);
}
 
Example 10
Source Project: micronaut-spring   Source File: MockTransactionManager.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
    return new SimpleTransactionStatus();
}
 
Example 11
Source Project: Spring-5.0-Cookbook   Source File: TransactionManager.java    License: MIT License 4 votes vote down vote up
@Override
public TransactionStatus getTransaction(TransactionDefinition definition)
                    throws TransactionException {
      return new SimpleTransactionStatus();
}
 
Example 12
@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
	this.definition = definition;
	this.status = new SimpleTransactionStatus();
	return callback.doInTransaction(this.status);
}