diff --git a/src/main/java/org/springframework/data/gemfire/repository/query/GemfireRepositoryQuery.java b/src/main/java/org/springframework/data/gemfire/repository/query/GemfireRepositoryQuery.java index 2c595ae5..b70e8e95 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/query/GemfireRepositoryQuery.java +++ b/src/main/java/org/springframework/data/gemfire/repository/query/GemfireRepositoryQuery.java @@ -21,21 +21,31 @@ import org.springframework.util.Assert; /** * Base class for GemFire specific {@link RepositoryQuery} implementations. - * + *

* @author Oliver Gierke * @author David Turanski + * @author John Blum + * @see org.springframework.data.gemfire.repository.query.GemfireQueryMethod + * @see org.springframework.data.repository.query.RepositoryQuery */ abstract class GemfireRepositoryQuery implements RepositoryQuery { private final GemfireQueryMethod queryMethod; + /* + * (non-Javadoc) + * Constructor used for testing purposes only! + */ + GemfireRepositoryQuery() { + queryMethod = null; + } + /** * Creates a new {@link GemfireRepositoryQuery} using the given {@link GemfireQueryMethod}. * * @param queryMethod must not be {@literal null}. */ public GemfireRepositoryQuery(GemfireQueryMethod queryMethod) { - Assert.notNull(queryMethod); this.queryMethod = queryMethod; } @@ -48,4 +58,5 @@ abstract class GemfireRepositoryQuery implements RepositoryQuery { public QueryMethod getQueryMethod() { return this.queryMethod; } + } diff --git a/src/main/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQuery.java b/src/main/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQuery.java index a239134c..b956a424 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQuery.java +++ b/src/main/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQuery.java @@ -15,9 +15,9 @@ */ package org.springframework.data.gemfire.repository.query; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.data.gemfire.GemfireTemplate; @@ -28,13 +28,13 @@ import org.springframework.util.StringUtils; import com.gemstone.gemfire.cache.query.SelectResults; import com.gemstone.gemfire.cache.query.internal.ResultsBag; -import com.gemstone.gemfire.cache.query.internal.ResultsCollectionPdxDeserializerWrapper; /** * {@link GemfireRepositoryQuery} using plain {@link String} based OQL queries. - * + *

* @author Oliver Gierke * @author David Turanski + * @author John Blum */ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery { @@ -44,6 +44,16 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery { private final GemfireQueryMethod method; private final GemfireTemplate template; + /* + * (non-Javadoc) + * Constructor used for testing purposes only! + */ + StringBasedGemfireRepositoryQuery() { + query = null; + method = null; + template = null; + } + /** * Creates a new {@link StringBasedGemfireRepositoryQuery} using the given {@link GemfireQueryMethod} and * {@link GemfireTemplate}. The actual query {@link String} will be looked up from the query method. @@ -85,13 +95,11 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery { */ @Override public Object execute(Object[] parameters) { - ParametersParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), parameters); QueryString query = this.query.forRegion(method.getEntityInformation().getJavaType(), template.getRegion()); - Iterator indexes = query.getInParameterIndexes().iterator(); - while (indexes.hasNext()) { - query = query.bindIn(toCollection(accessor.getBindableValue(indexes.next() - 1))); + for (Integer index : query.getInParameterIndexes()) { + query = query.bindIn(toCollection(accessor.getBindableValue(index - 1))); } Collection result = toCollection(template.find(query.toString(), parameters)); @@ -112,27 +120,33 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery { } /** - * Returns the given object as collection. Collections will be returned as is, Arrays will be converted into a - * collection and all other objects will be wrapped into a single-element collection. - * - * @param source - * @return + * Returns the given object as a Collection. Collections will be returned as is, Arrays will be converted into a + * Collection and all other objects will be wrapped into a single-element Collection. + *

+ * @param source the resulting object from the GemFire Query. + * @return the querying resulting object as a Collection. + * @see java.util.Arrays#asList(Object[]) + * @see java.util.Collection + * @see org.springframework.util.CollectionUtils#arrayToList(Object) */ - private Collection toCollection(Object source) { - + Collection toCollection(Object source) { if (source instanceof SelectResults) { return ((SelectResults) source).asList(); } if (source instanceof ResultsBag) { - ResultsBag bag = (ResultsBag) source; - return bag.asList(); + return ((ResultsBag) source).asList(); } if (source instanceof Collection) { return (Collection) source; } - return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source); + if (source == null) { + return Collections.emptyList(); + } + + return (source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Arrays.asList(source)); } + } diff --git a/src/test/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQueryTest.java b/src/test/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQueryTest.java new file mode 100644 index 00000000..2e654dac --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/query/StringBasedGemfireRepositoryQueryTest.java @@ -0,0 +1,113 @@ +/* + * Copyright 2010-2013 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.gemfire.repository.query; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.query.SelectResults; +import com.gemstone.gemfire.cache.query.internal.ResultsBag; + +/** + * The SpringBasedGemfireRepositoryQueryTest class is a test suite of test cases testing the contract and functionality + * of the StringBasedGemfireRepositoryQuery class. + *

+ * @author John Blum + * @see org.mockito.Mockito + * @see org.junit.Test + * @see org.springframework.data.gemfire.repository.query.StringBasedGemfireRepositoryQuery + * @since 1.4.0 + */ +public class StringBasedGemfireRepositoryQueryTest { + + private final StringBasedGemfireRepositoryQuery repositoryQuery = new StringBasedGemfireRepositoryQuery(); + + @Test + public void testToCollectionWithSelectResults() { + SelectResults mockSelectResults = mock(SelectResults.class, "testToCollectionWithSelectResults.SelectResults"); + List expectedList = Arrays.asList("one", "two", "three"); + + when(mockSelectResults.asList()).thenReturn(expectedList); + + Collection actualList = repositoryQuery.toCollection(mockSelectResults); + + assertSame(expectedList, actualList); + } + + @Test + public void testToCollectionWithResultsBag() { + ResultsBag mockResultsBag = mock(ResultsBag.class, "testToCollectionWithResultsBag.ResultsBag"); + List expectedList = Arrays.asList("a", "b", "c"); + + when(mockResultsBag.asList()).thenReturn(expectedList); + + Collection actualList = repositoryQuery.toCollection(mockResultsBag); + + assertSame(expectedList, actualList); + } + + @Test + public void testToCollectionWithCollection() { + List expectedList = Arrays.asList("x", "y", "z"); + Collection actualList = repositoryQuery.toCollection(expectedList); + + assertSame(expectedList, actualList); + } + + @Test + public void testToCollectionWithArray() { + Object[] array = { 1, 2, 3 }; + Collection list = repositoryQuery.toCollection(array); + + assertNotNull(list); + assertNotSame(array, list); + assertTrue(list instanceof List); + assertEquals(array.length, list.size()); + assertTrue(list.containsAll(Arrays.asList(array))); + } + + @Test + public void testToCollectionWithSingleObject() { + Collection list = repositoryQuery.toCollection("test"); + + assertTrue(list instanceof List); + assertFalse(list.isEmpty()); + assertEquals(1, list.size()); + assertEquals("test", ((List) list).get(0)); + } + + @Test + public void testToCollectionWithNull() { + Collection list = repositoryQuery.toCollection(null); + + assertNotNull(list); + assertTrue(list.isEmpty()); + } + +}