From 950e0d3b7db8575f9eca55442a29f0be45d1dcea Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 10 Feb 2015 20:12:47 +0100 Subject: [PATCH] DATACMNS-647 - Added test case for collection parameter invocation of query method through ReflectionRepositoryInvoker. Added test cases to make sure that request parameters both in separate values (i.e. ?ids=1&ids=2) as well as comma separated ones (i.e. ?ids=1,2) are translated into a Collection correctly. Related ticket: spring-hateoas/#280. --- .../CrudRepositoryInvokerUnitTests.java | 5 ++-- .../ReflectionRepositoryInvokerUnitTests.java | 28 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/springframework/data/repository/support/CrudRepositoryInvokerUnitTests.java b/src/test/java/org/springframework/data/repository/support/CrudRepositoryInvokerUnitTests.java index 724134fa0..298104fd6 100644 --- a/src/test/java/org/springframework/data/repository/support/CrudRepositoryInvokerUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/CrudRepositoryInvokerUnitTests.java @@ -20,6 +20,7 @@ import static org.springframework.data.repository.support.RepositoryInvocationTe import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Collection; import java.util.Date; import java.util.List; @@ -37,8 +38,6 @@ import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; import org.springframework.data.repository.query.Param; -import org.springframework.data.repository.support.CrudRepositoryInvoker; -import org.springframework.data.repository.support.RepositoryInvoker; import org.springframework.data.repository.support.RepositoryInvocationTestUtils.VerifyingMethodInterceptor; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat.ISO; @@ -183,6 +182,8 @@ public class CrudRepositoryInvokerUnitTests { Page findByCreatedUsingISO8601Date(@Param("date") @DateTimeFormat(iso = ISO.DATE_TIME) Date date, Pageable pageable); + + List findByIdIn(@Param("ids") Collection ids); } interface CrudWithFindAllWithSort extends CrudRepository { diff --git a/src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java b/src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java index 36508c739..d6d5998b7 100644 --- a/src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2015 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. @@ -21,10 +21,13 @@ import static org.mockito.Mockito.*; import static org.springframework.data.repository.support.RepositoryInvocationTestUtils.*; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -41,8 +44,6 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; -import org.springframework.data.repository.support.ReflectionRepositoryInvoker; -import org.springframework.data.repository.support.RepositoryInvoker; import org.springframework.data.repository.support.CrudRepositoryInvokerUnitTests.Person; import org.springframework.data.repository.support.CrudRepositoryInvokerUnitTests.PersonRepository; import org.springframework.data.repository.support.RepositoryInvocationTestUtils.VerifyingMethodInterceptor; @@ -169,8 +170,8 @@ public class ReflectionRepositoryInvokerUnitTests { @Test public void considersFormattingAnnotationsOnQueryMethodParameters() throws Exception { - HashMap parameters = new HashMap(); - parameters.put("date", new String[] { "2013-07-18T10:49:00.000+02:00" }); + Map parameters = Collections.singletonMap("date", + new String[] { "2013-07-18T10:49:00.000+02:00" }); Method method = PersonRepository.class.getMethod("findByCreatedUsingISO8601Date", Date.class, Pageable.class); PersonRepository repository = mock(PersonRepository.class); @@ -238,6 +239,23 @@ public class ReflectionRepositoryInvokerUnitTests { invoker.invokeSave(new Object()); } + /** + * @see DATACMNS-647 + */ + @Test + public void translatesCollectionRequestParametersCorrectly() throws Exception { + + for (String[] ids : Arrays.asList(new String[] { "1,2" }, new String[] { "1", "2" })) { + + Map parameters = Collections.singletonMap("ids", ids); + + Method method = PersonRepository.class.getMethod("findByIdIn", Collection.class); + PersonRepository repository = mock(PersonRepository.class); + + getInvokerFor(repository, expectInvocationOf(method)).invokeQueryMethod(method, parameters, null, null); + } + } + private static RepositoryInvoker getInvokerFor(Object repository) { RepositoryMetadata metadata = new DefaultRepositoryMetadata(repository.getClass().getInterfaces()[0]);