DATACMNS-25 - Refactored RepositoryFactorySupport.getQueryLookupStrategy(…)
Method is not abstract anymore but rather returns null by default. Adapted QueryExecutionMethodInterceptor to handle the null case appropriately by simply skipping the query lookup. It also throws an exception if the repository has query methods defined nonetheless to prevent exceptions at runtime.
This commit is contained in:
@@ -190,14 +190,15 @@ public abstract class RepositoryFactorySupport {
|
||||
protected abstract Class<?> getRepositoryBaseClass(
|
||||
Class<?> repositoryInterface);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the {@link QueryLookupStrategy} for the given {@link Key}.
|
||||
*
|
||||
* @param key can be {@literal null}
|
||||
* @return
|
||||
*/
|
||||
protected abstract QueryLookupStrategy getQueryLookupStrategy(Key key);
|
||||
/**
|
||||
* Returns the {@link QueryLookupStrategy} for the given {@link Key}.
|
||||
*
|
||||
* @param key can be {@literal null}
|
||||
* @return the {@link QueryLookupStrategy} to use or {@literal null} if no queries should be looked up.
|
||||
*/
|
||||
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -254,6 +255,18 @@ public abstract class RepositoryFactorySupport {
|
||||
|
||||
QueryLookupStrategy lookupStrategy =
|
||||
getQueryLookupStrategy(queryLookupStrategyKey);
|
||||
|
||||
if (lookupStrategy == null) {
|
||||
|
||||
if (repositoryMetadata.hasCustomMethod()) {
|
||||
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!");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (Method method : metadata.getQueryMethods()) {
|
||||
RepositoryQuery query =
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2011 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.support;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.support.RepositoryFactorySupport.QueryExecuterMethodInterceptor;
|
||||
|
||||
/**
|
||||
* Unit test for {@link QueryExecuterMethodInterceptor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryExecuterMethodInterceptorUnitTests {
|
||||
|
||||
@Mock
|
||||
RepositoryFactorySupport factory;
|
||||
@Mock
|
||||
RepositoryMetadata metadata;
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() {
|
||||
|
||||
when(metadata.hasCustomMethod()).thenReturn(true);
|
||||
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null);
|
||||
|
||||
factory.new QueryExecuterMethodInterceptor(metadata, null, new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipsQueryLookupsIfQueryLookupStrategyIsNull() {
|
||||
|
||||
when(metadata.hasCustomMethod()).thenReturn(false);
|
||||
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null);
|
||||
|
||||
factory.new QueryExecuterMethodInterceptor(metadata, null, new Object());
|
||||
verify(metadata, times(0)).getQueryMethods();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user