diff --git a/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java b/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java index d2ba3bf1e..a48599577 100644 --- a/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java +++ b/src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java @@ -60,7 +60,9 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata { TypeInformation returnTypeInfo = typeInformation.getReturnType(method); Class rawType = returnTypeInfo.getType(); - return Iterable.class.isAssignableFrom(rawType) ? returnTypeInfo.getComponentType().getType() : rawType; + boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray(); + + return needToUnwrap ? returnTypeInfo.getComponentType().getType() : rawType; } /* diff --git a/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/src/main/java/org/springframework/data/repository/query/QueryMethod.java index 69d536eb2..4dc43b204 100644 --- a/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2013 the original author or authors. + * Copyright 2008-2014 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. @@ -152,7 +152,8 @@ public class QueryMethod { public boolean isCollectionQuery() { Class returnType = method.getReturnType(); - return !isPageQuery() && org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType); + return !isPageQuery() && org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType) + || returnType.isArray(); } /** diff --git a/src/test/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadataUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadataUnitTests.java index e5d672bc0..c25a5a609 100644 --- a/src/test/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadataUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadataUnitTests.java @@ -40,6 +40,9 @@ import org.springframework.data.repository.core.RepositoryMetadata; */ public class AbstractRepositoryMetadataUnitTests { + /** + * @see DATACMNS-98 + */ @Test public void discoversSimpleReturnTypeCorrectly() throws Exception { @@ -58,6 +61,9 @@ public class AbstractRepositoryMetadataUnitTests { assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class))); } + /** + * @see DATACMNS-98 + */ @Test public void determinesReturnTypeFromPageable() throws Exception { @@ -93,6 +99,9 @@ public class AbstractRepositoryMetadataUnitTests { assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(GenericType.class))); } + /** + * @see DATACMNS-98 + */ @Test public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException { @@ -101,6 +110,18 @@ public class AbstractRepositoryMetadataUnitTests { assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(Map.class))); } + /** + * @see DATACMNS-471 + */ + @Test + public void detectsArrayReturnTypeCorrectly() throws Exception { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(PagedRepository.class); + Method method = PagedRepository.class.getMethod("returnsArray"); + + assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class))); + } + interface UserRepository extends Repository { User findSingle(); @@ -126,6 +147,7 @@ public class AbstractRepositoryMetadataUnitTests { interface PagedRepository extends PagingAndSortingRepository { + User[] returnsArray(); } class GenericType { @@ -146,5 +168,4 @@ public class AbstractRepositoryMetadataUnitTests { return null; } } - } diff --git a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java index 5375477c9..205b9e79a 100644 --- a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2011 the original author or authors. + * Copyright 2008-2014 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. @@ -15,32 +15,31 @@ */ package org.springframework.data.repository.query; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import java.io.Serializable; 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; +import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; /** * Unit tests for {@link QueryMethod}. * * @author Oliver Gierke */ -@RunWith(MockitoJUnitRunner.class) public class QueryMethodUnitTests { - @Mock - RepositoryMetadata metadata; + RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class); + /** + * @see DATAJPA-59 + */ @Test(expected = IllegalStateException.class) public void rejectsPagingMethodWithInvalidReturnType() throws Exception { @@ -48,18 +47,27 @@ public class QueryMethodUnitTests { new QueryMethod(method, metadata); } + /** + * @see DATAJPA-59 + */ @Test(expected = IllegalArgumentException.class) public void rejectsPagingMethodWithoutPageable() throws Exception { Method method = SampleRepository.class.getMethod("pagingMethodWithoutPageable"); new QueryMethod(method, metadata); } + /** + * @see DATACMNS-64 + */ @Test public void setsUpSimpleQueryMethodCorrectly() throws Exception { Method method = SampleRepository.class.getMethod("findByUsername", String.class); new QueryMethod(method, metadata); } + /** + * @see DATACMNS-61 + */ @Test public void considersIterableMethodForCollectionQuery() throws Exception { Method method = SampleRepository.class.getMethod("sampleMethod"); @@ -67,6 +75,9 @@ public class QueryMethodUnitTests { assertThat(queryMethod.isCollectionQuery(), is(true)); } + /** + * @see DATACMNS-67 + */ @Test public void doesNotConsiderPageMethodCollectionQuery() throws Exception { Method method = SampleRepository.class.getMethod("anotherSampleMethod", Pageable.class); @@ -75,33 +86,44 @@ public class QueryMethodUnitTests { assertThat(queryMethod.isCollectionQuery(), is(false)); } + /** + * @see DATACMNS-171 + */ @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)); } + /** + * @see DATACMNS-171 + */ @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 { + /** + * @see DATACMNS-471 + */ + @Test + public void detectsCollectionMethodForArrayRetrunType() throws Exception { + + RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class); + Method method = SampleRepository.class.getMethod("arrayOfUsers"); + + assertThat(new QueryMethod(method, repositoryMetadata).isCollectionQuery(), is(true)); + } + + interface SampleRepository extends Repository { + String pagingMethodWithInvalidReturnType(Pageable pageable); Page pagingMethodWithoutPageable(); @@ -115,6 +137,8 @@ public class QueryMethodUnitTests { SpecialUser returnsEntitySubclass(); Integer returnsProjection(); + + User[] arrayOfUsers(); } class User {