/** * Start a new transaction for the configured test context. * <p>Only call this method if {@link #endTransaction} has been called or if no * transaction has been previously started. * @throws TransactionException if starting the transaction fails */ void startTransaction() { Assert.state(this.transactionStatus == null, "Cannot start a new transaction without ending the existing transaction first"); this.flaggedForRollback = this.defaultRollback; this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition); int transactionsStarted = this.transactionsStarted.incrementAndGet(); if (logger.isInfoEnabled()) { logger.info(String.format( "Began transaction (%s) for test context %s; transaction manager [%s]; rollback [%s]", transactionsStarted, this.testContext, this.transactionManager, this.flaggedForRollback)); } }
protected void startNewTransaction() throws TransactionException { this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition); }
@Override public TransactionStatus getTransaction(@Nullable TransactionDefinition definition) { return delegate.getTransaction(definition); }
public void rollback() { // Just in case the rollback isn't triggered by an // exception, we mark the current transaction rollBackOnly. transactionManager.getTransaction(null).setRollbackOnly(); }
if (txAttr != null) { if (tm != null) { status = tm.getTransaction(txAttr);
@Override public V call() throws Exception { transactionStatus = transactionTemplate.getTransactionManager().getTransaction(transactionTemplate); if (rollback) { transactionStatus.setRollbackOnly(); } return target.call(); } }
@Override @Nullable public <T> T execute(TransactionCallback<T> action) throws TransactionException { Assert.state(this.transactionManager != null, "No PlatformTransactionManager set"); if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) { return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action); } else { TransactionStatus status = this.transactionManager.getTransaction(this); T result; try { result = action.doInTransaction(status); } catch (RuntimeException | Error ex) { // Transactional code threw application exception -> rollback rollbackOnException(status, ex); throw ex; } catch (Throwable ex) { // Transactional code threw unexpected exception -> rollback rollbackOnException(status, ex); throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception"); } this.transactionManager.commit(status); return result; } }
TransactionStatus status = this.transactionManager.getTransaction(this.transactionDefinition); boolean messageReceived; try {
@Test public void noExistingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(false, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertFalse("Must not have transaction", status1.hasTransaction()); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.hasTransaction()); assertTrue("Must be new transaction", status2.isNewTransaction()); try { tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); fail("Should not have thrown NoTransactionException"); } catch (IllegalTransactionStateException ex) { // expected } }
@Test public void putTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); Object key = new Object(); cache.put(key, "123"); assertNull(target.get(key)); this.txManager.commit(status); assertEquals("123", target.get(key, String.class)); }
@Test public void existingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(true, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertTrue("Must have transaction", status1.getTransaction() != null); assertTrue("Must not be new transaction", !status1.isNewTransaction()); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.getTransaction() != null); assertTrue("Must not be new transaction", !status2.isNewTransaction()); try { DefaultTransactionStatus status3 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); assertTrue("Must have transaction", status3.getTransaction() != null); assertTrue("Must not be new transaction", !status3.isNewTransaction()); } catch (NoTransactionException ex) { fail("Should not have thrown NoTransactionException"); } }
@Test public void evictTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.evict(key); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); }
@Test public void clearTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.clear(); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); } }
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()); }
/** * Test that TransactionStatus.setRollbackOnly works. */ @Test public void programmaticRollback() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); Method m = getNameMethod; MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(m, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); given(ptm.getTransaction(txatt)).willReturn(status); final String name = "jenny"; TestBean tb = new TestBean() { @Override public String getName() { TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus(); txStatus.setRollbackOnly(); return name; } }; ITestBean itb = (ITestBean) advised(tb, ptm, tas); // verification!? assertTrue(name.equals(itb.getName())); verify(ptm).commit(status); }
/** * Simulate a transaction infrastructure failure. * Shouldn't invoke target method. */ @Test public void cannotCreateTransaction() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); Method m = getNameMethod; MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(m, txatt); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // Expect a transaction CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null); given(ptm.getTransaction(txatt)).willThrow(ex); TestBean tb = new TestBean() { @Override public String getName() { throw new UnsupportedOperationException( "Shouldn't have invoked target method when couldn't create transaction for transactional method"); } }; ITestBean itb = (ITestBean) advised(tb, ptm, tas); try { itb.getName(); fail("Shouldn't have invoked method"); } catch (CannotCreateTransactionException thrown) { assertTrue(thrown == ex); } }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceedWithNotNew() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); // verification!? itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
/** * Check that two transactions are created and committed. */ @Test public void twoTransactionsShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource(); tas1.register(getNameMethod, txatt); MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource(); tas2.register(setNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] {tas1, tas2}); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); itb.setName("myName"); checkTransactionStatus(false); verify(ptm, times(2)).commit(status); }
given(ptm.getTransaction(txatt)).willReturn(status); UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null); willThrow(ex).given(ptm).commit(status);