diff --git a/src/main/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessor.java b/src/main/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessor.java new file mode 100644 index 000000000..3316d1d57 --- /dev/null +++ b/src/main/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessor.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 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 org.springframework.aop.framework.ProxyFactory; +import org.springframework.beans.factory.ListableBeanFactory; +import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor; +import org.springframework.util.Assert; + +/** + * {@link RepositoryProxyPostProcessor} to register a {@link PersistenceExceptionTranslationInterceptor} on the + * repository proxy. + * + * @author Oliver Gierke + */ +public class PersistenceExceptionTranslationRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor { + + private final PersistenceExceptionTranslationInterceptor interceptor; + + /** + * Creates a new {@link PersistenceExceptionTranslationRepositoryProxyPostProcessor} using the given + * {@link ListableBeanFactory}. + * + * @param beanFactory must not be {@literal null}. + */ + public PersistenceExceptionTranslationRepositoryProxyPostProcessor(ListableBeanFactory beanFactory) { + + Assert.notNull(beanFactory, "BeanFactory must not be null!"); + + this.interceptor = new PersistenceExceptionTranslationInterceptor(); + this.interceptor.setBeanFactory(beanFactory); + this.interceptor.afterPropertiesSet(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory) + */ + public void postProcess(ProxyFactory factory) { + factory.addAdvice(interceptor); + } +} 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 84846fd1b..faad32ecd 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-2010 the original author or authors. + * Copyright 2008-2013 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. @@ -37,6 +37,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport beans = new HashMap(); + beans.put("foo", mock(PersistenceExceptionTranslator.class)); + when(beanFactory.getBeansOfType(eq(PersistenceExceptionTranslator.class), anyBoolean(), anyBoolean())).thenReturn( + beans); + } + + @Test(expected = IllegalArgumentException.class) + public void rejectsNullBeanFactory() throws Exception { + + new PersistenceExceptionTranslationRepositoryProxyPostProcessor(null); + } + + @Test + public void setsUpBasicInstance() throws Exception { + + RepositoryProxyPostProcessor postProcessor = new PersistenceExceptionTranslationRepositoryProxyPostProcessor( + beanFactory); + + postProcessor.postProcess(proxyFactory); + + verify(proxyFactory).addAdvice(isA(PersistenceExceptionTranslationInterceptor.class)); + } +} diff --git a/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java index d167d7c06..4a738e6e8 100644 --- a/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2010 the original author or authors. + * Copyright 2008-2013 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 @@ -15,9 +15,7 @@ */ package org.springframework.data.repository.core.support; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isA; +import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import java.util.HashMap; @@ -30,10 +28,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.ListableBeanFactory; -import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor; import org.springframework.dao.support.PersistenceExceptionTranslator; -import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor; -import org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor; import org.springframework.transaction.interceptor.TransactionInterceptor; /** @@ -44,8 +39,6 @@ import org.springframework.transaction.interceptor.TransactionInterceptor; @RunWith(MockitoJUnitRunner.class) public class TransactionRepositoryProxyPostProcessorUnitTests { - TransactionalRepositoryProxyPostProcessor processor; - @Mock ListableBeanFactory beanFactory; @Mock @@ -62,13 +55,11 @@ public class TransactionRepositoryProxyPostProcessorUnitTests { @Test(expected = IllegalArgumentException.class) public void rejectsNullBeanFactory() throws Exception { - new TransactionalRepositoryProxyPostProcessor(null, "transactionManager"); } @Test(expected = IllegalArgumentException.class) public void rejectsNullTxManagerName() throws Exception { - new TransactionalRepositoryProxyPostProcessor(beanFactory, null); } @@ -76,10 +67,8 @@ public class TransactionRepositoryProxyPostProcessorUnitTests { public void setsUpBasicInstance() throws Exception { RepositoryProxyPostProcessor postProcessor = new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager"); - postProcessor.postProcess(proxyFactory); - verify(proxyFactory).addAdvice(isA(PersistenceExceptionTranslationInterceptor.class)); verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class)); } }