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); }
@Override public TransactionOperations getExecuteTransactionOperations() { return new TransactionOperations() { @Override public <T2> T2 execute(TransactionCallback<T2> action) throws TransactionException { return action.doInTransaction(new SimpleTransactionStatus()); } }; }
@Override public <T> T execute(TransactionCallback<T> action) throws TransactionException { try { entityManagerUtils.openEntityManager(); // We do not call super() here, no transaction will be opened. // Note : it doesn't exist any transaction manager which doesn't open transaction while // keeping entity manager open. return action.doInTransaction(new SimpleTransactionStatus()); } finally { entityManagerUtils.closeEntityManager(); } }
private void setupTransactionManager() { 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)); //anyTimes(); expectLastCall().anyTimes(); }
@Test public 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) throws Throwable { 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())); } }
@Test public 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) throws Throwable { 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)); } }
@Override public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { return new SimpleTransactionStatus(); }
@Override public <T> T execute(TransactionCallback<T> action) throws TransactionException { return action.doInTransaction(new SimpleTransactionStatus()); }
@Override public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException { this.definition = definition; this.status = new SimpleTransactionStatus(); return callback.doInTransaction(this.status); }
@Override public Object execute(TransactionDefinition definition, TransactionCallback callback) throws TransactionException { this.definition = definition; this.status = new SimpleTransactionStatus(); return callback.doInTransaction(this.status); }
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { return new SimpleTransactionStatus(); }
@Override public <T> T execute(TransactionCallback<T> action) throws TransactionException { return action.doInTransaction(new SimpleTransactionStatus(true)); }
public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) throws TransactionException { committed = false; return new SimpleTransactionStatus(true); }