DATACMNS-171 - Added isQueryForEntity() method to QueryMethod.

isQueryForEntity() returns true if the QueryMethod returns either a single entity, a collection or Page of those.
This commit is contained in:
Oliver Gierke
2012-05-16 14:27:53 +02:00
parent f4c3c786b7
commit 7673567a8c
2 changed files with 58 additions and 8 deletions

View File

@@ -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();
}
}

View File

@@ -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<String> sampleMethod();
Page<String> anotherSampleMethod(Pageable pageable);
SpecialUser returnsEntitySubclass();
Integer returnsProjection();
}
class User {
}
class SpecialUser extends User {
}
}