DATACMNS-318 - Refined persistence exception translation infrastructure.

Extracted the activation of persistence exception translation into a separate RepositoryProxyPostProcessor. Adapted TransactionalRepositoryFactoryBeanSupport to also register the newly introduced PersistenceExceptionTranslationRepositoryProxyPostProcessor.
This commit is contained in:
Oliver Gierke
2013-04-19 15:18:11 +02:00
parent 8a67259aef
commit 4303fdf399
5 changed files with 148 additions and 38 deletions

View File

@@ -0,0 +1,73 @@
/*
* 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 static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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;
/**
* Unit test for {@link TransactionalRepositoryProxyPostProcessor}.
*
* @author Oliver Gierke
* @since 1.6
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTests {
@Mock
ListableBeanFactory beanFactory;
@Mock
ProxyFactory proxyFactory;
@Before
public void setUp() {
Map<String, PersistenceExceptionTranslator> beans = new HashMap<String, PersistenceExceptionTranslator>();
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));
}
}

View File

@@ -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));
}
}