Search in sources :

Example 41 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method transactionShouldSucceed.

/**
	 * 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);
}
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 42 with PlatformTransactionManager

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

the class BeanFactoryTransactionTests method doTestGetsAreNotTransactional.

private void doTestGetsAreNotTransactional(final ITestBean testBean) {
    // Install facade
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    PlatformTransactionManagerFacade.delegate = ptm;
    assertTrue("Age should not be " + testBean.getAge(), testBean.getAge() == 666);
    // Expect no methods
    verifyZeroInteractions(ptm);
    // Install facade expecting a call
    final TransactionStatus ts = mock(TransactionStatus.class);
    ptm = new PlatformTransactionManager() {

        private boolean invoked;

        @Override
        public TransactionStatus getTransaction(TransactionDefinition def) throws TransactionException {
            if (invoked) {
                throw new IllegalStateException("getTransaction should not get invoked more than once");
            }
            invoked = true;
            if (!(def.getName().contains(DerivedTestBean.class.getName()) && def.getName().contains("setAge"))) {
                throw new IllegalStateException("transaction name should contain class and method name: " + def.getName());
            }
            return ts;
        }

        @Override
        public void commit(TransactionStatus status) throws TransactionException {
            assertTrue(status == ts);
        }

        @Override
        public void rollback(TransactionStatus status) throws TransactionException {
            throw new IllegalStateException("rollback should not get invoked");
        }
    };
    PlatformTransactionManagerFacade.delegate = ptm;
    // TODO same as old age to avoid ordering effect for now
    int age = 666;
    testBean.setAge(age);
    assertTrue(testBean.getAge() == age);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionException(org.springframework.transaction.TransactionException) TransactionStatus(org.springframework.transaction.TransactionStatus) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 43 with PlatformTransactionManager

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

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierSeveralTimes.

@Test
public void determineTransactionManagerWithQualifierSeveralTimes() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
    PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual);
    // Call again, should be cached
    PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual2);
    verify(beanFactory, times(1)).containsBean("fooTransactionManager");
    verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 44 with PlatformTransactionManager

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

the class TransactionInterceptorTests method determineTransactionManagerWithNoBeanFactoryAndNoTransactionAttribute.

@Test
public void determineTransactionManagerWithNoBeanFactoryAndNoTransactionAttribute() {
    PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
    TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, null);
    assertSame(transactionManager, ti.determineTransactionManager(null));
}
Also used : PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 45 with PlatformTransactionManager

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

the class TransactionInterceptorTests method determineTransactionManagerWithBeanNameSeveralTimes.

@Test
public void determineTransactionManagerWithBeanNameSeveralTimes() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("fooTransactionManager", beanFactory);
    PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual);
    // Call again, should be cached
    PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual2);
    verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) 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