Search in sources :

Example 11 with TransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnGroovyClass.

@Test
public void transactionAttributeDeclaredOnGroovyClass() throws Exception {
    Method getAgeMethod = ITestBean.class.getMethod("getAge");
    Method getNameMethod = ITestBean.class.getMethod("getName");
    Method getMetaClassMethod = GroovyObject.class.getMethod("getMetaClass");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, GroovyTestBean.class);
    assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
    TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, GroovyTestBean.class);
    assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getNameAttr.getPropagationBehavior());
    assertNull(atas.getTransactionAttribute(getMetaClassMethod, GroovyTestBean.class));
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 12 with TransactionAttribute

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

the class TxNamespaceHandlerTests method rollbackRules.

@Test
public void rollbackRules() {
    TransactionInterceptor txInterceptor = (TransactionInterceptor) context.getBean("txRollbackAdvice");
    TransactionAttributeSource txAttrSource = txInterceptor.getTransactionAttributeSource();
    TransactionAttribute txAttr = txAttrSource.getTransactionAttribute(getAgeMethod, ITestBean.class);
    assertTrue("should be configured to rollback on Exception", txAttr.rollbackOn(new Exception()));
    txAttr = txAttrSource.getTransactionAttribute(setAgeMethod, ITestBean.class);
    assertFalse("should not rollback on RuntimeException", txAttr.rollbackOn(new RuntimeException()));
}
Also used : TransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource) ITestBean(org.springframework.tests.sample.beans.ITestBean) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) Test(org.junit.Test)

Example 13 with TransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method transactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod.

/**
	 * Test that when an attribute exists on both class and interface, class takes precedence.
	 */
@Test
public void transactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod() throws Exception {
    Method interfaceMethod = ITestBean3.class.getMethod("getAge");
    Method interfaceMethod2 = ITestBean3.class.getMethod("getName");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class);
    assertEquals(TransactionAttribute.PROPAGATION_REQUIRES_NEW, actual.getPropagationBehavior());
    assertEquals(TransactionAttribute.ISOLATION_REPEATABLE_READ, actual.getIsolationLevel());
    assertEquals(5, actual.getTimeout());
    assertTrue(actual.isReadOnly());
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
    TransactionAttribute actual2 = atas.getTransactionAttribute(interfaceMethod2, TestBean3.class);
    assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, actual2.getPropagationBehavior());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with TransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customClassAttributeWithReadOnlyOverrideOnInterface.

@Test
public void customClassAttributeWithReadOnlyOverrideOnInterface() throws Exception {
    Method method = TestInterface9.class.getMethod("getAge");
    Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
    assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean9.getAge()", annotation);
    annotation = AnnotationUtils.findAnnotation(TestBean9.class, Transactional.class);
    assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean9", annotation);
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean9.class);
    assertNotNull("Failed to retrieve TransactionAttribute for TestBean9.getAge()", actual);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
    assertTrue(actual.isReadOnly());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with TransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnInterfaceMethodOnly.

/**
	 * Test case where attribute is on the interface method.
	 */
@Test
public void transactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception {
    Method interfaceMethod = ITestBean2.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

TransactionAttribute (org.springframework.transaction.interceptor.TransactionAttribute)23 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 RuleBasedTransactionAttribute (org.springframework.transaction.interceptor.RuleBasedTransactionAttribute)19 IOException (java.io.IOException)11 NoRollbackRuleAttribute (org.springframework.transaction.interceptor.NoRollbackRuleAttribute)11 RollbackRuleAttribute (org.springframework.transaction.interceptor.RollbackRuleAttribute)11 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 GroovyObject (groovy.lang.GroovyObject)1 DataSource (javax.sql.DataSource)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 ResourceDatabasePopulator (org.springframework.jdbc.datasource.init.ResourceDatabasePopulator)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 AnnotationTransactionAttributeSource (org.springframework.transaction.annotation.AnnotationTransactionAttributeSource)1 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)1 TransactionAttributeSource (org.springframework.transaction.interceptor.TransactionAttributeSource)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1