org.springframework.transaction.support.SimpleTransactionStatus Java Examples

The following examples show how to use org.springframework.transaction.support.SimpleTransactionStatus. 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
Source File: TransactionalTestExecutionListenerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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
Source File: TransactionalTestExecutionListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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
Source File: TransactionalTestExecutionListenerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 File: PipelineStateDaoCachingTest.java    From gocd with 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 File: PipelineStateDaoTest.java    From gocd with 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 File: PipelineStateDaoTest.java    From gocd with 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 File: DataSharingSettingsServiceTest.java    From gocd with 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
Source File: MockCallbackPreferringTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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
Source File: MockCallbackPreferringTransactionManager.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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 File: MockTransactionManager.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
    return new SimpleTransactionStatus();
}
 
Example #11
Source File: TransactionManager.java    From Spring-5.0-Cookbook with MIT License 4 votes vote down vote up
@Override
public TransactionStatus getTransaction(TransactionDefinition definition)
                    throws TransactionException {
      return new SimpleTransactionStatus();
}
 
Example #12
Source File: MockCallbackPreferringTransactionManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
	this.definition = definition;
	this.status = new SimpleTransactionStatus();
	return callback.doInTransaction(this.status);
}