DATAMONGO-1608 - Polishing.
Throw an IllegalArgumentException when trying to create a query using 'null' as an argument for queries resulting in a $regex query operator. Original Pull Request: #439
This commit is contained in:
@@ -300,9 +300,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
criteria = criteria.not();
|
||||
}
|
||||
|
||||
Object next = parameters.next();
|
||||
|
||||
return addAppropriateLikeRegexTo(criteria, part, next != null ? next.toString() : "");
|
||||
return addAppropriateLikeRegexTo(criteria, part, parameters.next());
|
||||
|
||||
case NEVER:
|
||||
// intentional no-op
|
||||
@@ -330,7 +328,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
return criteria.in(nextAsArray(parameters));
|
||||
}
|
||||
|
||||
return addAppropriateLikeRegexTo(criteria, part, parameters.next().toString());
|
||||
return addAppropriateLikeRegexTo(criteria, part, parameters.next());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,9 +339,15 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
* @param value
|
||||
* @return the criteria extended with the regex.
|
||||
*/
|
||||
private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, String value) {
|
||||
private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, Object value) {
|
||||
|
||||
return criteria.regex(toLikeRegex(value, part), toRegexOptions(part));
|
||||
if (value == null) {
|
||||
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Argument for creating $regex pattern for property '%s' must not be null!", part.getProperty().getSegment()));
|
||||
}
|
||||
|
||||
return criteria.regex(toLikeRegex(value.toString(), part), toRegexOptions(part));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,7 +30,9 @@ import java.util.stream.Stream;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
@@ -72,6 +74,8 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
|
||||
public @Rule ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Autowired protected PersonRepository repository;
|
||||
|
||||
@Autowired MongoOperations operations;
|
||||
@@ -171,6 +175,15 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(result, hasItem(boyd));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1608
|
||||
public void findByFirstnameLikeWithNull() {
|
||||
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("property 'firstname'");
|
||||
|
||||
repository.findByFirstnameLike(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsPagedPersons() throws Exception {
|
||||
|
||||
@@ -658,9 +671,10 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
@Test // DATAMONGO-1608
|
||||
public void findByFirstNameIgnoreCaseWithNull() {
|
||||
|
||||
List<Person> result = repository.findByFirstnameIgnoreCase(null);
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("property 'firstname'");
|
||||
|
||||
assertThat(result.size(), is(0));
|
||||
repository.findByFirstnameIgnoreCase(null);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-770
|
||||
|
||||
Reference in New Issue
Block a user