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:
Christoph Strobl
2017-02-10 09:36:32 +01:00
parent fb3789be35
commit 71fbb910ec
2 changed files with 26 additions and 8 deletions

View File

@@ -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));
}
/**

View File

@@ -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