Search in sources :

Example 36 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.

the class ChainedTransactionManagerTests method shouldThrowExceptionOnFailingRollback.

@Test(expected = UnexpectedRollbackException.class)
public void shouldThrowExceptionOnFailingRollback() throws Exception {
    PlatformTransactionManager first = createFailingTransactionManager("first");
    setupTransactionManagers(first);
    createAndRollbackTransaction();
}
Also used : TestPlatformTransactionManager(org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 37 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method transactionShouldSucceedWithNotNew.

/**
	 * 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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 38 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method cannotCreateTransaction.

/**
	 * 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);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 39 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method cannotCommitTransaction.

/**
	 * Simulate failure of the underlying transaction infrastructure to commit.
	 * Check that the target method was invoked, but that the transaction
	 * infrastructure exception was thrown to the client
	 */
@Test
public void cannotCommitTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = setNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    // Method m2 = getNameMethod;
    // No attributes for m2
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    TransactionStatus status = mock(TransactionStatus.class);
    given(ptm.getTransaction(txatt)).willReturn(status);
    UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
    willThrow(ex).given(ptm).commit(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    String name = "new name";
    try {
        itb.setName(name);
        fail("Shouldn't have succeeded");
    } catch (UnexpectedRollbackException thrown) {
        assertTrue(thrown == ex);
    }
    // Should have invoked target and changed name
    assertTrue(itb.getName() == name);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 40 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method twoTransactionsShouldSucceed.

/**
	 * 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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)54 Test (org.junit.Test)34 TransactionStatus (org.springframework.transaction.TransactionStatus)13 ITestBean (org.springframework.tests.sample.beans.ITestBean)10 TestBean (org.springframework.tests.sample.beans.TestBean)10 BeanFactory (org.springframework.beans.factory.BeanFactory)7 Method (java.lang.reflect.Method)6 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)6 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)6 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)6 TestPlatformTransactionManager (org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)4 TransactionException (org.springframework.transaction.TransactionException)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 HashMap (java.util.HashMap)3 DataSource (javax.sql.DataSource)3 Before (org.junit.Before)3 MockPlatformTransactionManager (org.opennms.core.test.MockPlatformTransactionManager)3 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)3