DATACMNS-464 - Transaction attribute lookup now considers target class as fallback.

In case of a standalone repository interface declaring CRUD methods manually we didn't inspect the invocation target method for transactional settings. That might be needed if e.g. a save(…) method is declared but not annotated at all.
This commit is contained in:
Oliver Gierke
2014-03-06 16:48:12 +01:00
parent 4f25a8da24
commit 4cdd178276
7 changed files with 150 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -124,6 +124,14 @@ public class DefaultCrudMethodsUnitTests {
assertFindAllMethodOn(type, CrudRepository.class.getDeclaredMethod("findAll"));
}
/**
* @see DATACMNS-464
*/
@Test
public void detectsCustomSaveMethod() throws Exception {
assertSaveMethodOn(RepositoryWithCustomSave.class, RepositoryWithCustomSave.class.getMethod("save", Domain.class));
}
private static CrudMethods getMethodsFor(Class<?> repositoryInterface) {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
@@ -179,6 +187,11 @@ public class DefaultCrudMethodsUnitTests {
interface DomainPagingAndSortingRepository extends PagingAndSortingRepository<Domain, Long> {}
interface RepositoryWithCustomSave extends Repository<Domain, Serializable> {
Domain save(Domain domain);
}
interface RepositoryWithCustomSortingAndPagingFindAll extends Repository<Domain, Serializable> {
Iterable<Domain> findAll(Sort sort);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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
@@ -40,10 +40,8 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
@RunWith(MockitoJUnitRunner.class)
public class PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTests {
@Mock
ListableBeanFactory beanFactory;
@Mock
ProxyFactory proxyFactory;
@Mock ListableBeanFactory beanFactory;
@Mock ProxyFactory proxyFactory;
@Before
public void setUp() {
@@ -54,19 +52,25 @@ public class PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTest
beans);
}
/**
* @see DATACMNS-318
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullBeanFactory() throws Exception {
new PersistenceExceptionTranslationRepositoryProxyPostProcessor(null);
}
/**
* @see DATACMNS-318
*/
@Test
public void setsUpBasicInstance() throws Exception {
RepositoryProxyPostProcessor postProcessor = new PersistenceExceptionTranslationRepositoryProxyPostProcessor(
beanFactory);
postProcessor.postProcess(proxyFactory);
postProcessor.postProcess(proxyFactory, null);
verify(proxyFactory).addAdvice(isA(PersistenceExceptionTranslationInterceptor.class));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2014 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,12 +15,16 @@
*/
package org.springframework.data.repository.core.support;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -29,6 +33,11 @@ import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor.CustomAnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/**
@@ -39,10 +48,9 @@ import org.springframework.transaction.interceptor.TransactionInterceptor;
@RunWith(MockitoJUnitRunner.class)
public class TransactionRepositoryProxyPostProcessorUnitTests {
@Mock
ListableBeanFactory beanFactory;
@Mock
ProxyFactory proxyFactory;
@Mock ListableBeanFactory beanFactory;
@Mock ProxyFactory proxyFactory;
@Mock RepositoryInformation repositoryInformation;
@Before
public void setUp() {
@@ -67,8 +75,63 @@ public class TransactionRepositoryProxyPostProcessorUnitTests {
public void setsUpBasicInstance() throws Exception {
RepositoryProxyPostProcessor postProcessor = new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager");
postProcessor.postProcess(proxyFactory);
postProcessor.postProcess(proxyFactory, repositoryInformation);
verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class));
}
/**
* @see DATACMNS-464
*/
@Test
public void fallsBackToTargetMethodTransactionSettings() throws Exception {
assertTransactionAttributeFor(SampleImplementation.class);
}
/**
* @see DATACMNS-464
*/
@Test
public void fallsBackToTargetClassTransactionSettings() throws Exception {
assertTransactionAttributeFor(SampleImplementationWithClassAnnotation.class);
}
private void assertTransactionAttributeFor(Class<?> implementationClass) throws Exception {
Method repositorySaveMethod = SampleRepository.class.getMethod("save", Sample.class);
Method implementationClassMethod = implementationClass.getMethod("save", Object.class);
when(repositoryInformation.getTargetClassMethod(repositorySaveMethod)).thenReturn(implementationClassMethod);
CustomAnnotationTransactionAttributeSource attributeSource = new CustomAnnotationTransactionAttributeSource();
attributeSource.setRepositoryInformation(repositoryInformation);
TransactionAttribute attribute = attributeSource.getTransactionAttribute(repositorySaveMethod,
SampleImplementation.class);
assertThat(attribute, Matchers.is(Matchers.notNullValue()));
}
static class Sample {}
interface SampleRepository extends Repository<Sample, Serializable> {
Sample save(Sample object);
}
static class SampleImplementation<T> {
@Transactional
public <S extends T> S save(S object) {
return null;
}
}
@Transactional
static class SampleImplementationWithClassAnnotation<T> {
public <S extends T> S save(S object) {
return null;
}
}
}