Search in sources :

Example 1 with TransactionAttribute

use of cn.taketoday.transaction.interceptor.TransactionAttribute in project today-infrastructure by TAKETODAY.

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 : RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) NoRollbackRuleAttribute(cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(cn.taketoday.transaction.interceptor.RollbackRuleAttribute) RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 2 with TransactionAttribute

use of cn.taketoday.transaction.interceptor.TransactionAttribute in project today-infrastructure by TAKETODAY.

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnClassWithEjb3.

@Test
public void transactionAttributeDeclaredOnClassWithEjb3() throws Exception {
    Method getAgeMethod = ITestBean1.class.getMethod("getAge");
    Method getNameMethod = ITestBean1.class.getMethod("getName");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean2.class);
    assertThat(getAgeAttr.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_REQUIRED);
    TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, Ejb3AnnotatedBean2.class);
    assertThat(getNameAttr.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_SUPPORTS);
}
Also used : RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionAttribute

use of cn.taketoday.transaction.interceptor.TransactionAttribute in project today-infrastructure by TAKETODAY.

the class AnnotationTransactionAttributeSourceTests method customClassAttributeDetected.

@Test
public void customClassAttributeDetected() throws Exception {
    Method method = TestBean5.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean5.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());
}
Also used : RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) NoRollbackRuleAttribute(cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(cn.taketoday.transaction.interceptor.RollbackRuleAttribute) RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionAttribute

use of cn.taketoday.transaction.interceptor.TransactionAttribute in project today-infrastructure by TAKETODAY.

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnClassMethod.

/**
 * 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 transactionAttributeDeclaredOnClassMethod() throws Exception {
    Method classMethod = ITestBean1.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
Also used : RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) NoRollbackRuleAttribute(cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(cn.taketoday.transaction.interceptor.RollbackRuleAttribute) RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionAttribute

use of cn.taketoday.transaction.interceptor.TransactionAttribute in project today-infrastructure by TAKETODAY.

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();
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
Also used : RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionAttribute (cn.taketoday.transaction.interceptor.TransactionAttribute)44 Method (java.lang.reflect.Method)40 Test (org.junit.jupiter.api.Test)40 RuleBasedTransactionAttribute (cn.taketoday.transaction.interceptor.RuleBasedTransactionAttribute)38 NoRollbackRuleAttribute (cn.taketoday.transaction.interceptor.NoRollbackRuleAttribute)22 RollbackRuleAttribute (cn.taketoday.transaction.interceptor.RollbackRuleAttribute)22 IOException (java.io.IOException)22 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)4 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 Resource (cn.taketoday.core.io.Resource)2 ResourceDatabasePopulator (cn.taketoday.jdbc.datasource.init.ResourceDatabasePopulator)2 CallCountingTransactionManager (cn.taketoday.transaction.CallCountingTransactionManager)2 DefaultTransactionAttribute (cn.taketoday.transaction.interceptor.DefaultTransactionAttribute)2 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)2 DataSource (javax.sql.DataSource)2