diff --git a/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java index 8c5e0b9..1808072 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java @@ -44,6 +44,7 @@ import org.springframework.util.ClassUtils; * * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch */ public class KeyValuePartTreeQuery implements RepositoryQuery { @@ -134,15 +135,17 @@ public class KeyValuePartTreeQuery implements RepositoryQuery { ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); - KeyValueQuery query = new KeyValueQuery(instance.getCritieria()); + Object criteria = instance.getCritieria(); - if (instance.getCritieria() instanceof SpelExpression) { + if (criteria instanceof SpelCriteria || criteria instanceof SpelExpression) { + SpelExpression spelExpression = getSpelExpression(criteria); EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), parameters); - query = new KeyValueQuery(new SpelCriteria((SpelExpression) instance.getCritieria(), context)); + criteria = new SpelCriteria(spelExpression, context); } - + + KeyValueQuery query = new KeyValueQuery(criteria); Pageable pageable = accessor.getPageable(); Sort sort = accessor.getSort(); @@ -153,6 +156,19 @@ public class KeyValuePartTreeQuery implements RepositoryQuery { return query; } + private SpelExpression getSpelExpression(Object criteria) { + + if(criteria instanceof SpelExpression){ + return (SpelExpression) criteria; + } + + if(criteria instanceof SpelCriteria){ + return getSpelExpression(((SpelCriteria) criteria).getExpression()); + } + + throw new IllegalStateException(String.format("Cannot retrieve SpelExpression from %s", criteria)); + } + public KeyValueQuery createQuery(ParameterAccessor accessor) { PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType()); diff --git a/src/test/java/org/springframework/data/keyvalue/repository/query/CachingKeyValuePartTreeQueryUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/query/CachingKeyValuePartTreeQueryUnitTests.java new file mode 100644 index 0000000..67014a6 --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/repository/query/CachingKeyValuePartTreeQueryUnitTests.java @@ -0,0 +1,85 @@ +/* + * Copyright 2016 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.keyvalue.repository.query; + +import static org.hamcrest.core.IsNot.*; +import static org.hamcrest.core.IsSame.*; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +import java.lang.reflect.Method; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.keyvalue.Person; +import org.springframework.data.keyvalue.core.KeyValueOperations; +import org.springframework.data.keyvalue.core.SpelCriteria; +import org.springframework.data.projection.ProjectionFactory; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.query.DefaultEvaluationContextProvider; +import org.springframework.data.repository.query.QueryMethod; + +/** + * Unit tests for {@link CachingKeyValuePartTreeQuery}. + * + * @author Mark Paluch + */ +@RunWith(MockitoJUnitRunner.class) +public class CachingKeyValuePartTreeQueryUnitTests { + + @Mock KeyValueOperations kvOpsMock; + @Mock RepositoryMetadata metadataMock; + @Mock ProjectionFactory projectionFactoryMock; + + @Before + public void setUp() throws Exception { + + when(metadataMock.getDomainType()).thenReturn((Class) Person.class); + when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class); + } + + /** + * @see DATAKV-137 + */ + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void cachedSpelExpressionShouldBeReusedWithNewContext() throws NoSuchMethodException, SecurityException { + + QueryMethod qm = new QueryMethod(Repo.class.getMethod("findByFirstname", String.class), metadataMock, + projectionFactoryMock); + + KeyValuePartTreeQuery query = new CachingKeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, + kvOpsMock, SpelQueryCreator.class); + + Object[] args = new Object[] { "foo" }; + + SpelCriteria first = (SpelCriteria) query.prepareQuery(args).getCritieria(); + SpelCriteria second = (SpelCriteria) query.prepareQuery(args).getCritieria(); + + assertThat(first.getExpression(), sameInstance(second.getExpression())); + assertThat(first.getContext(), not(sameInstance(second.getContext()))); + } + + static interface Repo { + + List findByFirstname(String firstname); + } +} diff --git a/src/test/java/org/springframework/data/map/AbstractRepositoryUnitTests.java b/src/test/java/org/springframework/data/map/AbstractRepositoryUnitTests.java index 1a9e869..023814d 100644 --- a/src/test/java/org/springframework/data/map/AbstractRepositoryUnitTests.java +++ b/src/test/java/org/springframework/data/map/AbstractRepositoryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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. @@ -43,6 +43,7 @@ import org.springframework.data.repository.CrudRepository; * @author Christoph Strobl * @author Oliver Gierke * @author Thomas Darimont + * @author Mark Paluch */ public abstract class AbstractRepositoryUnitTests { @@ -61,7 +62,7 @@ public abstract class AbstractRepositoryUnitTests, KeyValueRepository { diff --git a/src/test/java/org/springframework/data/map/CachingQuerySimpleKeyValueRepositoryUnitTests.java b/src/test/java/org/springframework/data/map/CachingQuerySimpleKeyValueRepositoryUnitTests.java new file mode 100644 index 0000000..6fec61a --- /dev/null +++ b/src/test/java/org/springframework/data/map/CachingQuerySimpleKeyValueRepositoryUnitTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016 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.map; + +import static org.hamcrest.Matchers.hasItems; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.data.keyvalue.core.KeyValueOperations; +import org.springframework.data.keyvalue.repository.query.CachingKeyValuePartTreeQuery; +import org.springframework.data.keyvalue.repository.query.SpelQueryCreator; +import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory; +import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository; +import org.springframework.data.map.AbstractRepositoryUnitTests.PersonRepository; + +/** + * Unit tests for {@link SimpleKeyValueRepository} using {@link CachingKeyValuePartTreeQuery} and {@link SpelQueryCreator}. + * + * @author Mark Paluch + */ +public class CachingQuerySimpleKeyValueRepositoryUnitTests extends AbstractRepositoryUnitTests { + + @Override + protected KeyValueRepositoryFactory createKeyValueRepositoryFactory(KeyValueOperations operations) { + return new KeyValueRepositoryFactory(operations, SpelQueryCreator.class, CachingKeyValuePartTreeQuery.class); + } + + @Override + protected PersonRepository getRepository(KeyValueRepositoryFactory factory) { + return factory.getRepository(PersonRepository.class); + } +}