DATAKV-169 - Add support for IN keyword in derived queries.

We now support `In` keyword in derived finder methods (eg. `findByFirstnameIn(Collection<String> firstnames)`) when using SpEL based queries.

Original pull request: #23.
This commit is contained in:
Christoph Strobl
2017-04-13 15:39:37 +02:00
committed by Mark Paluch
parent fe897b2888
commit c98c82e832
3 changed files with 134 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import org.joda.time.format.DateTimeFormatter;
@@ -380,6 +381,66 @@ public class SpelQueryCreatorUnitTests {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(ROBB), is(false));
}
/**
* @see DATAKV-169
*/
@Test
public void inReturnsMatchCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(ROBB), is(true));
}
/**
* @see DATAKV-169
*/
@Test
public void inNotMatchingReturnsCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
}
/**
* @see DATAKV-169
*/
@Test
public void inWithNullCompareValuesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
}
/**
* @see DATAKV-169
*/
@Test
public void inWithNullSourceValuesMatchesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(false));
}
/**
* @see DATAKV-169
*/
@Test
public void inMatchesNullValuesCorrectly() throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(true));
}
private Evaluation evaluate(String methodName, Object... args) throws Exception {
return new Evaluation((SpelExpression) createQueryForMethodWithArgs(methodName, args).getCritieria());
}
@@ -464,6 +525,9 @@ public class SpelQueryCreatorUnitTests {
// Type.REGEX
Person findByLastnameMatches(String lastname);
// Type.IN
Person findByFirstnameIn(ArrayList<String> in);
}
static class Evaluation {

View File

@@ -39,7 +39,7 @@ import org.springframework.data.repository.CrudRepository;
/**
* Base class for test cases for repository implementations.
*
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Thomas Darimont
@@ -200,13 +200,60 @@ public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUn
assertThat(result.get(0).getFirstname(), is(CERSEI.getFirstname()));
}
/**
* @see DATAKV-169
*/
@Test
public void findsByValueInCollectionCorrectly() {
repository.save(LENNISTERS);
List<Person> result = repository.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname()));
assertThat(result, hasSize(2));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME)));
}
/**
* @see DATAKV-169
*/
@Test
public void findsByValueInCollectionCorrectlyWhenTargetPathContainsNullValue() {
repository.save(LENNISTERS);
repository.save(new Person(null, 10));
List<Person> result = repository.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname()));
assertThat(result, hasSize(2));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME)));
}
/**
* @see DATAKV-169
*/
@Test
public void findsByValueInCollectionCorrectlyWhenTargetPathAndCollectionContainNullValue() {
repository.save(LENNISTERS);
Person personWithNullAsFirstname = new Person(null, 10);
repository.save(personWithNullAsFirstname);
List<Person> result = repository
.findByFirstnameIn(Arrays.asList(CERSEI.getFirstname(), JAIME.getFirstname(), null));
assertThat(result, hasSize(3));
assertThat(result, is(containsInAnyOrder(CERSEI, JAIME, personWithNullAsFirstname)));
}
protected KeyValueRepositoryFactory createKeyValueRepositoryFactory(KeyValueOperations operations) {
return new KeyValueRepositoryFactory(operations);
}
protected abstract T getRepository(KeyValueRepositoryFactory factory);
public static interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
public interface PersonRepository extends CrudRepository<Person, String>, KeyValueRepository<Person, String> {
List<Person> findByAge(int age);
@@ -225,6 +272,8 @@ public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUn
List<PersonSummary> findByAgeGreaterThan(int age, Sort sort);
<T> List<T> findByAgeGreaterThan(int age, Sort sort, Class<T> projectionType);
List<Person> findByFirstnameIn(List<String> firstname);
}
interface PersonSummary {