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:
committed by
Mark Paluch
parent
979f3a33a8
commit
f67a66bf25
@@ -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;
|
||||
@@ -260,6 +261,51 @@ public class SpelQueryCreatorUnitTests {
|
||||
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(ROBB), is(false));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
public void inReturnsMatchCorrectly() throws Exception {
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add(ROBB.firstname);
|
||||
|
||||
assertThat(evaluate("findByFirstnameIn", list).against(ROBB), is(true));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
public void inNotMatchingReturnsCorrectly() throws Exception {
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add(ROBB.firstname);
|
||||
|
||||
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
public void inWithNullCompareValuesCorrectly() throws Exception {
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add(null);
|
||||
|
||||
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
public void inWithNullSourceValuesMatchesCorrectly() throws Exception {
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add(ROBB.firstname);
|
||||
|
||||
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(false));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
public void inMatchesNullValuesCorrectly() throws Exception {
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
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());
|
||||
}
|
||||
@@ -344,6 +390,9 @@ public class SpelQueryCreatorUnitTests {
|
||||
// Type.REGEX
|
||||
Person findByLastnameMatches(String lastname);
|
||||
|
||||
// Type.IN
|
||||
Person findByFirstnameIn(ArrayList<String> in);
|
||||
|
||||
}
|
||||
|
||||
static class Evaluation {
|
||||
|
||||
@@ -168,13 +168,51 @@ public abstract class AbstractRepositoryUnitTests<T extends AbstractRepositoryUn
|
||||
assertThat(result.get(0).getFirstname(), is(CERSEI.getFirstname()));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
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)));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
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)));
|
||||
}
|
||||
|
||||
@Test // DATAKV-169
|
||||
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);
|
||||
|
||||
@@ -193,6 +231,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 {
|
||||
|
||||
Reference in New Issue
Block a user