DATACMNS-410 - Fixed bug in ReflectionRepositoryInvoker#invokeFindOne.

We now delegate calls to invokeFindOne to the correct target repository.

Original pull request: #80.
This commit is contained in:
Thomas Darimont
2013-12-13 12:24:38 +01:00
committed by Oliver Gierke
parent 11f1207e4e
commit f148d19c81
3 changed files with 67 additions and 6 deletions

View File

@@ -23,7 +23,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.CrudInvoker;
import org.springframework.data.repository.core.CrudMethods;
/**
@@ -34,7 +35,7 @@ import org.springframework.data.repository.core.CrudMethods;
@RunWith(MockitoJUnitRunner.class)
public class ReflectionRepositoryInvokerUnitTests {
@Mock Repository<Object, Serializable> repo;
@Mock CrudRepository<Object, Serializable> repo;
@Mock CrudMethods methods;
@Test
@@ -72,4 +73,37 @@ public class ReflectionRepositoryInvokerUnitTests {
new ReflectionRepositoryInvoker<Object>(repo, methods);
}
/**
* @see DATACMNS-410
*/
@Test
public void invokesFindOneCorrectly() throws Exception {
when(methods.hasFindOneMethod()).thenReturn(true);
when(methods.hasSaveMethod()).thenReturn(true);
when(methods.getFindOneMethod()).thenReturn(CrudRepository.class.getMethod("findOne", Serializable.class));
CrudInvoker<Object> invoker = new ReflectionRepositoryInvoker<Object>(repo, methods);
invoker.invokeFindOne(1L);
verify(repo, times(1)).findOne(1L);
}
/**
* @see DATACMNS-410
*/
@Test
public void invokesSaveCorrectly() throws Exception {
when(methods.hasFindOneMethod()).thenReturn(true);
when(methods.hasSaveMethod()).thenReturn(true);
when(methods.getSaveMethod()).thenReturn(CrudRepository.class.getMethod("save", Object.class));
CrudInvoker<Object> invoker = new ReflectionRepositoryInvoker<Object>(repo, methods);
Object object = new Object();
invoker.invokeSave(object);
verify(repo, times(1)).save(object);
}
}

View File

@@ -21,12 +21,14 @@ import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.CrudInvoker;
import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.test.context.ContextConfiguration;
@@ -36,6 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* Integration tests for {@link Repositories}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@@ -61,16 +64,24 @@ public class RepositoriesIntegrationTests {
}
@Bean
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory() {
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory(
ProductRepository productRepository) {
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>();
factory.setRepositoryInterface(ProductRepository.class);
factory.setCustomImplementation(productRepository);
return factory;
}
@Bean
public ProductRepository productRepository() {
return mock(ProductRepository.class);
}
}
@Autowired Repositories repositories;
@Autowired ProductRepository productRepository;
@Test
public void detectsRepositories() {
@@ -98,6 +109,21 @@ public class RepositoriesIntegrationTests {
assertThat(repositories.getPersistentEntity(user.getClass()), is(notNullValue()));
}
/**
* @see DATACMNS-410
*/
@Test
public void findOneShouldDelegateToAppropriateRepository() {
Mockito.reset(productRepository);
Product product = new Product();
when(productRepository.findOne(4711L)).thenReturn(product);
CrudInvoker<Product> crudInvoker = repositories.getCrudInvoker(Product.class);
assertThat(crudInvoker.invokeFindOne(4711L), is(product));
}
static class User {
}
@@ -106,9 +132,9 @@ public class RepositoriesIntegrationTests {
}
static class Product {}
public static class Product {}
interface ProductRepository extends Repository<Product, Long> {
public static interface ProductRepository extends Repository<Product, Long> {
Product findOne(Long id);