SGF-454 - Adapted to API changes in Spring Data Commons.

Related tickets: DATACMNS-89.
This commit is contained in:
Oliver Gierke
2015-12-14 22:23:44 +01:00
parent eebe7bb622
commit 6ad886ffae
3 changed files with 48 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2015 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.
@@ -28,6 +28,7 @@ import org.springframework.data.gemfire.repository.query.annotation.Import;
import org.springframework.data.gemfire.repository.query.annotation.Limit;
import org.springframework.data.gemfire.repository.query.annotation.Trace;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.util.Assert;
@@ -40,7 +41,6 @@ import org.springframework.util.StringUtils;
* @author John Blum
* @see org.springframework.data.repository.query.QueryMethod
*/
@SuppressWarnings("unused")
public class GemfireQueryMethod extends QueryMethod {
protected static final String[] EMPTY_STRING_ARRAY = new String[0];
@@ -53,12 +53,13 @@ public class GemfireQueryMethod extends QueryMethod {
*
* @param method must not be {@literal null}.
* @param metadata must not be {@literal null}.
* @param factory must not be {@literal null}.
* @param context must not be {@literal null}.
*/
public GemfireQueryMethod(Method method, RepositoryMetadata metadata,
public GemfireQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> context) {
super(method, metadata);
super(method, metadata, factory);
Assert.notNull(context);
assertNonPagingQueryMethod(method);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2015 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.
@@ -29,6 +29,7 @@ import org.springframework.data.gemfire.repository.query.GemfireQueryMethod;
import org.springframework.data.gemfire.repository.query.PartTreeGemfireRepositoryQuery;
import org.springframework.data.gemfire.repository.query.StringBasedGemfireRepositoryQuery;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -149,10 +150,14 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
*/
@Override
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
return new QueryLookupStrategy() {
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
GemfireQueryMethod queryMethod = new GemfireQueryMethod(method, metadata, context);
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
NamedQueries namedQueries) {
GemfireQueryMethod queryMethod = new GemfireQueryMethod(method, metadata, factory, context);
GemfireTemplate template = getTemplate(metadata);
if (queryMethod.hasAnnotatedQuery()) {

View File

@@ -16,12 +16,9 @@
package org.springframework.data.gemfire.repository.query;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
@@ -43,6 +40,8 @@ import org.springframework.data.gemfire.repository.query.annotation.Import;
import org.springframework.data.gemfire.repository.query.annotation.Limit;
import org.springframework.data.gemfire.repository.query.annotation.Trace;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.util.ObjectUtils;
@@ -59,6 +58,8 @@ public class GemfireQueryMethodUnitTests {
public ExpectedException expectedException = ExpectedException.none();
private GemfireMappingContext context = new GemfireMappingContext();
private ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
@Mock
private RepositoryMetadata metadata;
@@ -122,17 +123,17 @@ public class GemfireQueryMethodUnitTests {
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void detectsAnnotatedQueryCorrectly() throws Exception {
GemfireQueryMethod method = new GemfireQueryMethod(Sample.class.getMethod("annotated"), metadata, context);
GemfireQueryMethod method = new GemfireQueryMethod(Sample.class.getMethod("annotated"), metadata, factory, context);
assertThat(method.hasAnnotatedQuery(), is(true));
assertThat(method.getAnnotatedQuery(), is("foo"));
method = new GemfireQueryMethod(Sample.class.getMethod("annotatedButEmpty"), metadata, context);
method = new GemfireQueryMethod(Sample.class.getMethod("annotatedButEmpty"), metadata, factory, context);
assertThat(method.hasAnnotatedQuery(), is(false));
assertThat(method.getAnnotatedQuery(), is(nullValue()));
method = new GemfireQueryMethod(Sample.class.getMethod("notAnnotated"), metadata, context);
method = new GemfireQueryMethod(Sample.class.getMethod("notAnnotated"), metadata, factory, context);
assertThat(method.hasAnnotatedQuery(), is(false));
assertThat(method.getAnnotatedQuery(), is(nullValue()));
@@ -147,97 +148,97 @@ public class GemfireQueryMethodUnitTests {
expectedException.expectCause(is(nullValue(Throwable.class)));
expectedException.expectMessage(Matchers.startsWith("Pagination is not supported by GemFire Repositories!"));
new GemfireQueryMethod(Invalid.class.getMethod("someMethod", Pageable.class), metadata, context);
new GemfireQueryMethod(Invalid.class.getMethod("someMethod", Pageable.class), metadata, factory, context);
}
@Test
public void detectsQueryHintsCorrectly() throws Exception {
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context).hasHint(), is(true));
metadata, factory, context).hasHint(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context).hasHint(), is(false));
metadata, factory, context).hasHint(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context).hasHint(), is(true));
metadata, factory, context).hasHint(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("unlimitedQuery"),
metadata, context).hasHint(), is(false));
metadata, factory, context).hasHint(), is(false));
}
@Test
public void detectsQueryImportsCorrectly() throws Exception {
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context).hasImport(), is(false));
metadata, factory, context).hasImport(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context).hasImport(), is(true));
metadata, factory, context).hasImport(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context).hasImport(), is(true));
metadata, factory, context).hasImport(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("unlimitedQuery"),
metadata, context).hasImport(), is(false));
metadata, factory, context).hasImport(), is(false));
}
@Test
public void detectsQueryLimitsCorrectly() throws Exception {
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context).hasLimit(), is(false));
metadata, factory, context).hasLimit(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context).hasLimit(), is(false));
metadata, factory, context).hasLimit(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context).hasLimit(), is(true));
metadata, factory, context).hasLimit(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("unlimitedQuery"),
metadata, context).hasLimit(), is(false));
metadata, factory, context).hasLimit(), is(false));
}
@Test
public void detectsQueryTracingCorrectly() throws Exception {
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context).hasTrace(), is(true));
metadata, factory, context).hasTrace(), is(true));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context).hasTrace(), is(false));
metadata, factory, context).hasTrace(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context).hasTrace(), is(false));
metadata, factory, context).hasTrace(), is(false));
assertThat(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("unlimitedQuery"),
metadata, context).hasTrace(), is(true));
metadata, factory, context).hasTrace(), is(true));
}
@Test
public void hintOnQueryWithHint() throws Exception {
assertQueryHints(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context), "IdIdx", "LastNameIdx");
metadata, factory, context), "IdIdx", "LastNameIdx");
assertQueryHints(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context), "BirthDateIdx");
metadata, factory, context), "BirthDateIdx");
}
@Test
public void hintOnQueryWithNoHints() throws Exception {
assertNoQueryHints(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context));
metadata, factory, context));
}
@Test
public void importOnQueryWithImport() throws Exception {
assertImportStatement(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithImport"),
metadata, context), "org.example.app.domain.ExampleType");
metadata, factory, context), "org.example.app.domain.ExampleType");
assertImportStatement(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context), "org.example.app.domain.Person");
metadata, factory, context), "org.example.app.domain.Person");
}
@Test
public void importOnQueryWithNoImports() throws Exception {
assertNoImportStatement(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("queryWithHint"),
metadata, context));
metadata, factory, context));
}
@Test
public void limitOnQueryWithLimit() throws Exception {
assertLimitedQuery(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("limitedQuery"),
metadata, context), 1024);
metadata, factory, context), 1024);
}
@Test
public void limitOnQueryWithNoLimits() throws Exception {
assertUnlimitedQuery(new GemfireQueryMethod(AnnotatedQueryMethods.class.getMethod("unlimitedQuery"),
metadata, context));
metadata, factory, context));
}
@SuppressWarnings("unused")