DATACMNS-125 - QueryExecutorMethodInterceptor now correctly rejects repository interfaces with query methods if no QueryLookupStrategy defined.

This commit is contained in:
Oliver Gierke
2012-01-24 10:23:45 +01:00
parent c683d28add
commit ef65df279f
2 changed files with 18 additions and 8 deletions

View File

@@ -252,10 +252,11 @@ public abstract class RepositoryFactorySupport {
this.target = target;
QueryLookupStrategy lookupStrategy = getQueryLookupStrategy(queryLookupStrategyKey);
Iterable<Method> queryMethods = repositoryInformation.getQueryMethods();
if (lookupStrategy == null) {
if (repositoryInformation.hasCustomMethod()) {
if (queryMethods.iterator().hasNext()) {
throw new IllegalStateException("You have defined query method in the repository but "
+ "you don't have no query lookup strategy defined. The "
+ "infrastructure apparently does not support query methods!");
@@ -264,7 +265,7 @@ public abstract class RepositoryFactorySupport {
return;
}
for (Method method : repositoryInformation.getQueryMethods()) {
for (Method method : queryMethods) {
RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryInformation, namedQueries);
invokeListeners(query);
queries.put(method, query);

View File

@@ -18,16 +18,23 @@ package org.springframework.data.repository.core.support;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
/**
* Unit test for {@link QueryExecuterMethodInterceptor}.
*
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
@@ -37,11 +44,13 @@ public class QueryExecutorMethodInterceptorUnitTests {
RepositoryFactorySupport factory;
@Mock
RepositoryInformation information;
@Mock
QueryLookupStrategy strategy;
@Test(expected = IllegalStateException.class)
public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() {
public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() throws Exception {
when(information.hasCustomMethod()).thenReturn(true);
when(information.getQueryMethods()).thenReturn(Arrays.asList(Object.class.getMethod("toString")));
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null);
factory.new QueryExecutorMethodInterceptor(information, null, new Object());
@@ -50,10 +59,10 @@ public class QueryExecutorMethodInterceptorUnitTests {
@Test
public void skipsQueryLookupsIfQueryLookupStrategyIsNull() {
when(information.hasCustomMethod()).thenReturn(false);
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null);
when(information.getQueryMethods()).thenReturn(Collections.<Method> emptySet());
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(strategy);
factory.new QueryExecutorMethodInterceptor(information, null, new Object());
verify(information, times(0)).getQueryMethods();
verify(strategy, times(0)).resolveQuery(any(Method.class), any(RepositoryMetadata.class), any(NamedQueries.class));
}
}