DATACMNS-720 - Improved disabling of default transactions.

When default transactions get disabled, we now still configure a TransactionInterceptor to make sure explicitly defined transactions are still applied. Previously the interceptor was not registered at all if default transactions were configured to be turned off and thus even explicitly declared transaction configuration was not considered.

Related tickets: DATACMNS-656.
This commit is contained in:
Oliver Gierke
2015-06-22 17:04:45 +02:00
parent e0dafd29f4
commit ac2faab0d2
4 changed files with 43 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.repository.core.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import org.junit.Test;
import org.springframework.aop.Advisor;
@@ -38,7 +39,7 @@ public class TransactionRepositoryFactoryBeanSupportUnitTests {
* @see DATACMNS-656
*/
@Test
public void doesNotRegisterTransactionalRepositoryProxyPostProcessorIfConfigured() {
public void disablesDefaultTransactionsIfConfigured() {
SampleTransactionalRepositoryFactoryBean factoryBean = new SampleTransactionalRepositoryFactoryBean();
factoryBean.setEnableDefaultTransactions(false);
@@ -48,9 +49,22 @@ public class TransactionRepositoryFactoryBeanSupportUnitTests {
CrudRepository<Object, Long> repository = factoryBean.getObject();
Advisor[] advisors = ((Advised) repository).getAdvisors();
boolean found = false;
assertThat(advisors.length, is(greaterThanOrEqualTo(2)));
assertThat(advisors[1].getAdvice(), is(not(instanceOf(TransactionInterceptor.class))));
for (Advisor advisor : advisors) {
if (advisor.getAdvice() instanceof TransactionInterceptor) {
found = true;
TransactionInterceptor interceptor = (TransactionInterceptor) advisor.getAdvice();
assertThat(getField(interceptor.getTransactionAttributeSource(), "enableDefaultTransactions"),
is((Object) false));
break;
}
}
assertThat(found, is(true));
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@@ -63,18 +63,19 @@ public class TransactionRepositoryProxyPostProcessorUnitTests {
@Test(expected = IllegalArgumentException.class)
public void rejectsNullBeanFactory() throws Exception {
new TransactionalRepositoryProxyPostProcessor(null, "transactionManager");
new TransactionalRepositoryProxyPostProcessor(null, "transactionManager", true);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullTxManagerName() throws Exception {
new TransactionalRepositoryProxyPostProcessor(beanFactory, null);
new TransactionalRepositoryProxyPostProcessor(beanFactory, null, true);
}
@Test
public void setsUpBasicInstance() throws Exception {
RepositoryProxyPostProcessor postProcessor = new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager");
RepositoryProxyPostProcessor postProcessor = new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager",
true);
postProcessor.postProcess(proxyFactory, repositoryInformation);
verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class));