From 7673567a8c9affbbdd9a21b4b857347551055bbd Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 16 May 2012 14:27:53 +0200 Subject: [PATCH] DATACMNS-171 - Added isQueryForEntity() method to QueryMethod. isQueryForEntity() returns true if the QueryMethod returns either a single entity, a collection or Page of those. --- .../data/repository/query/QueryMethod.java | 26 ++++++++---- .../query/QueryMethodUnitTests.java | 40 +++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java index 57f179894..a1afaf5fc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -123,7 +123,7 @@ public class QueryMethod { /** * Returns the domain class the query method is targeted at. * - * @return + * @return will never be {@literal null}. */ protected Class getDomainClass() { @@ -165,29 +165,39 @@ public class QueryMethod { return org.springframework.util.ClassUtils.isAssignable(Page.class, returnType); } + /** + * Returns whether the query method is a modifying one. + * + * @return + */ public boolean isModifyingQuery() { - return false; } + /** + * Returns whether the query for theis method actually returns entities. + * + * @return + */ + public boolean isQueryForEntity() { + return getDomainClass().isAssignableFrom(getReturnedObjectType()); + } + /** * Returns the {@link Parameters} wrapper to gain additional information about {@link Method} parameters. * * @return */ public Parameters getParameters() { - return parameters; } /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { - return method.toString(); } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java index f77f462ff..5375477c9 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.data.repository.query; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import static org.hamcrest.CoreMatchers.*; import java.lang.reflect.Method; @@ -23,6 +24,7 @@ import java.lang.reflect.Method; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -73,6 +75,32 @@ public class QueryMethodUnitTests { assertThat(queryMethod.isCollectionQuery(), is(false)); } + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void detectsAnEntityBeingReturned() throws Exception { + + when(metadata.getDomainType()).thenReturn((Class) User.class); + when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) SpecialUser.class); + + Method method = SampleRepository.class.getMethod("returnsEntitySubclass"); + QueryMethod queryMethod = new QueryMethod(method, metadata); + + assertThat(queryMethod.isQueryForEntity(), is(true)); + } + + @Test + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void detectsNonEntityBeingReturned() throws Exception { + + when(metadata.getDomainType()).thenReturn((Class) User.class); + when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) Integer.class); + + Method method = SampleRepository.class.getMethod("returnsProjection"); + QueryMethod queryMethod = new QueryMethod(method, metadata); + + assertThat(queryMethod.isQueryForEntity(), is(false)); + } + interface SampleRepository { String pagingMethodWithInvalidReturnType(Pageable pageable); @@ -83,5 +111,17 @@ public class QueryMethodUnitTests { Iterable sampleMethod(); Page anotherSampleMethod(Pageable pageable); + + SpecialUser returnsEntitySubclass(); + + Integer returnsProjection(); + } + + class User { + + } + + class SpecialUser extends User { + } }