From d7adfbd859a4f8fe1a5dee8bb8584cfa00102a77 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 10 Mar 2015 19:11:06 +0100 Subject: [PATCH] DATACMNS-656 - Make application of default transactions configurable on TrasnactionalRepositoryFactoryBeanSupport. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TrasnactionalRepositoryFactoryBeanSupport now exposes a setEnableDefaultTransactions(…) which, if disabled, causes the TransactionalRepositoryProxyPostProcessor not to be registered and thus no default transactions to be applied. Related tickets: DATAJPA-685. --- .../support/RepositoryFactorySupport.java | 2 +- ...sactionalRepositoryFactoryBeanSupport.java | 18 ++++- .../RepositoryFactorySupportUnitTests.java | 18 ++++- ...RepositoryFactoryBeanSupportUnitTests.java | 71 +++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index c1640b8da..2de6b3175 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -134,7 +134,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { */ public void addRepositoryProxyPostProcessor(RepositoryProxyPostProcessor processor) { - Assert.notNull(processor); + Assert.notNull(processor, "RepositoryProxyPostProcessor must not be null!"); this.postProcessors.add(processor); } diff --git a/src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java b/src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java index faad32ecd..387bad44f 100644 --- a/src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2013 the original author or authors. + * Copyright 2008-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport backingRepo; @@ -207,6 +211,18 @@ public class RepositoryFactorySupportUnitTests { assertThat(result.iterator().next(), is((Object) "Dave")); } + /** + * @see DATACMNS-656 + */ + @Test + public void rejectsNullRepositoryProxyPostProcessor() { + + exception.expect(IllegalArgumentException.class); + exception.expectMessage(RepositoryProxyPostProcessor.class.getSimpleName()); + + factory.addRepositoryProxyPostProcessor(null); + } + interface ObjectRepository extends Repository, ObjectRepositoryCustom { Object findByClass(Class clazz); diff --git a/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java new file mode 100644 index 000000000..8c571c9ea --- /dev/null +++ b/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.repository.core.support; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import org.junit.Test; +import org.springframework.aop.Advisor; +import org.springframework.aop.framework.Advised; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.data.repository.CrudRepository; +import org.springframework.transaction.interceptor.TransactionInterceptor; + +/** + * Unit tests for {@link TransactionalRepositoryFactoryBeanSupport}. + * + * @author Oliver Gierke + * @soundtrack The Intersphere - Live in Mannheim + */ +public class TransactionRepositoryFactoryBeanSupportUnitTests { + + /** + * @see DATACMNS-656 + */ + @Test + public void doesNotRegisterTransactionalRepositoryProxyPostProcessorIfConfigured() { + + SampleTransactionalRepositoryFactoryBean factoryBean = new SampleTransactionalRepositoryFactoryBean(); + factoryBean.setEnableDefaultTransactions(false); + factoryBean.setBeanFactory(new DefaultListableBeanFactory()); + factoryBean.afterPropertiesSet(); + + CrudRepository repository = factoryBean.getObject(); + + Advisor[] advisors = ((Advised) repository).getAdvisors(); + + assertThat(advisors.length, is(greaterThanOrEqualTo(2))); + assertThat(advisors[1].getAdvice(), is(not(instanceOf(TransactionInterceptor.class)))); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + static class SampleTransactionalRepositoryFactoryBean extends + TransactionalRepositoryFactoryBeanSupport, Object, Long> { + + private final CrudRepository repository = mock(CrudRepository.class); + + public SampleTransactionalRepositoryFactoryBean() { + setRepositoryInterface((Class) CrudRepository.class); + } + + @Override + protected RepositoryFactorySupport doCreateRepositoryFactory() { + return new DummyRepositoryFactory(repository); + } + } +}