Search in sources :

Example 11 with RuleBasedTransactionAttribute

use of org.springframework.transaction.interceptor.RuleBasedTransactionAttribute 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("setAge", int.class);
    Method interfaceMethod3 = ITestBean3.class.getMethod("getName");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    atas.setEmbeddedValueResolver(strVal -> ("${myTimeout}".equals(strVal) ? "5" : strVal));
    TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class);
    assertThat(actual.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_REQUIRES_NEW);
    assertThat(actual.getIsolationLevel()).isEqualTo(TransactionAttribute.ISOLATION_REPEATABLE_READ);
    assertThat(actual.getTimeout()).isEqualTo(5);
    assertThat(actual.isReadOnly()).isTrue();
    TransactionAttribute actual2 = atas.getTransactionAttribute(interfaceMethod2, TestBean3.class);
    assertThat(actual2.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_REQUIRES_NEW);
    assertThat(actual2.getIsolationLevel()).isEqualTo(TransactionAttribute.ISOLATION_REPEATABLE_READ);
    assertThat(actual2.getTimeout()).isEqualTo(5);
    assertThat(actual2.isReadOnly()).isTrue();
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
    TransactionAttribute actual3 = atas.getTransactionAttribute(interfaceMethod3, TestBean3.class);
    assertThat(actual3.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_REQUIRED);
}
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.jupiter.api.Test)

Example 12 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customClassAttributeWithReadOnlyOverrideDetected.

@Test
public void customClassAttributeWithReadOnlyOverrideDetected() throws Exception {
    Method method = TestBean7.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean7.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
    assertThat(actual.isReadOnly()).isTrue();
}
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.jupiter.api.Test)

Example 13 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customMethodAttributeWithReadOnlyOverrideDetected.

@Test
public void customMethodAttributeWithReadOnlyOverrideDetected() throws Exception {
    Method method = TestBean8.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean8.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
    assertThat(actual.isReadOnly()).isTrue();
}
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.jupiter.api.Test)

Example 14 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnCglibClassMethod.

/**
 * Test the important case where the invocation is on a proxied interface method
 * but the attribute is defined on the target class.
 */
@Test
public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception {
    Method classMethod = ITestBean1.class.getMethod("getAge");
    TestBean1 tb = new TestBean1();
    ProxyFactory pf = new ProxyFactory(tb);
    pf.setProxyTargetClass(true);
    Object proxy = pf.getProxy();
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(classMethod, proxy.getClass());
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
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) ProxyFactory(org.springframework.aop.framework.ProxyFactory) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) GroovyObject(groovy.lang.GroovyObject) Method(java.lang.reflect.Method) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 15 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customMethodAttributeWithReadOnlyOverrideOnInterface.

@Test
public void customMethodAttributeWithReadOnlyOverrideOnInterface() throws Exception {
    Method method = TestInterface10.class.getMethod("getAge");
    Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
    assertThat(annotation).as("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean10.getAge()").isNotNull();
    annotation = AnnotationUtils.findAnnotation(TestBean10.class, Transactional.class);
    assertThat(annotation).as("AnnotationUtils.findAnnotation should not find @Transactional for TestBean10").isNull();
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean10.class);
    assertThat(actual).as("Failed to retrieve TransactionAttribute for TestBean10.getAge()").isNotNull();
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
    assertThat(actual.isReadOnly()).isTrue();
}
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.jupiter.api.Test)

Aggregations

RuleBasedTransactionAttribute (org.springframework.transaction.interceptor.RuleBasedTransactionAttribute)15 NoRollbackRuleAttribute (org.springframework.transaction.interceptor.NoRollbackRuleAttribute)14 RollbackRuleAttribute (org.springframework.transaction.interceptor.RollbackRuleAttribute)14 Method (java.lang.reflect.Method)12 Test (org.junit.jupiter.api.Test)12 TransactionAttribute (org.springframework.transaction.interceptor.TransactionAttribute)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)3 GroovyObject (groovy.lang.GroovyObject)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 Element (org.w3c.dom.Element)1