Skip to content

Commit f148d19

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATACMNS-410 - Fixed bug in ReflectionRepositoryInvoker#invokeFindOne.
We now delegate calls to invokeFindOne to the correct target repository. Original pull request: #80.
1 parent 11f1207 commit f148d19

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* information.
3030
*
3131
* @author Oliver Gierke
32+
* @author Thomas Darimont
3233
* @since 1.6
3334
*/
3435
class ReflectionRepositoryInvoker<T> implements CrudInvoker<T> {
@@ -72,6 +73,6 @@ public T invokeSave(T object) {
7273
@Override
7374
@SuppressWarnings("unchecked")
7475
public T invokeFindOne(Serializable id) {
75-
return (T) ReflectionUtils.invokeMethod(methods.getFindOneMethod(), id);
76+
return (T) ReflectionUtils.invokeMethod(methods.getFindOneMethod(), repository, id);
7677
}
7778
}

src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.junit.runner.RunWith;
2424
import org.mockito.Mock;
2525
import org.mockito.runners.MockitoJUnitRunner;
26-
import org.springframework.data.repository.Repository;
26+
import org.springframework.data.repository.CrudRepository;
27+
import org.springframework.data.repository.core.CrudInvoker;
2728
import org.springframework.data.repository.core.CrudMethods;
2829

2930
/**
@@ -34,7 +35,7 @@
3435
@RunWith(MockitoJUnitRunner.class)
3536
public class ReflectionRepositoryInvokerUnitTests {
3637

37-
@Mock Repository<Object, Serializable> repo;
38+
@Mock CrudRepository<Object, Serializable> repo;
3839
@Mock CrudMethods methods;
3940

4041
@Test
@@ -72,4 +73,37 @@ public void rejectsRepositoryIfItDoesntExposeASaveMethod() {
7273

7374
new ReflectionRepositoryInvoker<Object>(repo, methods);
7475
}
76+
77+
/**
78+
* @see DATACMNS-410
79+
*/
80+
@Test
81+
public void invokesFindOneCorrectly() throws Exception {
82+
83+
when(methods.hasFindOneMethod()).thenReturn(true);
84+
when(methods.hasSaveMethod()).thenReturn(true);
85+
when(methods.getFindOneMethod()).thenReturn(CrudRepository.class.getMethod("findOne", Serializable.class));
86+
87+
CrudInvoker<Object> invoker = new ReflectionRepositoryInvoker<Object>(repo, methods);
88+
invoker.invokeFindOne(1L);
89+
90+
verify(repo, times(1)).findOne(1L);
91+
}
92+
93+
/**
94+
* @see DATACMNS-410
95+
*/
96+
@Test
97+
public void invokesSaveCorrectly() throws Exception {
98+
99+
when(methods.hasFindOneMethod()).thenReturn(true);
100+
when(methods.hasSaveMethod()).thenReturn(true);
101+
when(methods.getSaveMethod()).thenReturn(CrudRepository.class.getMethod("save", Object.class));
102+
103+
CrudInvoker<Object> invoker = new ReflectionRepositoryInvoker<Object>(repo, methods);
104+
Object object = new Object();
105+
invoker.invokeSave(object);
106+
107+
verify(repo, times(1)).save(object);
108+
}
75109
}

src/test/java/org/springframework/data/repository/support/RepositoriesIntegrationTests.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
import org.junit.Test;
2323
import org.junit.runner.RunWith;
24+
import org.mockito.Mockito;
2425
import org.springframework.beans.factory.annotation.Autowired;
2526
import org.springframework.context.ApplicationContext;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Configuration;
2829
import org.springframework.data.repository.CrudRepository;
2930
import org.springframework.data.repository.Repository;
31+
import org.springframework.data.repository.core.CrudInvoker;
3032
import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean;
3133
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
3234
import org.springframework.test.context.ContextConfiguration;
@@ -36,6 +38,7 @@
3638
* Integration tests for {@link Repositories}.
3739
*
3840
* @author Oliver Gierke
41+
* @author Thomas Darimont
3942
*/
4043
@RunWith(SpringJUnit4ClassRunner.class)
4144
@ContextConfiguration
@@ -61,16 +64,24 @@ public RepositoryFactoryBeanSupport<Repository<User, Long>, User, Long> userRepo
6164
}
6265

6366
@Bean
64-
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory() {
67+
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory(
68+
ProductRepository productRepository) {
6569

6670
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>();
6771
factory.setRepositoryInterface(ProductRepository.class);
72+
factory.setCustomImplementation(productRepository);
6873

6974
return factory;
7075
}
76+
77+
@Bean
78+
public ProductRepository productRepository() {
79+
return mock(ProductRepository.class);
80+
}
7181
}
7282

7383
@Autowired Repositories repositories;
84+
@Autowired ProductRepository productRepository;
7485

7586
@Test
7687
public void detectsRepositories() {
@@ -98,6 +109,21 @@ public void returnsPersistentEntityForProxiedClass() {
98109
assertThat(repositories.getPersistentEntity(user.getClass()), is(notNullValue()));
99110
}
100111

112+
/**
113+
* @see DATACMNS-410
114+
*/
115+
@Test
116+
public void findOneShouldDelegateToAppropriateRepository() {
117+
118+
Mockito.reset(productRepository);
119+
Product product = new Product();
120+
when(productRepository.findOne(4711L)).thenReturn(product);
121+
122+
CrudInvoker<Product> crudInvoker = repositories.getCrudInvoker(Product.class);
123+
124+
assertThat(crudInvoker.invokeFindOne(4711L), is(product));
125+
}
126+
101127
static class User {
102128

103129
}
@@ -106,9 +132,9 @@ interface UserRepository extends CrudRepository<User, Long> {
106132

107133
}
108134

109-
static class Product {}
135+
public static class Product {}
110136

111-
interface ProductRepository extends Repository<Product, Long> {
137+
public static interface ProductRepository extends Repository<Product, Long> {
112138

113139
Product findOne(Long id);
114140

0 commit comments

Comments
 (0)